FasterXML / jackson-annotations

Core annotations (annotations that only depend on jackson-core) for Jackson data processor
https://github.com/FasterXML/jackson
Apache License 2.0
1.03k stars 330 forks source link

Reading json to Object failed when using add Mixin annotations #85

Closed DHbee closed 8 years ago

DHbee commented 8 years ago
package com.dh.inheritance;

import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.JsonTypeInfo;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;

public class Client6{

    public static void main(String[] args){

        ObjectMapper objectMapper = new ObjectMapper();
        objectMapper.enableDefaultTyping(); 
        objectMapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL, JsonTypeInfo.As.WRAPPER_OBJECT);         
        objectMapper.setVisibility(PropertyAccessor.SETTER, JsonAutoDetect.Visibility.NONE);
        objectMapper.setVisibility(PropertyAccessor.GETTER, JsonAutoDetect.Visibility.NONE);
        objectMapper.setVisibility(PropertyAccessor.FIELD, JsonAutoDetect.Visibility.ANY);
        objectMapper.setVisibility(PropertyAccessor.IS_GETTER, JsonAutoDetect.Visibility.NONE);         
        objectMapper.configure(SerializationFeature.WRITE_NULL_MAP_VALUES, true);
        objectMapper.configure(SerializationFeature.FAIL_ON_SELF_REFERENCES, true);

        objectMapper.registerModule(new MyModule());

        try {

            String test = null;
            test.length();

        } catch (Exception e) {

            try {
                String jsonString = objectMapper.writeValueAsString(e);
                System.out.println(jsonString);

                objectMapper.readValue(jsonString, e.getClass());
            } catch (JsonProcessingException e1) {
                e1.printStackTrace();
            }
        }
    }   

}

package com.dh.inheritance;

import com.fasterxml.jackson.annotation.JsonIdentityInfo;
import com.fasterxml.jackson.annotation.ObjectIdGenerators;

public abstract class MixIn {

    @JsonIdentityInfo(generator=ObjectIdGenerators.StringIdGenerator.class, property="$id")
    private Throwable cause;

}

package com.dh.inheritance;

import com.fasterxml.jackson.core.Version;
import com.fasterxml.jackson.databind.module.SimpleModule;

public class MyModule extends SimpleModule{

    public MyModule() {
        super("test", new Version(2, 7, 2, null));
    }

    @Override
    public void setupModule(SetupContext context) {
        context.setMixInAnnotations(Throwable.class, MixIn.class);      
    }
}

{"java.lang.NullPointerException":{"detailMessage":null,"cause":{"java.lang.NullPointerException":{"$id":"912da540-bf85-4e5d-869c-acfaebe85bdf","detailMessage":null,"cause":"912da540-bf85-4e5d-869c-acfaebe85bdf","stackTrace":null}},"stackTrace":null}} com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException: Unrecognized field "$id" (class java.lang.NullPointerException), not marked as ignorable (3 known properties: "cause", "stackTrace", "detailMessage"]) at [Source: {"java.lang.NullPointerException":{"detailMessage":null,"cause":{"java.lang.NullPointerException":{"$id":"912da540-bf85-4e5d-869c-acfaebe85bdf","detailMessage":null,"cause":"912da540-bf85-4e5d-869c-acfaebe85bdf","stackTrace":null}},"stackTrace":null}}; line: 1, column: 107](through reference chain: java.lang.NullPointerException["$id"]) at com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException.from(UnrecognizedPropertyException.java:62) at com.fasterxml.jackson.databind.DeserializationContext.reportUnknownProperty(DeserializationContext.java:855) at com.fasterxml.jackson.databind.deser.std.StdDeserializer.handleUnknownProperty(StdDeserializer.java:1083) at com.fasterxml.jackson.databind.deser.BeanDeserializerBase.handleUnknownProperty(BeanDeserializerBase.java:1389) at com.fasterxml.jackson.databind.deser.std.ThrowableDeserializer.deserializeFromObject(ThrowableDeserializer.java:135) at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:133) at com.fasterxml.jackson.databind.jsontype.impl.AsWrapperTypeDeserializer._deserialize(AsWrapperTypeDeserializer.java:115) at com.fasterxml.jackson.databind.jsontype.impl.AsWrapperTypeDeserializer.deserializeTypedFromObject(AsWrapperTypeDeserializer.java:49) at com.fasterxml.jackson.databind.deser.BeanDeserializerBase.deserializeWithType(BeanDeserializerBase.java:1017) at com.fasterxml.jackson.databind.deser.SettableBeanProperty.deserialize(SettableBeanProperty.java:488) at com.fasterxml.jackson.databind.deser.std.ThrowableDeserializer.deserializeFromObject(ThrowableDeserializer.java:104) at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:133) at com.fasterxml.jackson.databind.jsontype.impl.AsWrapperTypeDeserializer._deserialize(AsWrapperTypeDeserializer.java:115) at com.fasterxml.jackson.databind.jsontype.impl.AsWrapperTypeDeserializer.deserializeTypedFromObject(AsWrapperTypeDeserializer.java:49) at com.fasterxml.jackson.databind.deser.BeanDeserializerBase.deserializeWithType(BeanDeserializerBase.java:1017) at com.fasterxml.jackson.databind.deser.impl.TypeWrappedDeserializer.deserialize(TypeWrappedDeserializer.java:42) at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:3789) at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:2779) at com.dh.inheritance.Client6.main(Client6.java:38)

Above exception thrown, when converting json to Object.Please let me know what is the problem?

cowtowncoder commented 8 years ago

@DHbee please use mailing list for questions. For actual bugs with databinding, please use jackson-databind project: while this project defines annotation types to use, none of handling is included here; it is handled by code in jackson-databind.

As to problem itself: I do not understand what you are trying to do -- why are you forcing use of Object Id for Throwable object if the only goal is to print out json representation of it? Or this just to demonstrate the problem.

What triggers the issue is probably the fact that Throwable objects are not handled as POJOs, and thereby Object Id handling may not be supported on deserialization. If so, it might be worth filing an RFE for, if problem can be reproduced with slightly more straight-forward unit test.