Handling mission classes in json deserialization using jackson

Question

Using Jackson, I am serializing an object to JSON. Inside the JSON-Serialized parent object is an object that is some subtype of an abstract class.

@JsonTypeInfo(
        use = JsonTypeInfo.Id.CLASS,
        include = JsonTypeInfo.As.PROPERTY,
        property = "@class")

public abstract class AbstractSupertype{...}

public class ActualSerializedSubObject extends AbstractSypertype{...}

My object graph would then look something like

Parent object
|-> ChildObject
   |->ActualSerializedSubObject (Or some other subtype of AbstractSupertype)

The problem is that for various OSGi-related reasons, the specific subtype and object was serialized with, is not always available when it is later deserialized to an object. When that is the case Jackson rightly throws an exception com.fasterxml.jackson.databind.JsonMappingException.

What i would like, is that for this particular field, when the class in not found, an exception is not thrown, but the field is instead set to null, and the parsing is allowed to continue. Any remaining json for that object should be discarded, and replaced with a null.

Parent object
|-> ChildObject
   |->null (No exception is thrown, the object is set set null)

Is this possible, and how?


Show source
| java   | json   | jackson   2017-01-03 11:01 1 Answers

Answers to Handling mission classes in json deserialization using jackson ( 1 )

  1. 2017-01-03 11:01

    I think you want to use this configuration option:

    mapper.configure(DeserializationFeature.FAIL_ON_INVALID_SUBTYPE, false);
    

    It will replace unknown sub-type to null.

Leave a reply to - Handling mission classes in json deserialization using jackson

◀ Go back