T
- the type of the object to convert from and into a JSON Objectpublic class JsonConverter<T>
extends java.lang.Object
Constructor and Description |
---|
JsonConverter(java.lang.Class<T> targetClass)
Construct a JsonConverter to convert between JSON and objects of the specified
targetClass . |
Modifier and Type | Method and Description |
---|---|
java.lang.Class<T> |
getTargetClass()
Returns the target class that defines the objects being converted from and into JSON objects.
|
T |
readFromJson(javax.json.JsonObject json)
Convert the provided JSON Object into a Java object.
|
javax.json.JsonObject |
writeToJson(T t)
Convert the provided Java object into a JSON Object.
|
public JsonConverter(java.lang.Class<T> targetClass)
targetClass
.targetClass
- The target class defining the objects being converted from and into JSON Objects.public java.lang.Class<T> getTargetClass()
public T readFromJson(javax.json.JsonObject json)
targetClass
in the constructor, then null
will be returned.
The conversion works by inspecting all the property methods of the target class. A property method is any field that has both a getter and a setter method. The name of the property is taken from the getter method, by stripping the string "get" from the method name and converting the first character from upper case to lower case. It's possible to override the property name by adding an @XmlElement annotation. Then the name value of the annotation will be used as the property name.
The property name will then be looked up in the provided JSON Object. If a key was not found, the property will be ignored. Otherwise, the setter method will be called with the value from the JSON Object that is mapped to the key. The JsonConverter is able to convert from all types of JSON values, except for nested JSON Arrays.
json
- the instance of the JSON Object that needs to be converted into a Java objectpublic javax.json.JsonObject writeToJson(T t)
The conversion works by inspecting all the property methods of the target class. A property method is any field that has both a getter and a setter method. The name of the property is taken from the getter method, by stripping the string "get" from the method name and converting the first character from upper case to lower case. It's possible to override the property name by adding an @XmlElement annotation. Then the name value of the annotation will be used as the property name.
The property name will then be used as the key inside the JSON Object. Where the value will be the value that was returned by calling the getter method on the provided Java object.
As the return type of the getter method, all primitive java types are supported as well as the basic JavaFX
property objects (like BooleanProperty, IntegerProperty, etc...). Lists
are supported as
well and will be converted into a JSON Array. If the getter returns any other type, then the returned value will
be converted into a JSON Object as well by using a JsonConverter.
t
- the Java object to convert into a JSON Object