我正在尝试使用Jackson 2.6.3反序列化json上下文。我在json内容中具有空文字,如下所示。我试图避免将其添加到反序列化集合中。请参见下面的输出。我在映射器上尝试了各种配置选项。但是,我无法避免集合中出现空对象。欢迎任何解决问题的指针。

我在stackoverflow中看到了许多类似的问题,如下所示,但它们都与自定义反序列化类中的单个属性有关,而与集合中的元素无关。

How to deserialize JSON null to a NullNode instead of Java null?

JSON内容(Data.txt)

{
  "myList": [
  {
     "type": "1"
  },
  {
     "type": "2"
  },
  null
  ]
}


RootObject.java

@JsonInclude(Include.NON_NULL)
public class RootObject {

private List<Types> myList;

public List<Types> getMyList() {
    return myList;
}

public void setMyList(List<Types> myList) {
    this.myList = myList;
}

public String toString(){
    return new ReflectionToStringBuilder(this).toString();
}
}


Types.java

@JsonInclude(Include.NON_NULL)
public class Types {

private String type;

public String getType() {
    return type;
}

public void setType(String type) {
    this.type = type;
}

public String toString(){
    return new ReflectionToStringBuilder(this).toString();
}
}


杰克逊测试仪

public class JacksonTester2 {

    public static void main(String[] args) {
        ObjectMapper mapper = new ObjectMapper();
        mapper.getSerializationConfig().withSerializationInclusion(Include.NON_EMPTY);
        mapper.configure(SerializationFeature.WRITE_NULL_MAP_VALUES, false);
        RootObject rootObject = null;
        try {
            rootObject = mapper.readValue(new File("Data.txt"), RootObject.class);
        } catch (Exception e) {
            e.printStackTrace();
        }
        System.out.println(rootObject);
    }
    }


输出量

RootObject@1a74681a[myList=[Types@ace10d9[type=1], Types@5eb41c19[type=2], null]]

最佳答案

我从以下帖子中得到了启发

Jackson: Ignore whitespace in empty @XmlWrapperElement collection

这是上述问题的解决方案

public class NullCollectionHandler extends SimpleModule {
    private static class CustomizedCollectionDeserialiser extends CollectionDeserializer {

        public CustomizedCollectionDeserialiser(CollectionDeserializer src) {
            super(src);
        }

        private static final long serialVersionUID = 1L;

        @Override
        public Collection<Object> deserialize(JsonParser jp, DeserializationContext ctxt)
                throws IOException, JsonProcessingException {
            Collection<Object> oldCol = super.deserialize(jp, ctxt);
            Collection<Object> newCol = new ArrayList<Object>();
            if(CollectionUtils.isNotEmpty(oldCol)){
                for(Object obj : oldCol){
                    if(obj != null)
                    newCol.add(obj);
                }
            }
            return newCol;
        }

        @Override
        public CollectionDeserializer createContextual(DeserializationContext ctxt,
                BeanProperty property) throws JsonMappingException {
            return new CustomizedCollectionDeserialiser(super.createContextual(ctxt, property));
        }
    }

    private static final long serialVersionUID = 1L;

    @Override
    public void setupModule(SetupContext context) {
        super.setupModule(context);
        context.addBeanDeserializerModifier(new BeanDeserializerModifier() {
            @Override
            public JsonDeserializer<?> modifyCollectionDeserializer(
                    DeserializationConfig config, CollectionType type,
                    BeanDescription beanDesc, JsonDeserializer<?> deserializer) {
                if (deserializer instanceof CollectionDeserializer) {
                    return new CustomizedCollectionDeserialiser(
                        (CollectionDeserializer) deserializer);
                } else {
                    return super.modifyCollectionDeserializer(config, type, beanDesc,
                        deserializer);
                }
            }
        });
    }
}

10-04 17:30