serialize method

  1. @override
Object? serialize(
  1. DartType targetType,
  2. String expression,
  3. TypeHelperContextWithConvert context
)
override

Returns Dart code that serializes an expression representing a Dart object of type targetType.

If targetType is not supported, returns null.

Let's say you want to serialize a class Foo as just its id property of type int.

Treating expression as a opaque Dart expression, the serialize implementation could be a simple as:

String serialize(DartType targetType, String expression) =>
  "$expression.id";
```.

Implementation

@override
Object? serialize(
  DartType targetType,
  String expression,
  TypeHelperContextWithConvert context,
) {
  final toJsonData = context.serializeConvertData;
  if (toJsonData == null) {
    return null;
  }

  assert(toJsonData.paramType is TypeParameterType ||
      targetType.isAssignableTo(toJsonData.paramType));
  return LambdaResult(expression, toJsonData.name);
}