从 JSON (Gson) 反序列化时如何初始化 PropertyChangeSupport?
How to initialize PropertyChangeSupport when deserializing from JSON (Gson)?
我的对象实现了 PropertyChangeSupport
,但是当我从 json 字符串反序列化时,变量 propertyChangeSupport
将是 null
,尽管我初始化了值我自己在默认构造函数中有一个 new PropertyChangeSupport(this)
。如何使用 Gson 正确初始化或反序列化它?
假设我有这个对象:
public class Blah implements BlahInterface {
private PropertyChangeSupport propertyChangeSupport;
protected int id;
protected BlahType type;
public Blah() {
propertyChangeSupport = new PropertyChangeSupport(this);
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public BlahType getType() {
return type;
}
public void setType(BlahType type) {
this.type = type;
}
public void addPropertyChangeListener(PropertyChangeListener listener) {
this.propertyChangeSupport.addPropertyChangeListener(listener);
}
public PropertyChangeListener[] getPropertyChangeListeners() {
return this.propertyChangeSupport.getPropertyChangeListeners();
}
}
我也试过直接把new PropertyChangeSupport(this);
放在开头,也不行。我想避免手动创建 initializePropertyChangeSupport()
之类的函数,然后在反序列化后手动调用它,因为这有点难看。
我想做什么:
JsonArray ja = json.get("blahs").getAsJsonArray();
ja.forEach(item -> {
Blah blah = BlahInterface.Parse(item.toString());
// But here I can't addPropertyChangeListener because propertyChangeSupport is null
// vvvvvvvvvvvv
blah.addPropertyChangeListener(new PropertyChangeListener() {
@Override
public void propertyChange(PropertyChangeEvent evt) {
BlahState state = (BlahState) evt.getNewValue();
Logger.debug("Property had been updated, " + state.toString());
}
});
});
这是我的 json 解析函数:
@SuppressWarnings("unchecked")
public static <T extends Blah> T Parse(String json) {
Gson gson = new Gson();
Blah t = new Blah(gson.fromJson(json, Blah.class));
switch (t.getType()) {
case blahone:
return (T) gson.fromJson(json, BlahOne.class);
default:
return (T) t;
}
};
这个问题的解决方案是在我的对象中实现 InstanceCreator<T>
。因此,当 Gson 尝试反序列化对象时,它会调用 createInstance
函数,该函数又 returns 一个适当的对象,并初始化了 PropertyChangeSupport
变量。示例代码如下:
public class Blah implements InstanceCreator<Blah> {
private final transient PropertyChangeSupport pcs = new PropertyChangeSupport(this);
...
public void addPropertyChangeListener(PropertyChangeListener listener) {
this.pcs.addPropertyChangeListener(listener);
}
public void removePropertyChangeListener(PropertyChangeListener listener) {
this.pcs.removePropertyChangeListener(listener);
}
@Override
public Blah createInstance(Type type) {
return new Blah();
}
}
注意:pcs
中有 transient 关键字,这样 Gson 会在序列化期间跳过它,否则 Gson 会抛出异常。
我的对象实现了 PropertyChangeSupport
,但是当我从 json 字符串反序列化时,变量 propertyChangeSupport
将是 null
,尽管我初始化了值我自己在默认构造函数中有一个 new PropertyChangeSupport(this)
。如何使用 Gson 正确初始化或反序列化它?
假设我有这个对象:
public class Blah implements BlahInterface {
private PropertyChangeSupport propertyChangeSupport;
protected int id;
protected BlahType type;
public Blah() {
propertyChangeSupport = new PropertyChangeSupport(this);
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public BlahType getType() {
return type;
}
public void setType(BlahType type) {
this.type = type;
}
public void addPropertyChangeListener(PropertyChangeListener listener) {
this.propertyChangeSupport.addPropertyChangeListener(listener);
}
public PropertyChangeListener[] getPropertyChangeListeners() {
return this.propertyChangeSupport.getPropertyChangeListeners();
}
}
我也试过直接把new PropertyChangeSupport(this);
放在开头,也不行。我想避免手动创建 initializePropertyChangeSupport()
之类的函数,然后在反序列化后手动调用它,因为这有点难看。
我想做什么:
JsonArray ja = json.get("blahs").getAsJsonArray();
ja.forEach(item -> {
Blah blah = BlahInterface.Parse(item.toString());
// But here I can't addPropertyChangeListener because propertyChangeSupport is null
// vvvvvvvvvvvv
blah.addPropertyChangeListener(new PropertyChangeListener() {
@Override
public void propertyChange(PropertyChangeEvent evt) {
BlahState state = (BlahState) evt.getNewValue();
Logger.debug("Property had been updated, " + state.toString());
}
});
});
这是我的 json 解析函数:
@SuppressWarnings("unchecked")
public static <T extends Blah> T Parse(String json) {
Gson gson = new Gson();
Blah t = new Blah(gson.fromJson(json, Blah.class));
switch (t.getType()) {
case blahone:
return (T) gson.fromJson(json, BlahOne.class);
default:
return (T) t;
}
};
这个问题的解决方案是在我的对象中实现 InstanceCreator<T>
。因此,当 Gson 尝试反序列化对象时,它会调用 createInstance
函数,该函数又 returns 一个适当的对象,并初始化了 PropertyChangeSupport
变量。示例代码如下:
public class Blah implements InstanceCreator<Blah> {
private final transient PropertyChangeSupport pcs = new PropertyChangeSupport(this);
...
public void addPropertyChangeListener(PropertyChangeListener listener) {
this.pcs.addPropertyChangeListener(listener);
}
public void removePropertyChangeListener(PropertyChangeListener listener) {
this.pcs.removePropertyChangeListener(listener);
}
@Override
public Blah createInstance(Type type) {
return new Blah();
}
}
注意:pcs
中有 transient 关键字,这样 Gson 会在序列化期间跳过它,否则 Gson 会抛出异常。