fx-gson

A set of type adapters for Google Gson to make JavaFX properties serialization more natural.

License

License

Categories

Categories

Gson Data JSON
GroupId

GroupId

org.hildan.fxgson
ArtifactId

ArtifactId

fx-gson
Last Version

Last Version

4.0.1
Release Date

Release Date

Type

Type

jar
Description

Description

fx-gson
A set of type adapters for Google Gson to make JavaFX properties serialization more natural.
Project URL

Project URL

https://github.com/joffrey-bion/fx-gson
Source Code Management

Source Code Management

https://github.com/joffrey-bion/fx-gson

Download fx-gson

How to add to project

<!-- https://jarcasting.com/artifacts/org.hildan.fxgson/fx-gson/ -->
<dependency>
    <groupId>org.hildan.fxgson</groupId>
    <artifactId>fx-gson</artifactId>
    <version>4.0.1</version>
</dependency>
// https://jarcasting.com/artifacts/org.hildan.fxgson/fx-gson/
implementation 'org.hildan.fxgson:fx-gson:4.0.1'
// https://jarcasting.com/artifacts/org.hildan.fxgson/fx-gson/
implementation ("org.hildan.fxgson:fx-gson:4.0.1")
'org.hildan.fxgson:fx-gson:jar:4.0.1'
<dependency org="org.hildan.fxgson" name="fx-gson" rev="4.0.1">
  <artifact name="fx-gson" type="jar" />
</dependency>
@Grapes(
@Grab(group='org.hildan.fxgson', module='fx-gson', version='4.0.1')
)
libraryDependencies += "org.hildan.fxgson" % "fx-gson" % "4.0.1"
[org.hildan.fxgson/fx-gson "4.0.1"]

Dependencies

compile (1)

Group / Artifact Type Version
com.google.code.gson : gson jar 2.8.6

runtime (3)

Group / Artifact Type Version
org.openjfx : javafx-graphics jar 11
org.openjfx : javafx-base jar 11
org.jetbrains : annotations jar 18.0.0

Project Modules

There are no modules declared in this project.

FX Gson

Bintray Download Maven central version Build Status GitHub license

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 yourself

    Gson 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

Versions

Version
4.0.1
4.0.0
3.1.2
3.1.1
3.1.0