serialize method

  1. @override
ComponentSpec? serialize(
  1. Component component,
  2. SerializeContext context
)
override

Serializes component to a ComponentSpec, or returns null if this codec does not handle that component instance.

The returned spec must be freshly built (or a copy); the registry adjusts its properties in place, so a spec aliased by the live component (a retained placeholder spec) would see snapshots change retroactively.

Implementation

@override
ComponentSpec? serialize(Component component, SerializeContext context) {
  if (component is! C) return null;
  final properties = <String, PropertyValue>{};
  for (final field in resolvedFields) {
    final read = field.read;
    if (read == null || field.def.transient) continue;
    final value = read(component, context);
    if (value == null) continue;
    // Delta persistence: a value equal to the schema default is implied.
    if (propertyValuesEqual(value, field.def.defaultValue)) continue;
    properties[field.def.name] = value;
  }
  return ComponentSpec(type, properties: properties);
}