Class JsonBeanDecoder
- java.lang.Object
-
- org.jdrupes.json.JsonCodec
-
- org.jdrupes.json.JsonBeanDecoder
-
public class JsonBeanDecoder extends JsonCodec
Decoder for converting JSON to a Java object graph.The decoding is based on the expected type passed to the decode method.
The convertion rules are as follows:
-
If the expected type is
Object
and the JSON input is an array, the JSON array is converted to aJsonArray.DefaultJsonArray
, which is anArrayList
with element typeObject
and some helpful accessor methods. -
If the expected type implements
Collection
a container of the expected type is created. Its element type is againObject
. The JSON input must be an array. -
If the expected type is an array type, an array of the expected type with the given element type is created. The JSON input must be an array.
-
In all cases above, the element type is passed as expected type when decoding the members of the JSON array.
-
If the expected type is an
Object
and the JSON input is a JSON object, the input is converted to aJsonObject.DefaultJsonObject
, which is aHashMap<String,Object>
with some helpful accessor methods. -
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 passed 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 thatthis 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 of the object. 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 withsetClassConverter(Function)
is used to convert the name to a class. The function defaults toClass.forName(String)
. If the converter does not return a result, aJsonObject
is used as container for the values provided by the JSON object. -
-
-
Constructor Summary
Constructors Constructor Description JsonBeanDecoder(com.fasterxml.jackson.core.JsonParser parser)
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description JsonBeanDecoder
addAlias(Class<?> clazz, String alias)
Add an alias for the given class.static JsonBeanDecoder
create(com.fasterxml.jackson.core.JsonParser parser)
Create a new decoder using the given parser.static JsonBeanDecoder
create(Reader in)
Create a new decoder using a defaultJsonParser
.static JsonBeanDecoder
create(String input)
Create a new decoder using a default parser to parse the given string.<T> T
readArray(Class<T> expected)
Read a JSON array description into a new array of the expected type.JsonObject
readObject()
Read a JSON object description into a newJsonObject
.<T> T
readObject(Class<T> expected)
Read a JSON object description into a new object of the expected type.JsonBeanDecoder
setClassConverter(Function<String,Optional<Class<?>>> converter)
Sets the converter that maps a specified “class” to an actual JavaClass
.-
Methods inherited from class org.jdrupes.json.JsonCodec
clearCaches, defaultFactory, findBeanInfo, findPropertyEditor
-
-
-
-
Constructor Detail
-
JsonBeanDecoder
public JsonBeanDecoder(com.fasterxml.jackson.core.JsonParser parser)
-
-
Method Detail
-
addAlias
public JsonBeanDecoder addAlias(Class<?> clazz, String alias)
Description copied from class:JsonCodec
Add an alias for the given class.If defined, the alias will be used instead of the class name by the encoder.
-
setClassConverter
public JsonBeanDecoder setClassConverter(Function<String,Optional<Class<?>>> converter)
Sets the converter that maps a specified “class” to an actual JavaClass
.If it does not return a class, a
HashMap
is used to store the data of the JSON object.- Parameters:
converter
- the converter to use- Returns:
- the conversion result
-
create
public static JsonBeanDecoder create(Reader in)
Create a new decoder using a defaultJsonParser
.- Parameters:
in
- the source- Returns:
- the decoder
-
create
public static JsonBeanDecoder create(String input)
Create a new decoder using a default parser to parse the given string.- Parameters:
input
- the input- Returns:
- the decoder
-
create
public static JsonBeanDecoder create(com.fasterxml.jackson.core.JsonParser parser)
Create a new decoder using the given parser.- Parameters:
parser
- the parser- Returns:
- the decoder
-
readObject
public JsonObject readObject() throws JsonDecodeException
Read a JSON object description into a newJsonObject
.- Returns:
- the object
- Throws:
JsonDecodeException
-
readObject
public <T> T readObject(Class<T> expected) throws JsonDecodeException
Read a JSON object description into a new object of the expected type.The result may have a type derived from the expected type if the JSON read has a
class
key.- Parameters:
expected
- the expected type- Returns:
- the result
- Throws:
JsonDecodeException
-
readArray
public <T> T readArray(Class<T> expected) throws JsonDecodeException
Read a JSON array description into a new array of the expected type.- Type Parameters:
T
- the generic type- Parameters:
expected
- the expected type- Returns:
- the result
- Throws:
JsonDecodeException
-
-