Note that there are some explanatory texts on larger screens.

plurals
  1. PONested Classes that need custom Jackson deserializer
    text
    copied!<p>I have a light wrapper class around a complex class for which I needed to write a custom Jackson JSON deserializer. The wrapper class is simple and only contains a String, a Date, and my complex object as properties. Does Jackson automatically apply a simple deserializer to the wrapper and my custom deserializer to my complex object? The custom deserializer works by itself. But when I try to serialize the wrapper Jackson throws a Nullpointer Exception. I must be missing something conceptual. Do I have to register another serializer with my module in addition to my custom deserializer?</p> <pre><code>java.lang.NullPointerException at org.codehaus.jackson.impl.ReaderBasedParser._skipWSOrEnd(ReaderBasedParser.java:1477) at org.codehaus.jackson.impl.ReaderBasedParser.nextToken(ReaderBasedParser.java:368) at org.codehaus.jackson.map.deser.BeanDeserializer.deserializeFromObject(BeanDeserializer.java:690) at org.codehaus.jackson.map.deser.BeanDeserializer.deserialize(BeanDeserializer.java:580) at org.codehaus.jackson.map.ObjectMapper._readMapAndClose(ObjectMapper.java:2732) at org.codehaus.jackson.map.ObjectMapper.readValue(ObjectMapper.java:1863) at com.newoak.noc.curve.model.tests.ModelParamsTest.deserializeGraph(ModelParamsTest.java:100) at com.newoak.noc.curve.model.tests.ModelParamsTest.testSerializationDeserialization(ModelParamsTest.java:113) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:601) </code></pre> <p><strong>Trying to deserialize</strong></p> <pre><code>public ModelParamGraph deserializeGraph(String json) throws JsonGenerationException, JsonMappingException, IOException { ObjectMapper mapper = new ObjectMapper(); SimpleModule testModule = new SimpleModule("MyModule", new Version(1, 0, 0, null)); testModule.addSerializer(new SpaceJsonSerializer()); testModule.addDeserializer(Space.class, new SpaceJsonDeserializer()); mapper.registerModule(testModule); ModelParamGraph space = mapper.readValue(json, ModelParamGraph.class); return space; } </code></pre> <p><strong>Wrapper</strong></p> <pre><code>public class ModelParamGraph { public String source; public Date date; @JsonSerialize(using=SpaceJsonSerializer.class) @JsonDeserialize(using=SpaceJsonDeserializer.class) public Space&lt;TModelParam&gt; paramSpace; public ModelParamGraph() { } public ModelParamGraph(String source, Date date) { setSource(source); setDate(date); setParamSpace(new Space&lt;TModelParam&gt;()); } //getters and setters } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload