Class JavaBeanDeserializer

java.lang.Object
org.jdrupes.jsonb.beans.JavaBeanConverter
org.jdrupes.jsonb.beans.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, a Map or a JsonValue (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 a ConstructorProperties 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.