Class JavaBeanDeserializer
- All Implemented Interfaces:
JsonbDeserializer<Object>
Decoder for converting JSON to a Java object graph. The decoding is based on the expected type passed to the decode method.
The conversion rules are as follows:
-
If the expected type is a primitive, an array, a
Collection
, aMap
or aJsonValue
(i.e. cannot be a JavaBean)DeserializationContext.deserialize(java.lang.Class<T>, jakarta.json.stream.JsonParser)
is used for the conversion. -
If the expected type is neither of the above, it is assumed to be a JavaBean and the JSON input must be a JSON object. The key/value pairs of the JSON input are interpreted as properties of the JavaBean and set if the values have been parsed successfully. The type of the properties are used as expected types when parsing the values.
Constructors with
ConstructorProperties
are used if all required values are available. Else, if no setter is available for a key/value pair, an attempt is made to gain access to a private field with the name of the key and assign the value to that field. Note that this will fail when using Java 9 modules unless you explicitly grant the decoder access to private fields. So defining a constructor with aConstructorProperties
annotation and all immutable properties as parameters is strongly recommended.
A JSON object can have a "@class" key. It must be the first key provided by the parser, i.e. the property order strategy must be lexicographic. Its value is used to instantiate the Java object in which the information of the JSON object is stored. If provided, the class specified by this key/value pair overrides the class passed as expected class. It is checked, however, that the specified class is assignable to the expected class.
The value specified is first matched against the aliases that
have been registered with the decoder
(see addAlias(Class, String)
). If no match is found,
the converter set with setClassConverter(Function)
is used to convert the name to a class. The function defaults
to Class.forName(String)
. If the converter does not
return a result, DeserializationContext.deserialize(java.lang.Class<T>, jakarta.json.stream.JsonParser)
is
used for the conversion.
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionAdds an alias for a class.deserialize
(JsonParser parser, DeserializationContext ctx, Type rtType) setClassConverter
(Function<String, Optional<Class<?>>> converter) Sets the converter that maps a specified "class" to an actual JavaClass
.Cause this decoder to silently skip information from the JSON source that cannot be mapped to a property of the bean being created.Methods inherited from class org.jdrupes.jsonb.beans.JavaBeanConverter
findBeanInfo, findPropertyEditor
-
Constructor Details
-
JavaBeanDeserializer
public JavaBeanDeserializer()Creates a new instance.
-
-
Method Details
-
addAlias
Adds an alias for a class.- Parameters:
clazz
- the clazzalias
- the alias- Returns:
- the java bean serializer
-
setClassConverter
-
skipUnknown
Cause this decoder to silently skip information from the JSON source that cannot be mapped to a property of the bean being created. This is useful if e.g. a REST invocation returns data that you are not interested in and therefore don't want to model in your JavaBean.- Returns:
- the decoder for chaining
-
deserialize
- Specified by:
deserialize
in interfaceJsonbDeserializer<Object>
-