JsonSerializer<T extends Object> class abstract

Custom code that writes values of T as JSON, for a type the mapper cannot handle or should write differently.

The mapper writes introspected classes, enums and the common Dart types on its own. Write a serializer for a class you cannot annotate (from another package), or for a compact form such as money as "12.50 EUR". It returns plain JSON values; maps and lists it returns are written further, so they may hold other objects. Register it with ObjectMapper(serializers: [...]), in a JsonModule, or, in Spring Boot, as a bean: the auto-configured mapper picks it up. Serializers are tried in order before the built-in handling, the first that handles a value wins, and subtypes of T count. Pair it with a JsonDeserializer to read the same form back.

// Money is written as one string, "1250 EUR".
final class MoneySerializer extends JsonSerializer<Money> {
  const MoneySerializer();
  @override
  Object? serialize(Money value, ObjectMapper mapper) =>
      '${value.amount} ${value.currency}';
}

Spring (Java):

class MoneySerializer extends JsonSerializer<Money> {
  public void serialize(
      Money value, JsonGenerator gen, SerializerProvider p) { ... }
}

Constructors

JsonSerializer()
Lets subclasses be const.
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

handles(Object value) → bool
Whether this serializer writes value; by default, whether it is a T (subtypes included). Override it to be more selective.
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
serialize(T value, ObjectMapper mapper) → Object?
Returns the JSON form of value: null, a bool, a number, a string, or a list or map, whose elements the mapper writes in turn. mapper is the mapper at work, for writing nested values with its settings.
toString() → String
A string representation of this object.
inherited
write(Object value, ObjectMapper mapper) → Object?
Calls serialize with value cast to T; the mapper uses it, since it only knows the serializer as a JsonSerializer<Object>.

Operators

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