Interface JsonDeserializer
- All Known Implementing Classes:
BooleanConverter,EnumConverter,FieldMustBeNullConverter,IgnoredFieldConverter,IntegerConverter,JsonElementConverter,ListConverter,LocalDateConverter,LocalDateTimeConverter,LocalTimeConverter,LongConverter,MapConverter,NullableFieldConverter,OptionalFieldConverter,RecordConverter,StringConverter
The JSON side is represented by JsonElement. That is, parsing the JSON syntax is out-of-scope
for this interface. Only mapping the higher-level structure is done here.
Deserializing JSON is driven by the Type to deserialize. This is crucial because there is otherwise no
information about the Java class to generate from it. The class alone might not be enough though. This is most
obvious when deserializing an array to a List: Even when the caller specifies to deserialize the List class,
the deserializer will fail for the list elements because the element type is unknown. Therefore the deserializer
gets passed the whole type, such as List<Integer>, so it knows to deserialize the elements as Integer.
There is no precedence rule for deserializers based on specificity: If one deserializer supports supertype A, and a second one supports subtype B of A, then for an instance of B, the second one does NOT take precedence just because it is more specific. Instead, the registry will select the deserializer that was registered later.
-
Method Summary
Modifier and TypeMethodDescriptiondeserialize(com.google.gson.JsonElement json, Type type) Converts a value from JSON.default ObjectdeserializeAbsent(Type type) Converts a value from an absent JSON fragment.booleanChecks if this deserializer supports the specified type.
-
Method Details
-
supportsTypeForDeserialization
Checks if this deserializer supports the specified type.- Parameters:
type- the type to check- Returns:
- true if supported, false if not
-
deserialize
Converts a value from JSON.This method must not be called with a type for which
supportsTypeForDeserialization(Type)returns false. Calling it with such types anyway results in undefined behavior.- Parameters:
json- the JSONtype- the type to deserialize- Returns:
- the deserialized value
- Throws:
JsonDeserializationException- if the JSON does not match the expected structure
-
deserializeAbsent
Converts a value from an absent JSON fragment. This can be used to return a default for optional object properties.The standard implementation of this method is that missing values are not tolerated, and throws an exception.
This method must not be called with a type for which
supportsTypeForDeserialization(Type)returns false. Calling it with such types anyway results in undefined behavior.- Parameters:
type- the type to deserialize- Returns:
- the default value for that type
- Throws:
JsonDeserializationException- if the JSON does not match the expected structure
-