Jackson:反序列化使用findTypedValueSerializer()序列化的Object

时间:2018-03-13 15:38:37

标签: jackson jackson2 jackson-databind

在我的JsonSerializer中,我有这段代码:

// Variable needs to be of type Object
Object object = new Person("John Doe");
...
JsonSerializer<Object> serializer = serializers.findTypedValueSerializer(object.getClass(), true, null);
serializer.serialize(object, generator, serializers);

很好地序列化到这个JSON:

{"@class":"org.foo.Person","name":"John Doe"}

因为我的课程注释如下:

@JsonTypeInfo(use=JsonTypeInfo.Id.CLASS, include=JsonTypeInfo.As.PROPERTY, property="@class")
public class Person {
    private String name;
    ...
}

如何使用JsonDeserializer反序列化该JSON文本?

此代码抛出

空指针异常

JsonDeserializer<Object> deserializer = context.findNonContextualValueDeserializer(context.constructType(Object.class));
TypeDeserializer typeDeserializer = context.getFactory().findTypeDeserializer(context.getConfig(), context.constructType(Object.class));
Object object = deserializer.deserializeWithType(parser, context, typeDeserializer);

0 个答案:

没有答案