Subler
Subler

Reputation: 657

Jackson Different JSONFilter same Class

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

Imagine class Foo

public class Foo{
    Bar p1;
    Bar p2;
}

and class Bar

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

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

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

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)

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

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

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

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!

Upvotes: 2

Views: 8589

Answers (1)

Alexey Gavrilov
Alexey Gavrilov

Reputation: 10853

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

}

Output:

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

Upvotes: 4

Related Questions