Jackson deserialize object or array

前端 未结 4 1985
梦如初夏
梦如初夏 2020-12-09 04:14

I have a Jackson Question.

Is there a way to deserialize a property that may have two types, for some objects it appears like this

\"someObj\" : { \"         


        
4条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-09 04:49

    Edit: Since Jackson 2.5.0, you can use DeserializationFeature.ACCEPT_EMPTY_ARRAY_AS_EMPTY_OBJECT to resolve your problem.

    The solution Bruce provides has a few problems/disadvantages:

    • you'll need to duplicate that code for each type you need to deserialize that way
    • ObjectMapper should be reused since it caches serializers and deserializers and, thus, is expensive to create. See http://wiki.fasterxml.com/JacksonBestPracticesPerformance
    • if your array contains some values, you probably want let jackson to fail deserializing it because it means there was a problem when it got encoded and you should see and fix that asap.

    Here is my "generic" solution for that problem:

    public abstract class EmptyArrayAsNullDeserializer extends JsonDeserializer {
    
      private final Class clazz;
    
      protected EmptyArrayAsNullDeserializer(Class clazz) {
        this.clazz = clazz;
      }
    
      @Override
      public T deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException {
        ObjectCodec oc = jp.getCodec();
        JsonNode node = oc.readTree(jp);
        if (node.isArray() && !node.getElements().hasNext()) {
          return null;
        }
        return oc.treeToValue(node, clazz);
      }
    }
    

    then you still need to create a custom deserializer for each different type, but that's a lot easier to write and you don't duplicate any logic:

    public class Thing2Deserializer extends EmptyArrayAsNullDeserializer {
    
      public Thing2Deserializer() {
        super(Thing2.class);
      }
    }
    

    then you use it as usual:

    @JsonDeserialize(using = Thing2Deserializer.class)
    

    If you find a way to get rid of that last step, i.e. implementing 1 custom deserializer per type, I'm all ears ;)

提交回复
热议问题