Details
-
Type:
Improvement
-
Status:
Closed
-
Priority:
Minor
-
Resolution: Cannot Reproduce
-
Affects Version/s: None
-
Fix Version/s: None
-
Component/s: Serializer
-
Labels:None
-
Number of attachments :
Description
For serialization, a simpler property filtering configuration should be available to not require use of annotations.
A good solution might be to allow registering type-based filters with a SimpleFilterProvider, such that this code
public class JacksonFoo { public static void main(String[] args) throws Exception { ObjectMapper mapper = new ObjectMapper().setVisibility(JsonMethod.FIELD, Visibility.ANY); mapper.getSerializationConfig().addMixInAnnotations(Object.class, PropertyFilterMixIn.class); String[] ignorableFieldNames = { "id", "color" }; FilterProvider filters = new SimpleFilterProvider().addFilter("filter properties by name", SimpleBeanPropertyFilter.serializeAllExcept(ignorableFieldNames)); ObjectWriter writer = mapper.writer(filters); System.out.println(writer.writeValueAsString(new Bar())); // output: // {"name":"James","foo":{"size":"big","height":"tall"}} } } @JsonFilter("filter properties by name") class PropertyFilterMixIn { } class Bar { String id = "42"; String name = "Fred"; String color = "blue"; Foo foo = new Foo(); } class Foo { String id = "99"; String size = "big"; String height = "tall"; }
...might then be reduced to just
public class JacksonFoo { public static void main(String[] args) throws Exception { ObjectMapper mapper = new ObjectMapper().setVisibility(JsonMethod.FIELD, Visibility.ANY); String[] ignorableFieldNames = { "id", "color" }; FilterProvider filters = new SimpleFilterProvider().addFilter(Bar.class, SimpleBeanPropertyFilter.serializeAllExcept(ignorableFieldNames)); ObjectWriter writer = mapper.writer(filters); System.out.println(writer.writeValueAsString(new Bar())); // output: // {"name":"James","foo":{"size":"big","height":"tall"}} } } class Bar { String id = "42"; String name = "Fred"; String color = "blue"; Foo foo = new Foo(); } class Foo { String id = "99"; String size = "big"; String height = "tall"; }
...or if
is too dissimilar to the current configuration approach, then
addFilter(Object.class ...would be more appropriate in the example above.