- java.lang.Object
-
- com.gluonhq.connect.converter.JsonConverter<T>
-
- Type Parameters:
T
- the type of the object to convert from and into a JSON Object
public class JsonConverter<T> extends Object
A utility class to convert Java objects from JSON Objects and from JSON Objects into Java objects.
-
-
Constructor Summary
Constructors Constructor Description JsonConverter(Class<T> targetClass)
Construct a JsonConverter to convert between JSON and objects of the specifiedtargetClass
.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description 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.
-
-
-
Method Detail
-
getTargetClass
public Class<T> getTargetClass()
Returns the target class that defines the objects being converted from and into JSON objects.- Returns:
- The target class.
-
readFromJson
public T readFromJson(javax.json.JsonObject json)
Convert the provided JSON Object into a Java object. If a new instance could not be created from the specifiedtargetClass
in the constructor, thennull
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.
- Parameters:
json
- the instance of the JSON Object that needs to be converted into a Java object- Returns:
- The Java object that is converted from the provided JSON Object.
-
writeToJson
public javax.json.JsonObject writeToJson(T t)
Convert the provided Java object into a JSON Object.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.- Parameters:
t
- the Java object to convert into a JSON Object- Returns:
- The JSON Object that was converted from the provided Java object.
-
-