PropertyChangeSupport
구현 된 내 개체가 있지만 json 문자열에서 deserialize 할 때 변수 propertyChangeSupport
null
, 기본 생성자 new PropertyChangeSupport(this)
함께 값을 직접 초기화하는 것입니다. Gson을 사용하여이를 어떻게 초기화하거나 deserialize 할 수 있습니까?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);
퍼팅 시도하지 않고 하나, 이동 :
initializePropertyChangeSupport()
과 같은 함수를 피하기 위해 누른 다음 수동으로 deserialization 후 전화 좀 추한. 내가 할 노력하고있어
:
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;
}
};
어디서 알림 방법을 시작합니까? –
또한 오브젝트가 존재하기 전에 리스너를 오브젝트에 어떻게 추가 할 수 있습니까? 정확하게 ** 어떤 속성 ** 속성 변경 청취자가들을 것입니다 -이 클래스의 * bound * 속성 (들)이되어야 할 것인가? 이것이'id' 속성이라면 setter 메소드는 지원 객체의 통지 메소드를 호출해야합니다. –
내 json 객체를 비 직렬화 한 후에 내 'PropertyChangeListener'를 추가합니다. 기본적으로 json 문자열을 서버에서 가져온 다음 해당 데이터로 'Blah' 객체로 비 직렬화 한 다음 내'PropertyChangeListener '를 추가하려고합니다. . 문제는 json 문자열의 일부가 아니기 때문에 변수가 null이라는 것입니다. 그러나 추한 해킹을 피하려고합니다. – codenamezero