All Projects → joffrey-bion → fx-gson

joffrey-bion / fx-gson

Licence: MIT license
A set of type adapters for Google Gson to make JavaFX properties serialization more natural

Programming Languages

java
68154 projects - #9 most used programming language
kotlin
9241 projects

Projects that are alternatives of or similar to fx-gson

RxjavaSamples
This repo is a container for some samples using RXJAVA2 samples
Stars: ✭ 12 (-77.36%)
Mutual labels:  gson
chorus
📝 The first editor for Spigot configurations.
Stars: ✭ 133 (+150.94%)
Mutual labels:  javafx
OmniGraph
Desktop application for creating graphs and algorithm visualisation
Stars: ✭ 27 (-49.06%)
Mutual labels:  javafx
Azkar-App
Desktop Application 💻 for Calculating Muslim prayer times 🕌 , Morning and Nights Azkar 🤲 with notification for random Azkar that pops-up in specific time.
Stars: ✭ 64 (+20.75%)
Mutual labels:  javafx
moshi-gson-interop
An interop tool for safely mixing Moshi and Gson models in JSON serialization.
Stars: ✭ 39 (-26.42%)
Mutual labels:  gson
GNCarousel
Carousel based on web design
Stars: ✭ 19 (-64.15%)
Mutual labels:  javafx
wayland-javafx
wayland backend for javafx
Stars: ✭ 20 (-62.26%)
Mutual labels:  javafx
imagelab
ImageLab is a standalone tool which supports anyone to get started with image processing related concepts and techniques in an interactive, less logical way.
Stars: ✭ 28 (-47.17%)
Mutual labels:  javafx
vic2 economy analyzer
Victoria 2 savegame economy analyzer, updated version
Stars: ✭ 44 (-16.98%)
Mutual labels:  javafx
webfx
A JavaFX application transpiler. Write your Web Application in JavaFX and WebFX will transpile it in pure JS.
Stars: ✭ 210 (+296.23%)
Mutual labels:  javafx
JFXC
Jonato JavaFX Controls - More Power for your JavaFX Gui
Stars: ✭ 42 (-20.75%)
Mutual labels:  javafx
wt4
Work tracker for JIRA
Stars: ✭ 26 (-50.94%)
Mutual labels:  javafx
WellBehavedFX
Composable event handlers and skin scaffolding for JavaFX controls.
Stars: ✭ 52 (-1.89%)
Mutual labels:  javafx
YuMusic
A Music Player Build with JavaFX WebView, iView,RequireJS
Stars: ✭ 17 (-67.92%)
Mutual labels:  javafx
jfx-asynctask
This project was created to simplify how to handle Thread tasks in Javafx, and it is based on the same idea of AsyncTask from Android.
Stars: ✭ 33 (-37.74%)
Mutual labels:  javafx
sliding-puzzle
Sliding puzzle game implemented in Scala / Scala.js / JavaFX
Stars: ✭ 25 (-52.83%)
Mutual labels:  javafx
sudokufx
AR Sudoku grabber and solver using JavaCV, JavaFX and Scala
Stars: ✭ 64 (+20.75%)
Mutual labels:  javafx
HTTP-Wrapper
A simple http wrapper
Stars: ✭ 13 (-75.47%)
Mutual labels:  gson
store-pos
It is java accounting software basically developed using javafx which has various modules like purchase, sales, receipts, payments, and journals.
Stars: ✭ 84 (+58.49%)
Mutual labels:  javafx
haxe-javafx-sample
Just a sample to start with Haxe + JavaFX
Stars: ✭ 12 (-77.36%)
Mutual labels:  javafx

FX Gson

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

There are different JRE requirements depending on the version of FX Gson:

FX Gson version JRE requirement
3.x.x 8
4.x.x 9+ (JPMS compatible)
5.x.x 8+ (JPMS compatible)

Starting from FX Gson 4.0.0, the library has a module-info.java for compatibility with Jigsaw (Java 9+).

Starting from FX Gson 5.0.0, the library still has module-info.java compiled with Java 9, but all other class files have bytecode level 8, so it can still be used in Java 8. Thanks to @Glavo for the contribution!

Setup - adding the dependency

Manual download

You may manually download the JAR from Maven Central, although I recommend using a build tool such as Gradle.

Gradle

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

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].