Skip to main content

Napacitor Data Types

Data moving between the web runtime and C# in Napacitor is serialized as JSON. The supported data types are those that can be represented in JSON: numbers, strings, booleans, arrays, and objects. Both directions go through a single set of shared serializer options (NapacitorJson), so mapping rules are identical on every platform.

Serializer conventions​

The shared JsonSerializerOptions use:

  • camelCase property naming — JS-style { "batteryLevel": 0.8 } maps to the C# property BatteryLevel;
  • case-insensitive property matching — batteryLevel, BatteryLevel, and batterylevel all resolve to the same C# property;
  • standard System.Text.Json rules otherwise (numbers, strings, booleans, null, arrays, objects).

This mirrors Capacitor's behavior, where JS object keys are transferred verbatim and native code must match them; in Napacitor the runtime accepts both camelCase and PascalCase, so a C# DTO written in normal PascalCase just works against JS camelCase data.

Parameter binding​

A [NapacitorMethod] receives arguments in one of three shapes:

  1. Zero parameters — for calls like getInfo() with no options;
  2. One strongly typed DTO — the bridge deserializes the JS arguments object into it (camelCase → PascalCase, case-insensitive);
  3. One NapacitorCall — loose access to the raw JSON payload.
[NapacitorMethod]
public Task<BatteryInfo> GetBatteryInfo() { … } // (1)

[NapacitorMethod]
public Task Share(ShareOptions options) { … } // (2) DTO

[NapacitorMethod]
public Task<string> Echo(NapacitorCall call) // (3) loose access
{
var value = call.Get<string>("value");
…
}

Return values are serialized with the same options: a C# object with PascalCase properties becomes camelCase JSON on the JS side.

Null Values​

C# can express nulls natively (null for reference types, Nullable<T> for value types), so no NSNull-style placeholder is needed (unlike Objective-C in Capacitor). Prefer nullable DTO properties over relying on key presence:

public class ShareOptions
{
public string? Title { get; set; } // JS: { "title": null } or omitted — both fine
public int? Count { get; set; }
}

When using NapacitorCall, test for presence and type rather than assuming a key exists. null in JSON is a valid value, not an absence.

Dates​

Dates serialize as ISO 8601 strings — the default System.Text.Json behavior. A JS Date that is stringified to ISO 8601 (or an ISO string produced by the web code) deserializes to DateTime/DateTimeOffset on the C# side, and C# dates serialize back to ISO strings. For full control, serialize dates to your preferred JSON type in the plugin before returning them.

Web implementations​

The same DTO types are used by the web implementations (web.ts), so a plugin that has a web fallback returns identical JSON shapes from the browser — only the data source differs. Platform-only methods that throw are surfaced to JS as NapacitorException with code NOT_IMPLEMENTED.