i trying serialize class has enum 1 of members. setters , getters enum in class trying serialize not follow classic format. below example of mean
public class myobj { private stateenum state; public string getstate() { // problem getter return state == null ? null : state.name(); } public void setstate(string state) { // problem setter this.state = stateenum.valueof(state); } } public enum stateenum implements cloneable { active("active"), preprocess("preprocessing"), select("selecting"); private final string description; entitystate(string description) { this.description = description; } @override public string tostring() { return "entitystate: " + this.description; } }
currently serialize/deserialize using similar following.
myobj obj = new myobj(); objectmapper omap = new objectmapper(); string payload = omap.writevalueasstring(obj); myobj deserializedobj = omap.readvalue(obj); and error: com.fasterxml.jackson.databind.jsonmappingexception: name null
how can go serializing/deserializing without changing setter , getter (adding @json tags okay)?
the problem has nothing jackson. has setter not handling null correctly, , throwing nullpointerexception.
if had read full stack trace of exception (and posted it, avoid forcing guess problem), have noticed it:
caused by: java.lang.nullpointerexception: name null @ java.lang.enum.valueof(enum.java:236) @ com.foo.stateenum.valueof(stateenum.java:3) @ com.foo.myobj.setstate(myobj.java:15) @ sun.reflect.nativemethodaccessorimpl.invoke0(native method) @ sun.reflect.nativemethodaccessorimpl.invoke(nativemethodaccessorimpl.java:62) @ sun.reflect.delegatingmethodaccessorimpl.invoke(delegatingmethodaccessorimpl.java:43) @ java.lang.reflect.method.invoke(method.java:483) @ com.fasterxml.jackson.databind.deser.impl.methodproperty.set(methodproperty.java:113) replace by
this.state = state == null ? null : stateenum.valueof(state);
Comments
Post a Comment