JsonDeserializer<T extends Object> class abstract

Custom code that builds values of T from decoded JSON, for a type the mapper cannot read on its own or should read differently.

It is the reading half of a JsonSerializer: use it for classes you cannot annotate, or for a compact JSON form. It receives what jsonDecode produced (never null; a JSON null gives null without calling it). Register it with ObjectMapper(deserializers: [...]), in a JsonModule, or, in Spring Boot, as a bean. It is used when the requested type is exactly T or T?, not a subtype, before the built-in handling. An error it throws becomes a JsonMappingException with the path of the value.

// Reads the "1250 EUR" form back.
final class MoneyDeserializer extends JsonDeserializer<Money> {
  const MoneyDeserializer();
  @override
  Money deserialize(Object json, ObjectMapper mapper) {
    final [amount, currency] = (json as String).split(' ');
    return Money(int.parse(amount), currency);
  }
}

Spring (Java):

class MoneyDeserializer extends JsonDeserializer<Money> {
  public Money deserialize(JsonParser p, DeserializationContext c) { ... }
}

Constructors

JsonDeserializer()
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

deserialize(Object json, ObjectMapper mapper) → T
Returns a T built from the decoded json: a string, number, bool, list or map, never null. mapper is the mapper at work, for reading nested values with its settings. Throw when json has the wrong shape.
handles<S>() → bool
Whether this deserializer reads the requested type S: true for T and T?.
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