json2<D> static method

JsonTypeConverter2<D, String, Object?> json2<D>({
  1. required D fromJson(
    1. Object? json
    ),
  2. Object? 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.

For sqlite3 databases, jsonb can be used as an alternative encoding for binary columns.

Implementation

static JsonTypeConverter2<D, String, Object?> json2<D>({
  required D Function(Object? json) fromJson,
  Object? Function(D column)? toJson,
  convert.JsonCodec json = convert.json,
}) {
  return _DefaultJsonConverter<D, String, Object?>(
    mapFromJson: fromJson,
    mapToJson: toJson ?? (value) => value,
    jsonForDb: json,
  );
}