deserialize method
Deserializes an object of type T using the given P and
DeserializationContext.
This method reads tokens from the provided parser, interprets them
according to the target type metadata, and reconstructs an instance
of T.
Example
final user = UserDeserializer().deserialize(parser, context, Class<User>());
Responsibilities
- Interpret object tokens from the parser sequentially
- Use DeserializationContext for nested deserialization and converters
- Handle complex object graphs, lists, and maps recursively
- Return a fully reconstructed object of type
T
Error Handling
Should throw a descriptive exception if:
- object structure mismatches the expected type
- Required fields are missing or invalid
- Type conversion fails during object creation
See also
Implementation
@override
LocalDateTime? deserialize(P parser, DC ctxt, Class toClass) {
final raw = parser.getCurrentValue();
if (raw == null) {
return null;
}
try {
return ctxt.getConversionService().convert(raw, Class<LocalDateTime>());
} catch (_) {
if (raw is String) {
return LocalDateTime.parse(raw);
}
throw InvalidFormatException(
'Cannot deserialize LocalDateTime: expected a zoned ISO-8601 formatted string '
'(e.g. "2025-06-27T08:15:30.123"), '
'but received value "$raw" of type ${raw.runtimeType}.'
);
}
}