GithubHelp home page GithubHelp logo

andro-s / fx-gson Goto Github PK

View Code? Open in Web Editor NEW

This project forked from joffrey-bion/fx-gson

0.0 2.0 0.0 107 KB

A Gson extension to serialize JavaFX properties as their values, and deserialize values into properties.

License: MIT License

Java 100.00%

fx-gson's Introduction

FX Gson

Bintray Travis Build Dependency Status GitHub license

A set of type adapters for Google Gson to serialize JavaFX properties as their values, and deserialize values into properties.

Why FX Gson?

In JavaFX, POJOs usually contain Property objects instead of primitives. When serialized with Gson, we 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 / prop getters
}

Here is how it is serialized:

With Gson With FxGson
{
    "firstName": {
        "name": "",
        "value": "Hans",
        "valid": true,
        "helper": {
            "observable": {}
        }
    },
    "lastName": {
        "name": "",
        "value": "Muster",
        "valid": true,
        "helper": {
            "observable": {}
        }
    }
}
{
    "firstName": "Hans",
    "lastName": "Muster"
}

Convincing, eh?

Usage

The simple way

You can use the built-in factory methods directly:

// to handle only Properties and Observable collections
Gson fxGson = FxGson.create();

// to also handle the Color & Font classes
Gson fxGsonWithExtras = FxGson.createWithExtras();

FxGson can also return a builder so that you can add your own configuration to it:

Gson fxGson = FxGson.coreBuilder()
                    .registerTypeAdapterFactory(new MyFactory())
                    .disableHtmlEscaping()
                    .create();

Gson fxGsonWithExtras = FxGson.fullBuilder()
                              .registerTypeAdapter(Pattern.class, new PatternSerializer())
                              .setPrettyPrinting()
                              .create();

You can find more info on the built-in "core" and "full" builder in the Wiki.

Configuring an existing builder to handle JavaFX properties

Sometimes you don't control the creation of the GsonBuilder you are using, because you take it from some library or some other piece of code. In this case, use the provided helper methods to add FxGson configuration to an existing GsonBuilder:

GsonBuilder builder = MyLib.getBuilder();
Gson gson = FxGson.addFxSupport(builder).create();

Going full control

You will find more customization info in this dedicated wiki page.

Add the dependency

In Gradle

repositories {
    jcenter()
}

dependencies {
    compile 'org.hildan.fxgson:fx-gson:2.0.0'
}

In Maven

<dependency>
  <groupId>org.hildan.fxgson</groupId>
  <artifactId>fx-gson</artifactId>
  <version>2.0.0</version>
  <type>pom</type>
</dependency>

Note: the artifact is on Bintray JCenter, not in Maven Central, so make sure you point your maven to JCenter by adding the repo to your settings.xml using the URL http://jcenter.bintray.com.

You can find a complete XML example in the "Set me up!" blue rectangle on the JCenter home page.

License

Code released under the MIT license

fx-gson's People

Contributors

joffrey-bion avatar

Watchers

James Cloos avatar Andrew Wang C avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.