Jackson/Gson将JavaFX Properties序列化和反序列化为json [英] Jackson/Gson serialize and deserialize JavaFX Properties to json

查看:195
本文介绍了Jackson/Gson将JavaFX Properties序列化和反序列化为json的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在DAO类中添加了BooleanProperty,该类将被序列化为JSON并发送到服务器以保存在MySQL数据库中.我使用BooleanProperty的原因是因为我想对JavaFX桌面应用程序中的"isActive"字段使用数据绑定.

I have added a BooleanProperty into a DAO class which is to be serialized into JSON and sent across to a server to be saved in a MySQL database. The reason I am using the BooleanProperty is because I want to use data binding for the 'isActive' field in my JavaFX desktop application.

要序列化的类:

package com.example.myapplication

import lombok.Data;
import javafx.beans.property.BooleanProperty;
import javafx.beans.property.SimpleBooleanProperty;

@Data
public class MyDAO {
    private int id;
    private String firstName;
    private String lastname;
    private final BooleanProperty isActive = new SimpleBooleanProperty();
}

我正在使用Gson序列化为JSON:

I am serializing to JSON using Gson:

StringEntity entity = new StringEntity(new Gson().toJson(myDTO), "UTF-8");

当将其序列化为JSON时,它看起来像这样:

When this gets serialized to JSON, it looks like this:

{
   "id":0,
   "first_name":"Joe",
   "last_name":"Bloggs",
   "is_active":{
      "name":"",
      "value":false,
      "valid":true

   }
}

这在反序列化(使用Jackson)时在服务器上引起问题,因为服务器期望布尔值与将保存在数据库中的值相对应.有没有办法从BooleanProperty中反序列化真/假值?

This is causing problems on the server when deserializing (using Jackson), as the server expects a boolean value to correspond with what will be saved in the database. Is there a way to just get the true/false value deserialized from the BooleanProperty?

这是我想在服务器中看到的:

This is what I would like to see in the server:

{
   "id":0,
   "first_name":"Joe",
   "last_name":"Bloggs",
   "is_active": false,
}

推荐答案

您的客户端应用程序使用GsonPOJO序列化为JSON,而服务器应用程序使用JacksonJSON反序列化回POJO .在这两种情况下,这两个库默认情况下都将提供的类作为常规的POJO -s序列化.在POJO中,使用JavaFX Properties,它们是具有附加功能的值的包装.当您将POJO序列化为JSON时,有时您需要隐藏POJO的内部实现,这就是为什么您应该实现自定义序列化程序或使用

Your client app uses Gson to serialise POJO to JSON and server app uses Jackson to deserialise JSON back to POJO. In both cases these two libraries by default serialise provided classes as regular POJO-s. In your POJO you use JavaFX Properties which are wrappers for values with extra functionality. When you serialise POJO to JSON sometimes you need to hide internal implementation of POJO and this is why you should implement custom serialiser or use FX Gson library.

要编写自定义序列化程序,您需要实现com.google.gson.JsonSerializer接口.在下面,您可以找到一个可能看起来很热的示例:

To write custom serialiser you need to implement com.google.gson.JsonSerializer interface. Below you can find an example hot it could look like:

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonElement;
import com.google.gson.JsonPrimitive;
import com.google.gson.JsonSerializationContext;
import com.google.gson.JsonSerializer;
import javafx.beans.property.BooleanProperty;
import javafx.beans.property.SimpleBooleanProperty;
import lombok.Data;

import java.lang.reflect.Type;

public class GsonApp {

    public static void main(String[] args) {
        MyDAO myDAO = new MyDAO();
        myDAO.setId(1);
        myDAO.setFirstName("Vika");
        myDAO.setLastname("Zet");
        myDAO.getIsActive().set(true);

        Gson gson = new GsonBuilder()
                .registerTypeAdapter(BooleanProperty.class, new BooleanPropertySerializer())
                .setPrettyPrinting().create();
        System.out.println(gson.toJson(myDAO));
    }

}

class BooleanPropertySerializer implements JsonSerializer<BooleanProperty> {
    @Override
    public JsonElement serialize(BooleanProperty src, Type typeOfSrc, JsonSerializationContext context) {
        return new JsonPrimitive(src.getValue());
    }
}

@Data
class MyDAO {
    private int id;
    private String firstName;
    private String lastname;
    private final BooleanProperty isActive = new SimpleBooleanProperty();
}

上面的代码显示:

{
  "id": 1,
  "firstName": "Vika",
  "lastname": "Zet",
  "isActive": true
}

2. FX Gson

如果您使用javafx.beans.property.*包中的许多类型,最好使用FX Gson库,该库为大多数使用的类型实现自定义序列化程序.您只需要添加一个依赖性 Maven POM文件:

2. FX Gson

In case you use many types from javafx.beans.property.* package good idea would be to use FX Gson library which implement custom serialisers for most used types. You just need to add one extra dependency to your Maven POM file:

<dependency>
    <groupId>org.hildan.fxgson</groupId>
    <artifactId>fx-gson</artifactId>
    <version>3.1.2</version>
</dependency>

示例用法:

import com.google.gson.Gson;
import javafx.beans.property.BooleanProperty;
import javafx.beans.property.SimpleBooleanProperty;
import lombok.Data;
import org.hildan.fxgson.FxGson;

public class GsonApp {

    public static void main(String[] args) {
        MyDAO myDAO = new MyDAO();
        myDAO.setId(1);
        myDAO.setFirstName("Vika");
        myDAO.setLastname("Zet");
        myDAO.getIsActive().set(true);

        Gson gson = FxGson.coreBuilder().setPrettyPrinting().create();
        System.out.println(gson.toJson(myDAO));
    }

}

上面的代码显示:

{
  "id": 1,
  "firstName": "Vika",
  "lastname": "Zet",
  "isActive": true
}

这篇关于Jackson/Gson将JavaFX Properties序列化和反序列化为json的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆