杰克逊基于场的序列化 [英] Jackson field based serialization

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

问题描述

我认为Jackson会基于方法进行序列化,有什么方法可以让它基于字段吗?

I think Jackson does method based serialization, is there any way I could make it field based?

Ex:

class Bean {
    Integer i;
    String s;

    public Integer getI() { return this.i; }
    public void setI(Integer i) { this.i = i; }
    public bool isSetI() { return this.i != null; }

    // same for s as well
}

输出JSON具有i和setI。无论如何,我可以覆盖这个只获得我?
如果有一种方法可以在不向类添加任何注释的情况下(它们是自动生成的),也会很棒。

The output JSON has "i" and "setI". Is there anyway I could override this to get only "i"? Also it would be great if there was a way to do this without adding any annotations to the class (they are auto generated).

推荐答案

要在没有注释的情况下完成此操作,您可以在按以下方式序列化/反序列化之前配置 ObjectMapper

To accomplish this without annotations you can configure the ObjectMapper before serializing/deserializing in the following manner:

ObjectMapper om = new ObjectMapper();
om.setVisibilityChecker(om.getSerializationConfig().getDefaultVisibilityChecker().
            withGetterVisibility(JsonAutoDetect.Visibility.NONE).
            withSetterVisibility(JsonAutoDetect.Visibility.NONE));

这篇关于杰克逊基于场的序列化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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