Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jackson filtering out fields without annotations

Tags:

java

json

jackson

I was trying to filter out certain fields from serialization via SimpleBeanPropertyFilter using the following (simplified) code:

public static void main(String[] args) {
    ObjectMapper mapper = new ObjectMapper();

    SimpleFilterProvider filterProvider = new SimpleFilterProvider().addFilter("test",
            SimpleBeanPropertyFilter.filterOutAllExcept("data1"));
    try {
        String json = mapper.writer(filterProvider).writeValueAsString(new Data());

        System.out.println(json); // output: {"data1":"value1","data2":"value2"}

    } catch (JsonProcessingException e) {
        e.printStackTrace();
    }
}

private static class Data {
    public String data1 = "value1";
    public String data2 = "value2";
}

Us I use SimpleBeanPropertyFilter.filterOutAllExcept("data1")); I was expecting that the created serialized Json string contains only {"data1":"value1"}, however I get {"data1":"value1","data2":"value2"}.

How to create a temporary writer that respects the specified filter (the ObjectMapper can not be re-configured in my case).

Note: Because of the usage scenario in my application I can only accept answers that do not use Jackson annotations.

like image 739
JMax Avatar asked Jun 21 '17 08:06

JMax


People also ask

How do you ignore fields in Jackson?

If there are fields in Java objects that do not wish to be serialized, we can use the @JsonIgnore annotation in the Jackson library. The @JsonIgnore can be used at the field level, for ignoring fields during the serialization and deserialization.

How do you ignore certain fields based on a serializing Object to JSON?

The Jackson @JsonIgnore annotation can be used to ignore a certain property or field of a Java object. The property can be ignored both when reading JSON into Java objects and when writing Java objects into JSON.

How do I ignore JSON property?

To ignore individual properties, use the [JsonIgnore] attribute. You can specify conditional exclusion by setting the [JsonIgnore] attribute's Condition property. The JsonIgnoreCondition enum provides the following options: Always - The property is always ignored.

What is JsonFilter?

@JsonFilter defines a filter name using which we filter out properties in JSON serialization. @JsonFilter are used at class level. It can also be used at property level since Jackson 2.3. Jackson provides SimpleFilterProvider that is used to add filters to ObjectMapper .


1 Answers

If for some reason MixIns does not suit you. You can try this approach:

ObjectMapper objectMapper = new ObjectMapper();
objectMapper.setAnnotationIntrospector(new JacksonAnnotationIntrospector(){
    @Override
    public boolean hasIgnoreMarker(final AnnotatedMember m) {

    List<String> exclusions = Arrays.asList("field1", "field2");
    return exclusions.contains(m.getName())|| super.hasIgnoreMarker(m);
    }
});
like image 174
doubleW Avatar answered Sep 18 '22 11:09

doubleW