オブジェクトはPropertyChangeSupport
に実装されていますが、デフォルトのコンストラクタでnew PropertyChangeSupport(this)
という値を自分自身で初期化しても、変数propertyChangeSupport
はnull
になります。 Gsonを使用して正しく初期化または非直列化するにはどうすればよいですか?JSON(Gson)からデシリアライズするときにPropertyChangeSupportを初期化する方法は?
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);
を入れてみましおよびnoのいずれかに行くされています
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;
}
};
どこで通知方法を起動しますか? –
また、オブジェクトにリスナーを追加するにはどうすればいいですか?私はちょうど** **どのプロパティがプロパティリスナーがリッスンすることになるのか明確ではない - このクラスの*束縛されたプロパティであると思われるものは何ですか?それが 'id'プロパティの場合、setterメソッドはサポートオブジェクトの通知メソッドを呼び出す必要があります。 –
私は自分のjsonオブジェクトを逆シリアル化した後に 'PropertyChangeListener'を追加します。基本的にサーバーからjson文字列を取得し、そのデータを' Blah'オブジェクトに逆シリアル化してから 'PropertyChangeListener'を追加しようとします。 。問題は変数がjson文字列の一部ではないので変数がnullであることですが、私は醜いハックを避けようとしています... – codenamezero