Skip to content

JSON Converters API ​

JsonConverter<T> Interface ​

dart
abstract interface class JsonConverter<T> {
  T fromJson(dynamic json);
  dynamic toJson(T value);
  T get defaultValue;
}
MethodDescription
fromJson(json)Convert JSON to typed value
toJson(value)Convert typed value to JSON
defaultValueDefault value for the type

BaseJsonConverter<T> ​

Abstract base class with error handling. Subclasses override convertFromJson and convertToJson instead of fromJson/toJson directly.

dart
abstract base class BaseJsonConverter<T> implements JsonConverter<T> {
  const BaseJsonConverter();
  T convertFromJson(dynamic json);      // implement this
  dynamic convertToJson(T value);       // implement this
}

Errors are wrapped in JsonConversionException.

JsonConversionException ​

dart
final class JsonConversionException implements Exception {
  final String message;
  final dynamic json;
  final Type targetType;
  final Object? cause;
}

Built-in Converters ​

StringJsonConverter ​

AspectValue
TypeString
Default''
fromJsonnull returns '', non-strings converted via .toString()
toJsonReturns the string as-is

IntJsonConverter ​

AspectValue
Typeint
Default0
fromJsonParses int, double (truncated), or String
toJsonReturns the integer as-is

DoubleJsonConverter ​

AspectValue
Typedouble
Default0.0
fromJsonParses double, int (promoted), or String
toJsonReturns the double as-is

BoolJsonConverter ​

AspectValue
Typebool
Defaultfalse
fromJsonParses bool, int (0/1), or String ('true'/'false')
toJsonReturns the boolean as-is

DateTimeJsonConverter ​

AspectValue
TypeDateTime
DefaultDateTime(0)
fromJsonParses ISO 8601 string or DateTime instance
toJsonReturns ISO 8601 string via .toIso8601String()

NullableJsonConverter ​

Wraps another converter to handle nullable values.

dart
final class NullableJsonConverter<T> implements JsonConverter<T?> {
  final JsonConverter<T> innerConverter;
  const NullableJsonConverter(this.innerConverter);
}
AspectValue
Defaultnull
fromJsonReturns null if input is null, delegates to inner converter otherwise
toJsonReturns null if value is null, delegates to inner converter otherwise

EnumJsonConverter ​

For Dart enum types (requires T extends Enum).

dart
final class EnumJsonConverter<T extends Enum> extends BaseJsonConverter<T> {
  final List<T> values;
  const EnumJsonConverter(this.values);
}
AspectValue
DefaultFirst value in the list
fromJsonMatches by .name, .toString(), or index
toJsonReturns .name

EnumOptionsJsonConverter ​

For any type T using EnumOption<T> for matching. Auto-registered by EnumProperty.

dart
final class EnumOptionsJsonConverter<T> extends BaseJsonConverter<T> {
  final List<EnumOption<T>> options;
  const EnumOptionsJsonConverter(this.options);
}
AspectValue
DefaultFirst option's value
fromJsonMatches by Enum.name, .toString(), or index
toJsonReturns .name for enums, .toString() for others

ListJsonConverter ​

Converts lists by delegating each item to an inner converter.

dart
final class ListJsonConverter<T> extends BaseJsonConverter<List<T>> {
  final JsonConverter<T> itemConverter;
  const ListJsonConverter(this.itemConverter);
}
AspectValue
Default<T>[]
fromJsonnull returns empty list. Single item wrapped in list.
toJsonMaps each item through itemConverter.toJson()

UnionJsonConverter ​

For UnionProperty's selected key string. The full union JSON structure is handled by UnionProperty.toJson()/fromJson().

dart
final class UnionJsonConverter extends BaseJsonConverter<String> {
  const UnionJsonConverter();
}
AspectValue
Default''
fromJsonExtracts selectedKey from map, or converts to string
toJsonReturns the string as-is

Auto-Registered Defaults ​

PropertySystem auto-registers these on first access:

TypeConverterEditor
StringStringJsonConverterTextPropertyEditor
intIntJsonConverterIntPropertyEditor
doubleDoubleJsonConverterDoublePropertyEditor
boolBoolJsonConverterBoolPropertyEditor
DateTimeDateTimeJsonConverterDateTimePropertyEditor
String?NullableJsonConverter<String>OptionalStringPropertyEditor
int?NullableJsonConverter<int>OptionalIntPropertyEditor
double?NullableJsonConverter<double>OptionalDoublePropertyEditor
bool?NullableJsonConverter<bool>OptionalBoolPropertyEditor
DateTime?NullableJsonConverter<DateTime>OptionalDateTimePropertyEditor

Custom key registrations:

KeyConverterEditor
readonly:string--ReadOnlyPropertyEditor<String>
readonly:int--ReadOnlyPropertyEditor<int>
readonly:double--ReadOnlyPropertyEditor<double>
readonly:bool--ReadOnlyPropertyEditor<bool>
readonly:datetime--ReadOnlyPropertyEditor<DateTime>
unionUnionJsonConverter--

Next Steps ​