gpt4 book ai didi

java - Jackson/Gson 将 JavaFX Properties 序列化和反序列化为 json

转载 作者:行者123 更新时间:2023-12-03 08:48:24 27 4
gpt4 key购买 nike

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

要序列化的类:

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:

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

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

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

}
}

这会在反序列化(使用 Jackson)时导致服务器出现问题,因为服务器期望 boolean 值与数据库中保存的值相对应。有没有办法从 BooleanProperty 中反序列化真/假值?

这是我希望在服务器中看到的内容:

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

最佳答案

您的客户端应用程序使用GsonPOJO序列化为JSON,服务器应用程序使用Jackson反序列化 >JSON 返回到 POJO。在这两种情况下,这两个库默认将提供的类序列化为常规 POJO-s。在您的 POJO 中,您使用 JavaFX Properties,它们是具有额外功能的值的包装器。当您将 POJO 序列化为 JSON 时,有时您需要隐藏 POJO 的内部实现,这就是为什么您应该实现自定义序列化程序或使用 FX Gson图书馆。

1。自定义序列化器

要编写自定义序列化程序,您需要实现 com.google.gson.JsonSerializer 接口(interface)。您可以在下面找到一个示例,它可能如下所示:

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 库,它为大多数使用的类型实现自定义序列化器。您只需额外添加一个 dependency到您的 Maven POM 文件:

<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
}

关于java - Jackson/Gson 将 JavaFX Properties 序列化和反序列化为 json,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60455801/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com