json<D> static method

JsonTypeConverter<D, String> json<D>({
  1. required D fromJson(
    1. dynamic json
    ),
  2. dynamic toJson(
    1. D column
    )?,
  3. JsonCodec json = convert.json,
})

Creates a type converter for storing complex Dart objects in a text column by serializing them as JSON.

This requires supplying fromJson, a function responsible for mapping the parsed JSON structure to the Dart type D. Optionally, you can also be explicit about the other direction via toJson. By default, Dart's JSON encoder simply calls toJson() on the object.

Finally, the json codec itself can be customized as well if needed.

Implementation

static JsonTypeConverter<D, String> json<D>({
  required D Function(dynamic json) fromJson,
  dynamic Function(D column)? toJson,
  convert.JsonCodec json = convert.json,
}) {
  return _JsonBasedConverter<D>(
    mapFromJson: fromJson,
    mapToJson: toJson ?? (value) => value,
    json: json,
  );
}