Serializer class

Static JSON serializer with type registration.

The registry state is held in process-wide static maps, which makes Serializer thread-unsafe across isolates. For server-side use cases that need isolation, create your own subclass or wrap the registration calls in your own mutex. For typical Flutter / CLI use (single isolate) the current implementation is sufficient.

Use Serializer.reset between tests to clear the registry and Serializer.snapshot to introspect it for debugging.

Constructors

Serializer()

Properties

hashCode int
The hash code for this object.
no setterinherited
runtimeType Type
A representation of the runtime type of the object.
no setterinherited

Methods

noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
toString() String
A string representation of this object.
inherited

Operators

operator ==(Object other) bool
The equality operator.
inherited

Static Methods

encodeDynamic(Object? value) Object?
Encodes a runtime value to a JSON-compatible structure.
formatDate(DateTime value, String pattern) String
Formats a DateTime with a supported pattern.
fromDynamic<T>(dynamic decoded) → T
Deserializes a decoded JSON value into type T.
fromJson<T>(String json) → T
Deserializes JSON text into type T.
parseDate(String value, String pattern) DateTime
Parses a DateTime using a supported pattern.
register<T>({required JsonFactory<T> fromJson, required JsonEncoder<T> toJson}) → void
Registers conversion functions for a specific type.
registerUnion<T>({required String typeField, required String discriminator, required JsonFactory<T> fromJson}) → void
Registers a union subtype factory under a discriminator value.
reset() → void
Resets the global registry.
snapshot() SerializerSnapshot
Returns an immutable snapshot of the current registry state.
toJson<T>(T value) String
Serializes value to JSON text.
validateMapKeys(Map map) → void
Validates that the keys of map can be safely converted to JSON keys.