反序列化似乎不尊重@JsonIgnore

时间:2018-09-28 01:26:48

标签: java serialization jackson deserialization

Jackson版本:2.8

课程摘要:

import com.fasterxml.jackson.annotation.JsonIgnore;

@Builder
@Data
public class DifficultType implements Comparable<DifficultType> {
    @Builder.Default
    private String type = "";
    ...
    @Builder.Default
    @JsonIgnore
    private MethodCallExpr astNode = null;
}

MyObjectType类具有List<DifficultType>,并且List<MyObjectType>正在序列化/反序列化。

该对象的序列化工作正常,但是在尝试反序列化时,出现以下异常:

    Exception in thread "main" com.fasterxml.jackson.databind.JsonMappingException: Conflicting setter definitions for property "asParentNodeOf": com.github.javaparser.ast.Node#setAsParentNodeOf(1 params) vs com.github.javaparser.ast.Node#setAsParentNodeOf(1 params)
 at [Source: ./ObjectList.json; line: 1, column: 1]
    at com.fasterxml.jackson.databind.JsonMappingException.from(JsonMappingException.java:305)
    at com.fasterxml.jackson.databind.deser.DeserializerCache._createAndCache2(DeserializerCache.java:268)
    at com.fasterxml.jackson.databind.deser.DeserializerCache._createAndCacheValueDeserializer(DeserializerCache.java:244)
    at com.fasterxml.jackson.databind.deser.DeserializerCache.findValueDeserializer(DeserializerCache.java:142)
    at com.fasterxml.jackson.databind.DeserializationContext.findContextualValueDeserializer(DeserializationContext.java:443)
    at com.fasterxml.jackson.databind.deser.impl.PropertyBasedCreator.construct(PropertyBasedCreator.java:89)
    at com.fasterxml.jackson.databind.deser.BeanDeserializerBase.resolve(BeanDeserializerBase.java:565)
    at com.fasterxml.jackson.databind.deser.DeserializerCache._createAndCache2(DeserializerCache.java:293)
    at com.fasterxml.jackson.databind.deser.DeserializerCache._createAndCacheValueDeserializer(DeserializerCache.java:244)
    at com.fasterxml.jackson.databind.deser.DeserializerCache.findValueDeserializer(DeserializerCache.java:142)
    at com.fasterxml.jackson.databind.DeserializationContext.findContextualValueDeserializer(DeserializationContext.java:443)
    at com.fasterxml.jackson.databind.deser.std.CollectionDeserializer.createContextual(CollectionDeserializer.java:206)
    at com.fasterxml.jackson.databind.deser.std.CollectionDeserializer.createContextual(CollectionDeserializer.java:26)
    at com.fasterxml.jackson.databind.DeserializationContext.handlePrimaryContextualization(DeserializationContext.java:650)
    at com.fasterxml.jackson.databind.deser.BeanDeserializerBase.resolve(BeanDeserializerBase.java:490)
    at com.fasterxml.jackson.databind.deser.DeserializerCache._createAndCache2(DeserializerCache.java:293)
    at com.fasterxml.jackson.databind.deser.DeserializerCache._createAndCacheValueDeserializer(DeserializerCache.java:244)
    at com.fasterxml.jackson.databind.deser.DeserializerCache.findValueDeserializer(DeserializerCache.java:142)
    at com.fasterxml.jackson.databind.DeserializationContext.findContextualValueDeserializer(DeserializationContext.java:443)
    at com.fasterxml.jackson.databind.deser.std.CollectionDeserializer.createContextual(CollectionDeserializer.java:206)
    at com.fasterxml.jackson.databind.deser.std.CollectionDeserializer.createContextual(CollectionDeserializer.java:26)
    at com.fasterxml.jackson.databind.DeserializationContext.handleSecondaryContextualization(DeserializationContext.java:681)
    at com.fasterxml.jackson.databind.DeserializationContext.findRootValueDeserializer(DeserializationContext.java:481)
    at com.fasterxml.jackson.databind.ObjectMapper._findRootDeserializer(ObjectMapper.java:3915)
    at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:3810)
    at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:2775)
    at com.amazonaws.services.guru.ast.MissingApiDetector.main(MissingApiDetector.java:62)
Caused by: java.lang.IllegalArgumentException: Conflicting setter definitions for property "asParentNodeOf": com.github.javaparser.ast.Node#setAsParentNodeOf(1 params) vs com.github.javaparser.ast.Node#setAsParentNodeOf(1 params)
    at com.fasterxml.jackson.databind.introspect.POJOPropertyBuilder.getSetter(POJOPropertyBuilder.java:323)
    at com.fasterxml.jackson.databind.deser.BeanDeserializerFactory.filterBeanProps(BeanDeserializerFactory.java:610)
    at com.fasterxml.jackson.databind.deser.BeanDeserializerFactory.addBeanProps(BeanDeserializerFactory.java:500)
    at com.fasterxml.jackson.databind.deser.BeanDeserializerFactory.buildBeanDeserializer(BeanDeserializerFactory.java:230)
    at com.fasterxml.jackson.databind.deser.BeanDeserializerFactory.createBeanDeserializer(BeanDeserializerFactory.java:145)
    at com.fasterxml.jackson.databind.deser.DeserializerCache._createDeserializer2(DeserializerCache.java:403)
    at com.fasterxml.jackson.databind.deser.DeserializerCache._createDeserializer(DeserializerCache.java:349)
    at com.fasterxml.jackson.databind.deser.DeserializerCache._createAndCache2(DeserializerCache.java:264)
    ... 25 more

序列化代码:

 ObjectMapper mapper = new ObjectMapper();
 String jsonDataObject = mapper.writeValueAsString(myObjectList);                                                                                                                                                                                                              
 ObjectWriter writer = mapper.writer(new DefaultPrettyPrinter());                                                                                                                                                                                                                   
 writer.writeValue(new File("./ObjectList.json"), myObjectList);                                                                                                                                                                                             

反序列化代码:

final List<MyObjectType> myObjectList =  mapper.readValue(new File("./ObjectList.json"), new TypeReference<List<MyObjectType>>(){});

串行化工作正常,ObjectList.json看起来还不错。当我尝试读取此文件并反序列化时,出现上述异常。

由于某些原因,这里似乎没有忽略com.github.javaparser.ast.expr.MethodCallExpr字段。救命!

0 个答案:

没有答案