<< Source Code>>
public enum State {
AA, BB
}
public static class CC {
Set<State> s = new HashSet<State>();
public Set<State> getS() {
return s;
}
public void setS(Set<State> s) {
this.s = s;
}
};
public static void main(String[] args) {
try {
String test = "";
JSONUtils.getMorpherRegistry().registerMorpher(new EnumMorpher(State.class));
CC cc = new CC();
cc.getS().add(State.AA);
cc.getS().add(State.BB);
test = net.sf.json.JSONObject.fromObject(cc).toString();
System.out.println(test);
cc = (CC)JSONObject.toBean(JSONObject.fromObject(test), CC.class);
for(Object s : cc.getS()) {
System.out.println(s.getClass());
}
System.out.println(test);
} catch (Exception e) {
e.printStackTrace();
}
}
<<Result>>
{"s":["AA","BB"]}
class java.lang.String
class java.lang.String
{"s":["AA","BB"]}
State.AA, State.BB enum is serialized as String object (not State object).
This require some tho, as the underlying problem is generics (yet again).
Enums must be serialized as Strings otherwise serialization will fail, the problem lies in realizing enums back from their literal representation.