需要自定义Jackson反序列化器的嵌套类

时间:2013-12-11 21:53:50

标签: java json spring serialization jackson

我有一个复杂类的轻包装类,我需要编写一个自定义的Jackson JSON解串器。包装器类很简单,只包含一个String,一个Date和我的复杂对象作为属性。杰克逊会自动将一个简单的反序列化器应用于包装器和我的自定义反序列化器到我的复杂对象吗?自定义反序列化器本身就可以工作。但是当我尝试序列化封装时,杰克逊抛出了一个Nullpointer异常。我必须遗漏一些概念性的东西。除了我的自定义反序列化器之外,我还需要在模块中注册另一个串行器吗?

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)

尝试反序列化

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;
    }

包装

public class ModelParamGraph
{
    public String source;

    public Date date;

    @JsonSerialize(using=SpaceJsonSerializer.class)
    @JsonDeserialize(using=SpaceJsonDeserializer.class)
    public Space<TModelParam> paramSpace;

    public ModelParamGraph()
    {
    }

    public ModelParamGraph(String source, Date date)
    {
        setSource(source);
        setDate(date);

        setParamSpace(new Space<TModelParam>());
    }
    //getters and setters
}

1 个答案:

答案 0 :(得分:1)

如果你可以修改被包装的类,那么你可以使用Jackson的@JsonView注释,这里是full tutorial

步骤1:创建如下界面:

public class MyJsonViews {
    public static class Small { }
    public static class Medium extends Small { }
    public static class Large extends Medium { }
}

第2步:在POJO中注释属性(或方法):

public class Wrapper {
    @JsonView(MyJsonViews.Small.class)
    private String name;

    @JsonView(MyJsonViews.Medium.class)
    private Wrapped wrapped;
    // getters and setters
}

public class Wrapped {
   @JsonView(MyJsonViews.Small.class)
   private String someField;

   @JsonView(MyJsonViews.Medium.class)
   private String anotherField;
  // getters and setters
}

第3步:使用新视图序列化:

ObjectMapper objectMapper = new ObjectMapper();
// important: this excludes all fields without @JsonView from being serialized
objectMapper.configure(MapperFeature.DEFAULT_VIEW_INCLUSION, false);
String json = objectMapper.writerWithView(SMyJsonViews.mall.class).writeValueAsString(wrapper);

您的JSON现在只包含使用@JsonView(MyJsonViews.Small.class)注释的字段。

如果您无法修改被包装的类,那么您可以使用过滤方法,描述为here