Jackson不同的JSONFilter同类 [英] Jackson Different JSONFilter same Class

查看:158
本文介绍了Jackson不同的JSONFilter同类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用不同的JsonFilters来序列化同一类的对象。

I am trying to use different JsonFilters to serialize objects of the same class.

想象一下类Foo

public class Foo{
    Bar p1;
    Bar p2;
}

和班级酒吧

@JsonFilter("myFilter")
public class Bar{
    String prop1;
    String prop2;
    String prop3;
}

现在我想要实现的是为变量p1和p2设置不同的JsonFilters在Foo类中。

Now what I want to achieve is set different JsonFilters for the variables p1 and p2 in class Foo.

例如。对于p1我想序列化只有prop1,而对于p2我想序列化prop2和prop3。
获得以下json

For example. for p1 i want to serialize only prop1, and for p2 i want to serialize prop2 and prop3. To get the following json

{
    "p1": { "prop1":"blabla" }
    "p2": { "prop2":"blabla", "prop3":"blabla" }
}

所以我会使用以下过滤器,只有prop1会被序列化:

So I would use the following filter so that only "prop1" would get serialized:

FilterProvider filter = new SimpleFilterProvider().addFilter("myFilter",
    SimpleBeanPropertyFilter.filterOutAllExcept("prop1"));

String json = new ObjectMapper().filteredWriter(filter).writeValueAsString(foo)

但是使用这个也会导致p2被序列化只有prop1

But using this would also cause p2 to be serialized only having prop1

我想为p2创建另一个过滤器,如下所示:

I would like to create another filter for p2 like so:

FilterProvider filter2 = new SimpleFilterProvider().addFilter("myFilter",
    SimpleBeanPropertyFilter.filterOutAllExcept("prop2","prop3"));

但我真的找不到如何实现它,以便p1和p2有不同的过滤器看到他们属于同一类Foo。

But I really can't find how to implement it so that there are different filters for p1 and p2 seeing that they are of the same class Foo.

感谢您的阅读,希望有人可以提供帮助!

Thank you for reading, I hope someone can help!

推荐答案

从Jackson 2.3开始, @JsonFilter 注释可以放在字段和方法上。我认为它应该对你的情况有所帮助。下面是一个例子:

Starting from Jackson 2.3 the @JsonFilter annotation can be put on the fields and method. I think it should help in your case. Here is an example:

public class JacksonFilters {
    static class Bar {
        final public String prop1;
        final public String prop2;
        final public String prop3;

        Bar(final String prop1, final String prop2, final String prop3) {
            this.prop1 = prop1;
            this.prop2 = prop2;
            this.prop3 = prop3;
        }
    }

    static class Foo {
        @JsonFilter("myFilter1")
        final public Bar p1;
        @JsonFilter("myFilter2")
        final public Bar p2;

        Foo(final Bar p1, final Bar p2) {
            this.p1 = p1;
            this.p2 = p2;
        }
    }

    public static void main(String[] args) throws JsonProcessingException {
        final SimpleFilterProvider filterProvider = new SimpleFilterProvider();
        filterProvider.addFilter("myFilter1",
                SimpleBeanPropertyFilter.filterOutAllExcept("prop1"));
        filterProvider.addFilter("myFilter2",
                SimpleBeanPropertyFilter.serializeAllExcept("prop2"));

        final Foo bar = new Foo(new Bar("a", "b", "c"), new Bar("d", "e", "f"));

        final ObjectMapper mapper = new ObjectMapper();
        mapper.setFilters(filterProvider);
        System.out.println(mapper
                        .writerWithDefaultPrettyPrinter()
                        .writeValueAsString(bar));
    }

}

输出:

{
  "p1" : {
    "prop1" : "a"
  },
  "p2" : {
    "prop1" : "d",
    "prop3" : "f"
  }
}

这篇关于Jackson不同的JSONFilter同类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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