java - (Jackson 1.9.2) 让@JsonIgnore 工作 : deserialization with mixin annotations and polymorphic types

标签 java json jackson deserialization

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

{
    "$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 注释

@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,我得到的错误是

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

我尝试使用 @JsonIgnore,但这没有帮助(检查我注释掉的代码的位置,看看我是如何尝试的)。我可能遗漏了什么,但我认为需要另一双眼睛来审视它并帮助我。

这是反序列化环境的样子:

        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 模块看起来像

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

感谢所有的帮助。提前致谢。

PS:我不知道这是否有所不同,但 JSON 是由用 .Net Framework v4 编写的库生成的。

最佳答案

我认为 Jackson 有问题,因为您作为创建者委托(delegate)的方法没有 readOnly 字段的参数。因此, jackson 并没有默默地忽略这个缺失的参数,而是抛出了一个错误(良好的行为)。尝试使用 JsonIgnoreProperties注解。我以前不需要使用 if ,但是对于反序列化,您可以明确表示创建者不需要的字段。

哦,this似乎也相关。

编辑: Another question that the asker found that was useful.

关于java - (Jackson 1.9.2) 让@JsonIgnore 工作 : deserialization with mixin annotations and polymorphic types,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8421990/

相关文章:

xml - 如何强制 Jackson 关闭一个空的 XML 元素?

java - 在远程客户端计算机上运行sqoop

java - 如何暂停/停止正在播放的歌曲?

json - Elasticsearch 将用户输入存储为 JSON 文档

php - 字符串中的无效 JSON 转义序列

java - jackson 解析器 : ignore deserializing for type mismatch

java - 从 Gcloud 存储下载的图像或 Pdf 文件已损坏

java - 如何为第三方 POJO 创建构建器?

java - 使用 postman 测试具有 RequestParam 的休息服务

java - 以编程方式更改 JsonProperty (Access.WRITE_ONLY)