Spring中JSON反序列化期间的NullPointerException

时间:2013-12-11 16:54:57

标签: java json spring serialization jackson

我有一个Space对象,我已经编写了一个自定义序列化器和反序列化器。现在我已将此对象包装在一个简单的对象中以用于某些上下文。我想要做的是序列化/反序列化这个包装器。

My Space对象序列化/反序列化很好。我在尝试反序列化时遇到以下异常:

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 implements Serializable
{

private static final long serialVersionUID = 6123736933251596579L;

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

public String source;

public Date date;

private static Log log_ = LogFactory.getLog(ModelParamGraph.class);

public ModelParamGraph(){}

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

    log_.debug("Creating ModelParam Graph");
    setParamSpace(new Space<TModelParam>());
}

public Space<TModelParam> getParamSpace()
{
    return paramSpace;
}

public void setParamSpace(Space<TModelParam> paramSpace)
{
    this.paramSpace = paramSpace;
}

public void addParameter(TModelParam param, TModelParam parent)
{
    if (paramSpace.getRootData() == null)
    {
        paramSpace.addData(param, true);
    }
    else 
    {
        paramSpace.addData(param);
    }

    if (parent != null)
    {
        paramSpace.link(parent, param, "parent");
    }
}

public String getSource()
{
    return source;
}

public void setSource(String source)
{
    this.source = source;
}

public Date getDate()
{
    return date;
}

public void setDate(Date date)
{
    this.date = date;
}

}

任何帮助将不胜感激!谢谢!

1 个答案:

答案 0 :(得分:0)

无法解决此问题。从jackson中抛出NullPointer异常。听起来这可能是他们最终的错误。