(Jackson 1.9.2) Getting @JsonIgnore to work: deserialization with mixin annotations and polymorphic types

我们两清 提交于 2019-12-11 12:09:55

问题


I am trying to deserialize a JSON String like (extracted this bit to show the issue)

{
    "$type": "com.example.StringProperty",
    "value": "hello world",
    "name": "text",
    "readOnly": false
}

into a hierarchy of classes that look like

public class Property
{
    public String name;
    public int    type;

    Property(String pName, int pType) { name = pName; type = pType; }
}

public class StringProperty extends Property 
{
    public String value;        
    StringProperty(String pName, String pValue) {
        super(pName, String);
        value = pValue;
    }       
}

using the following mixin annotations

@JsonTypeInfo(use=JsonTypeInfo.Id.CLASS, include=JsonTypeInfo.As.PROPERTY, property="$type")
abstract public class PropertyMixin
{
    @JsonProperty
    public String name;
    //@JsonIgnore
    //public boolean readOnly;
    @JsonProperty
    public int type;

    PropertyMixin(@JsonProperty("name") String pName, @JsonProperty("type") int pType)
    {
    }
}

abstract public class StringPropertyMixin
{
    @JsonProperty
    public String value;
    //@JsonIgnore
    //public boolean readOnly;

    @JsonCreator
    public StringPropertyMixin(@JsonProperty("name") String pName, @JsonProperty("value") String value)
    {
    }
}

With Jackson 1.9.2, the error I am getting is

Unrecognized field "readOnly" (Class com.example.StringProperty), not marked as ignorable

I tried using @JsonIgnore, but that did not help (check the locations of the code I commented out to see how I tried it). I am probably missing something, but I think another set of eyes need to look at it and help me.

This is how the deserialization environment looks like:

        objectMapper.setVisibilityChecker(objectMapper.getSerializationConfig().getDefaultVisibilityChecker()
                .withFieldVisibility(JsonAutoDetect.Visibility.NONE)
                .withGetterVisibility(JsonAutoDetect.Visibility.NONE)
                .withSetterVisibility(JsonAutoDetect.Visibility.NONE)
                .withCreatorVisibility(JsonAutoDetect.Visibility.NONE));

        MixinRegistrations module = new MixinRegistrations();
        objectMapper.registerModule(module);

and mixin module looks like

@Override
public void setupModule(SetupContext context)
{
    context.setMixInAnnotations(Property.class, PropertyMixin.class);
    context.setMixInAnnotations(StringProperty.class, StringPropertyMixin.class);
}

I appreciate all the help. Thanks in advance.

PS: I do not know if this makes a difference but the JSON is being generated by a library written in .Net Framework v4.


回答1:


I believe Jackson is having problems because the method that you have delegated as a creator does not have a parameter for the readOnly field. So rather than silently ignore this missing parameter Jackson is throwing an error (good behaviour). Try using the JsonIgnoreProperties annotation. I've not had need to use if before, but for deserialisation you can explicitly denote fields that are not required by the creator.

Oh, this appears related as well.

Edit: Another question that the asker found that was useful.




回答2:


Note that your problem is NOT that class has a property Jackson does not recognize, but rather that JSON has a property. As such you may want to read this wiki page.

As mentioned, @JsonIgnoreProperties would work here, since adding that annotation to a class does not require a field or setter, unlike @JsonIgnore does. Plus it can be used to ignore any and all unknown properties.




回答3:


@JsonIgnoreProperties({"propX", "propY"})

Would work just fine at class level!



来源:https://stackoverflow.com/questions/8421990/jackson-1-9-2-getting-jsonignore-to-work-deserialization-with-mixin-annotati

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!