ObjectSerializer<T, G extends Generator, Context extends SerializationContext<Generator> > class
abstract
A generic, extensible object serializer responsible for converting Dart objects into structured, generator-friendly representations (such as JSON objects, YAML maps, or custom AST nodes).
ObjectSerializer<T, Generator> forms the core abstraction used by JetLeaf’s
serialization framework. It defines how instances of T are transformed into
transferable structures using a format-specific Generator and a
SerializationContext for nested and custom serialization strategies.
Purpose
Serializers allow JetLeaf (and downstream frameworks) to:
- Serialize annotated or runtime-discovered Dart classes
- Apply naming strategies (e.g., snake_case vs camelCase)
- Produce consistent, predictable output across formats
- Integrate custom serializers for domain-specific types
Typical Usage
final generator = JsonGenerator();
final provider = SerializerProvider.default();
final serializer = UserSerializer();
serializer.serialize(user, generator, provider);
print(generator.toJsonString());
// {"id":1,"name":"Alice"}
Responsibilities
- Convert an instance of
Tinto a structured object representation - Emit map/array/primitive entries through the provided Generator
- Transform field names using the active NamingStrategy
- Delegate nested objects to SerializationContext
- Ensure output is structurally valid for the target format
Serialization Pipeline
| Step | Description |
|---|---|
| 1 | Verify serializer compatibility via canSerialize |
| 2 | Begin object structure (e.g., startObject()) |
| 3 | Emit each field with transformed key names |
| 4 | Serialize nested objects using the provider |
| 5 | End object structure and finalize output |
Design Notes
- Serialization is intentionally separated from parsing/formatting, allowing different backends (JSON, YAML, BSON, etc.) to share the same model-level serializers.
- Works directly with JetLeaf’s reflection API (
Class<T>) to determine fields, annotations, and visibility rules. - Fully supports user-implemented serializers for custom models.
Error Handling
Implementations must throw clear, descriptive errors when:
- A required field cannot be written
- A value is unsupported by the serializer or generator
- Output structure would become invalid or inconsistent
See Also
- Generator — writes structured output for the target format
- SerializationContext — locates nested serializers
- NamingStrategy — transforms field names consistently
- JsonSerializer — default JSON implementation
- Implemented types
- Implementers
- BoolSerializer
- DateTimeSerializationAdapter
- DoubleSerializer
- DurationSerializationAdapter
- IntSerializer
- JsonSerializationAdapter
- JsonSerializer
- LocalDateSerializationAdapter
- LocalDateTimeSerializationAdapter
- NumSerializer
- StringSerializer
- UriSerializationAdapter
- UrlSerializationAdapter
- XmlSerializationAdapter
- XmlSerializer
- YamlSerializationAdapter
- YamlSerializer
- ZonedDateTimeSerializationAdapter
- Annotations
-
- @Generic.new(ObjectSerializer)
Constructors
- ObjectSerializer()
-
Creates a new ObjectSerializer.
const
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
-
canSerialize(
Class type) → bool -
Returns whether the given
typecan be serialized by the system. -
noSuchMethod(
Invocation invocation) → dynamic -
Invoked when a nonexistent method or property is accessed.
inherited
-
serialize(
T value, G generator, Context serializer) → void -
Serializes an object of type
Tinto an object using the provided Generator and SerializationContext. -
supportsContext(
SerializationContext< Generator> context) → bool - Determines whether this serializer supports the given serialization context.
-
toClass(
) → Class< T> -
Provides a contract for types capable of exposing their associated
runtime
Classmetadata representation within the JetLeaf reflection system.inherited -
toString(
) → String -
A string representation of this object.
inherited
Operators
-
operator ==(
Object other) → bool -
The equality operator.
inherited
Static Properties
-
CLASS
→ Class<
ObjectSerializer< dynamic, Generator, SerializationContext< >Generator> > -
Represents the ObjectSerializer type for reflection and type resolution.
final