使用mixin注释和多态类型进行反序列化

使用mixin注释和多态类型进行反序列化

本文介绍了(Jackson 1.9.2)让@JsonIgnore工作:使用mixin注释和多态类型进行反序列化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试反序列化一个JSON字符串(提取此位以显示问题)

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
}

进入类似于

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;
    }
}

使用以下mixin注释

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)
    {
    }
}

使用Jackson 1.9.2,我得到的错误是

With Jackson 1.9.2, the error I am getting is

无法识别的字段readOnly(类com.example.StringProperty),未标记为可忽略

我尝试使用 @JsonIgnore ,但这没有帮助(检查我注释掉的代码的位置以了解我是如何尝试的)。我可能错过了一些东西,但我认为另一组眼睛需要看一下并帮助我。

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);

和mixin模块看起来像

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:我不知道这是否有所不同,但JSON是由.Net Framework v4编写的库生成的。

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

推荐答案

我相信杰克逊遇到了问题,因为您作为创建者委派的方法没有的参数readOnly 字段。所以,而不是默默地忽略这个缺失的参数杰克逊抛出一个错误(良好的行为)。尝试使用注释。我之前没有必要使用,但是对于反序列化,你可以明确地表示创建者不需要的字段。

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.

哦,也显示相关。

编辑:

这篇关于(Jackson 1.9.2)让@JsonIgnore工作:使用mixin注释和多态类型进行反序列化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-21 13:38