杰克逊处理其余的田地 [英] Jackson handling of remaining fields

查看:172
本文介绍了杰克逊处理其余的田地的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道这是否可行。
我有一个巨大的json文件来解析和修改。
但并非所有字段都对我有用:我不想仅映射我感兴趣的字段,修改它们然后将整个json保存到我不关心的所有字段中保持不变。

I wonder if this is possible. I have a huge json file to parse and modify. But not all fields are interresting to me : I wan't to map only fields I'm interrested in, modify them then save the whole json with all fields I didn't care left unchanged.

然后假设我有很多属性,只有一个我对名为MyProp感兴趣,我尝试了类似

Then suppose I have lot of properties and only one I'm interrested in named "MyProp", I tried something like

public class MyMapper extends HashMap {
    private String myProp;
    public void setMyProp(String myProp) {
        this.myProp = myProp;
    }
    public String getMyProp() {
        return myProp;
    }
}

但属性MyProp始终为空(直到我删除extends HashMap)。
相反,所有内容都在地图中。

But the property "MyProp" is always null (until I remove "extends HashMap"). Instead all goes in the map.

能够将具体字段与地图混合起来是否有用(或者它是否已经可行?)使用已知字段而不会丢失未知数?

Wouldn't be useful (or is it already possible ?) to be able to mix concrete fields with maps to work with known fields without loosing unknowns ?

最好不要扩展HashMap,而是使用字段来保存所有未知字段,如下所示:

And it would be better to did not extend HashMap but instead having a field to hold all unknown fields like this :

public class MyMapper {
    private String myProp;
    private Map remaining = new HashMap();
    public void setMyProp(String myProp) {
        this.myProp = myProp;
    }
    public String getMyProp() {
        return myProp;
    }
    @JsonRemaining
    public Map getRemaining() {
        return remaining;
    }
}

在这种情况下你会怎么做?

How would you do in this case ?

推荐答案

好像是 @JsonAnySetter @JsonAnyGetter 注释可能有所帮助。所有无意义的问题都被填入一个地图然后地图内容被转储回json

Seems like @JsonAnySetter and @JsonAnyGetter annotations might help. All irrelecant proerpties get stuffed into one Map and then the map contents get dumped back to json

请参阅如何指导这里
http://vincentdevillers.blogspot.co.il/2013/ 08 / how-to-map-unknown-json-properties-with.html

这篇关于杰克逊处理其余的田地的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆