使用 GSON 序列化 JavaFX 模型
Serialize JavaFX model with GSON
我目前正在学习一个教程来帮助我了解 JavaFX 的工作原理,并且在教程中他们正在构建一个小应用程序来管理人们的信息。本教程还使用 XML 作为 loading/saving,但我不想使用 XML,而是想使用 JSON。我有一个使用 StringProperty
、IntegerProperty
和 ObjectProperty
的 Person
模型。我的问题是我不确定加载和保存它的最佳方式是什么而不保存不必要的字段并且加载时 Gson 不会抛出错误。
人
import java.time.LocalDate;
import javafx.beans.property.IntegerProperty;
import javafx.beans.property.ObjectProperty;
import javafx.beans.property.SimpleIntegerProperty;
import javafx.beans.property.SimpleObjectProperty;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;
/**
* Model class for a Person.
*
* @author Marco Jakob
*/
public class Person {
private final StringProperty firstName;
private final StringProperty lastName;
private final StringProperty street;
private final IntegerProperty postalCode;
private final StringProperty city;
private final ObjectProperty<LocalDate> birthday;
/**
* Default constructor.
*/
public Person() {
this(null, null);
}
/**
* Constructor with some initial data.
*
* @param firstName
* @param lastName
*/
public Person(String firstName, String lastName) {
this.firstName = new SimpleStringProperty(firstName);
this.lastName = new SimpleStringProperty(lastName);
// Some initial dummy data, just for convenient testing.
this.street = new SimpleStringProperty("some street");
this.postalCode = new SimpleIntegerProperty(1234);
this.city = new SimpleStringProperty("some city");
this.birthday = new SimpleObjectProperty<LocalDate>(LocalDate.of(1999, 2, 21));
}
public String getFirstName() {
return firstName.get();
}
public void setFirstName(String firstName) {
this.firstName.set(firstName);
}
public StringProperty firstNameProperty() {
return firstName;
}
public String getLastName() {
return lastName.get();
}
public void setLastName(String lastName) {
this.lastName.set(lastName);
}
public StringProperty lastNameProperty() {
return lastName;
}
public String getStreet() {
return street.get();
}
public void setStreet(String street) {
this.street.set(street);
}
public StringProperty streetProperty() {
return street;
}
public int getPostalCode() {
return postalCode.get();
}
public void setPostalCode(int postalCode) {
this.postalCode.set(postalCode);
}
public IntegerProperty postalCodeProperty() {
return postalCode;
}
public String getCity() {
return city.get();
}
public void setCity(String city) {
this.city.set(city);
}
public StringProperty cityProperty() {
return city;
}
public LocalDate getBirthday() {
return birthday.get();
}
public void setBirthday(LocalDate birthday) {
this.birthday.set(birthday);
}
public ObjectProperty<LocalDate> birthdayProperty() {
return birthday;
}
}
保存其中 personData
是 Person
的 ObservableList
try (Writer writer = new FileWriter(file)) {
new GsonBuilder().setPrettyPrinting().disableHtmlEscaping().create().toJson(personData, writer);
}
这种保存方式当前会生成一个包含许多不必要字段的保存,例如 name
、value
等,而实际上它可能是 "firstName": "Hans"
[{
"firstName": {
"name": "",
"value": "Hans",
"valid": true,
"helper": {
"observable": {}
}
},
"lastName": {
"name": "",
"value": "Muster",
"valid": true,
"helper": {
"observable": {}
}
},
"street": {
"name": "",
"value": "some street",
"valid": true
},
"postalCode": {
"name": "",
"value": 1234,
"valid": true
},
"city": {
"name": "",
"value": "some city",
"valid": true
},
"birthday": {}
}]
现在,即使尝试使用 Gson 加载上面的字符串也会产生错误,Failed to invoke public javafx.beans.property.StringProperty() with no args
。
加载程序
Person[] persons;
try (Reader reader = new FileReader(file)) {
persons = gson.fromJson(reader, Person[].class);
}
personData.clear();
personData.addAll(persons);
我在谷歌上搜索过是否可以将 getter 和 setter 与 Gson 一起使用,但似乎不太可能,所以我不知道该怎么做。
我在 GSON 和 JavaFX 中遇到了同样的问题 属性 Model.And 我已经使用 LinkedHashMap 解决了它,如下所示:-
在你的模型中 class :-
public Person(LinkedHashMap<String, Object> personData) {
this.firstName = new SimpleStringProperty((String) personData.get("firstName"));
this.lastName = new SimpleStringProperty((String) personData.get("lastName"));
this.street = new SimpleStringProperty((String) personData.get("street"));
this.postalCode = new SimpleIntegerProperty(((Double) personData.get("postalCode")).intValue());
this.city = new SimpleStringProperty((String) personData.get("city"));
String birthdayString = (String) personData.get("birthday");
LocalDate date = LocalDate.parse(birthdayString ,DateTimeFormatter.ofPattern("yyy, mm, dd"));
this.birthday = new SimpleObjectProperty<LocalDate>(date);
}
public LinkedHashMap<String, Object> getPersonData() {
LinkedHashMap<String, Object> personData = new LinkedHashMap<>();
personData.put("firstName", firstName.getValue());
personData.put("lastName", lastName.getValue());
personData.put("street", street.getValue());
personData.put("postalCode", postalCode.getValue());
personData.put("city", city.getValue());
personData.put("birthday", birthday.getValue());
return personData;
}
然后在加载程序中:-
Gson gson = new Gson();
List<LinkedHashMap<String, Object>> persons = new Gson().fromJson(jsonData, new TypeToken<List<LinkedHashMap<String, Object>>>() {}.getType());
for(LinkedHashMap<String, Object> personData : persons) {
Person person = new Person(personData);
}
并转换为 Json :-
LinkedHashMap<String, Object> personData = person.getPersonData();
String jsonData = new Gson().toJson(personData);
请注意,GSON 将 int 值映射为 double,因为它更通用,因此您需要先将邮政编码转换为 double,然后从中获取 int 值,请参阅此问题以获取更多信息。
How to prevent Gson from expressing integers as floats
我知道我来晚了一点,但这是为未来的读者准备的。
我遇到了完全相同的问题。我最终写了一堆 Gson TypeAdapter
s,每个 JavaFX 属性 类型一个(还有一些 Color
和 Font
)。
我把它们全部收集在 a lightweight library called FxGson (< 30kB).
现在,只需使用 FxGson 的 GsonBuilder
,JavaFX POJO 将被序列化,就像它们的属性是简单值一样。
在您的示例中使用 Person
class:
Person p = new Person("Hans", "Muster");
Gson gson = FxGson.coreBuilder().setPrettyPrinting().disableHtmlEscaping().create();
System.out.println(gson.toJson(p));
这输出:
{
"firstName": "Hans",
"lastName": "Muster",
"street": "some street",
"postalCode": 1234,
"city": "some city",
"birthday": {
"year": 1999,
"month": 2,
"day": 21
}
}
GSON 是必需的吗?
我和 GSON 有同样的问题并切换到 Jackson。有效:
ObjectMapper mapper = new ObjectMapper();
System.out.println(mapper.writeValueAsString(persons));
我目前正在学习一个教程来帮助我了解 JavaFX 的工作原理,并且在教程中他们正在构建一个小应用程序来管理人们的信息。本教程还使用 XML 作为 loading/saving,但我不想使用 XML,而是想使用 JSON。我有一个使用 StringProperty
、IntegerProperty
和 ObjectProperty
的 Person
模型。我的问题是我不确定加载和保存它的最佳方式是什么而不保存不必要的字段并且加载时 Gson 不会抛出错误。
人
import java.time.LocalDate;
import javafx.beans.property.IntegerProperty;
import javafx.beans.property.ObjectProperty;
import javafx.beans.property.SimpleIntegerProperty;
import javafx.beans.property.SimpleObjectProperty;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;
/**
* Model class for a Person.
*
* @author Marco Jakob
*/
public class Person {
private final StringProperty firstName;
private final StringProperty lastName;
private final StringProperty street;
private final IntegerProperty postalCode;
private final StringProperty city;
private final ObjectProperty<LocalDate> birthday;
/**
* Default constructor.
*/
public Person() {
this(null, null);
}
/**
* Constructor with some initial data.
*
* @param firstName
* @param lastName
*/
public Person(String firstName, String lastName) {
this.firstName = new SimpleStringProperty(firstName);
this.lastName = new SimpleStringProperty(lastName);
// Some initial dummy data, just for convenient testing.
this.street = new SimpleStringProperty("some street");
this.postalCode = new SimpleIntegerProperty(1234);
this.city = new SimpleStringProperty("some city");
this.birthday = new SimpleObjectProperty<LocalDate>(LocalDate.of(1999, 2, 21));
}
public String getFirstName() {
return firstName.get();
}
public void setFirstName(String firstName) {
this.firstName.set(firstName);
}
public StringProperty firstNameProperty() {
return firstName;
}
public String getLastName() {
return lastName.get();
}
public void setLastName(String lastName) {
this.lastName.set(lastName);
}
public StringProperty lastNameProperty() {
return lastName;
}
public String getStreet() {
return street.get();
}
public void setStreet(String street) {
this.street.set(street);
}
public StringProperty streetProperty() {
return street;
}
public int getPostalCode() {
return postalCode.get();
}
public void setPostalCode(int postalCode) {
this.postalCode.set(postalCode);
}
public IntegerProperty postalCodeProperty() {
return postalCode;
}
public String getCity() {
return city.get();
}
public void setCity(String city) {
this.city.set(city);
}
public StringProperty cityProperty() {
return city;
}
public LocalDate getBirthday() {
return birthday.get();
}
public void setBirthday(LocalDate birthday) {
this.birthday.set(birthday);
}
public ObjectProperty<LocalDate> birthdayProperty() {
return birthday;
}
}
保存其中 personData
是 Person
的 ObservableList
try (Writer writer = new FileWriter(file)) {
new GsonBuilder().setPrettyPrinting().disableHtmlEscaping().create().toJson(personData, writer);
}
这种保存方式当前会生成一个包含许多不必要字段的保存,例如 name
、value
等,而实际上它可能是 "firstName": "Hans"
[{
"firstName": {
"name": "",
"value": "Hans",
"valid": true,
"helper": {
"observable": {}
}
},
"lastName": {
"name": "",
"value": "Muster",
"valid": true,
"helper": {
"observable": {}
}
},
"street": {
"name": "",
"value": "some street",
"valid": true
},
"postalCode": {
"name": "",
"value": 1234,
"valid": true
},
"city": {
"name": "",
"value": "some city",
"valid": true
},
"birthday": {}
}]
现在,即使尝试使用 Gson 加载上面的字符串也会产生错误,Failed to invoke public javafx.beans.property.StringProperty() with no args
。
加载程序
Person[] persons;
try (Reader reader = new FileReader(file)) {
persons = gson.fromJson(reader, Person[].class);
}
personData.clear();
personData.addAll(persons);
我在谷歌上搜索过是否可以将 getter 和 setter 与 Gson 一起使用,但似乎不太可能,所以我不知道该怎么做。
我在 GSON 和 JavaFX 中遇到了同样的问题 属性 Model.And 我已经使用 LinkedHashMap 解决了它,如下所示:-
在你的模型中 class :-
public Person(LinkedHashMap<String, Object> personData) {
this.firstName = new SimpleStringProperty((String) personData.get("firstName"));
this.lastName = new SimpleStringProperty((String) personData.get("lastName"));
this.street = new SimpleStringProperty((String) personData.get("street"));
this.postalCode = new SimpleIntegerProperty(((Double) personData.get("postalCode")).intValue());
this.city = new SimpleStringProperty((String) personData.get("city"));
String birthdayString = (String) personData.get("birthday");
LocalDate date = LocalDate.parse(birthdayString ,DateTimeFormatter.ofPattern("yyy, mm, dd"));
this.birthday = new SimpleObjectProperty<LocalDate>(date);
}
public LinkedHashMap<String, Object> getPersonData() {
LinkedHashMap<String, Object> personData = new LinkedHashMap<>();
personData.put("firstName", firstName.getValue());
personData.put("lastName", lastName.getValue());
personData.put("street", street.getValue());
personData.put("postalCode", postalCode.getValue());
personData.put("city", city.getValue());
personData.put("birthday", birthday.getValue());
return personData;
}
然后在加载程序中:-
Gson gson = new Gson();
List<LinkedHashMap<String, Object>> persons = new Gson().fromJson(jsonData, new TypeToken<List<LinkedHashMap<String, Object>>>() {}.getType());
for(LinkedHashMap<String, Object> personData : persons) {
Person person = new Person(personData);
}
并转换为 Json :-
LinkedHashMap<String, Object> personData = person.getPersonData();
String jsonData = new Gson().toJson(personData);
请注意,GSON 将 int 值映射为 double,因为它更通用,因此您需要先将邮政编码转换为 double,然后从中获取 int 值,请参阅此问题以获取更多信息。
How to prevent Gson from expressing integers as floats
我知道我来晚了一点,但这是为未来的读者准备的。
我遇到了完全相同的问题。我最终写了一堆 Gson TypeAdapter
s,每个 JavaFX 属性 类型一个(还有一些 Color
和 Font
)。
我把它们全部收集在 a lightweight library called FxGson (< 30kB).
现在,只需使用 FxGson 的 GsonBuilder
,JavaFX POJO 将被序列化,就像它们的属性是简单值一样。
在您的示例中使用 Person
class:
Person p = new Person("Hans", "Muster");
Gson gson = FxGson.coreBuilder().setPrettyPrinting().disableHtmlEscaping().create();
System.out.println(gson.toJson(p));
这输出:
{
"firstName": "Hans",
"lastName": "Muster",
"street": "some street",
"postalCode": 1234,
"city": "some city",
"birthday": {
"year": 1999,
"month": 2,
"day": 21
}
}
GSON 是必需的吗?
我和 GSON 有同样的问题并切换到 Jackson。有效:
ObjectMapper mapper = new ObjectMapper();
System.out.println(mapper.writeValueAsString(persons));