FX Gson
FX Gson is a set of type adapters for Google Gson to serialize JavaFX properties as their values, and deserialize values into properties.
FX Gson simply removes the property "wrapping" and delegates the serialization of the value to the Gson. This means that any configuration you add to Gson regarding a type will be taken into account when serializing a property of that type. This is true for objects and primitives.
Why use FX Gson?
In JavaFX, POJOs usually contain Property
objects instead of primitives. When serialized, we usually don't want to see the internals of such Property
objects in the produced JSON, but rather the actual value held by the property.
For instance, suppose the Person
class is defined like this:
public class Person {
private final StringProperty firstName;
private final StringProperty lastName;
public Person(String firstName, String lastName) {
this.firstName = new SimpleStringProperty(firstName);
this.lastName = new SimpleStringProperty(lastName);
}
// getters, setters, and property getters are omitted for brevity
}
Here is how new Person("Hans", "Muster")
is serialized:
With vanilla Gson | With FxGson-configured Gson |
---|---|
{ "firstName": { "name": "", "value": "Hans", "valid": true, "helper": { "observable": {} } }, "lastName": { "name": "", "value": "Muster", "valid": true, "helper": { "observable": {} } } } |
{ "firstName": "Hans", "lastName": "Muster" } |
This produces a more human-readable output, and make manual modifications of the JSON easier.
Usage
All you need to know is in the wiki, but here is a quick overview.
You can use FX Gson in multiple ways depending on the degree of customization you need:
-
directly create a ready-to-go
Gson
able to serialize JavaFX properties// to handle only Properties and Observable collections Gson fxGson = FxGson.create(); // to also handle the Color & Font classes Gson fxGsonWithExtras = FxGson.createWithExtras();
-
create a pre-configured
GsonBuilder
that you can further configure yourselfGson fxGson = FxGson.coreBuilder() .registerTypeAdapterFactory(new MyFactory()) .disableHtmlEscaping() .create(); Gson fxGsonWithExtras = FxGson.fullBuilder() .registerTypeAdapter(Pattern.class, new PatternSerializer()) .setPrettyPrinting() .create();
-
add JavaFX configuration to an existing
GsonBuilder
GsonBuilder builder = MyLib.getBuilder(); Gson gson = FxGson.addFxSupport(builder).create();
-
cherry-pick some pieces of FX Gson configuration and customize it to fit your needs
Java version compatiblity
Starting from FX Gson 4.0.0, the library is published as a Java Jigsaw module (Java 9+).
Setup - adding the dependency
Manual download
You may directly download the JAR from FX Gson's Bintray Repository, although I recommend using a build tool such as Gradle.
Gradle
compile 'org.hildan.fxgson:fx-gson:$VERSION'
or with the Kotlin DSL:
compile("org.hildan.fxgson:fx-gson:$VERSION")
Maven
<dependency>
<groupId>org.hildan.fxgson</groupId>
<artifactId>fx-gson</artifactId>
<version>$VERSION</version> <!-- replace with latest version -->
<type>pom</type>
</dependency>
License
Code released under the MIT license