Skip to content

Commit

Permalink
Feature : convenience method `SimpleBeanPropertyFilter.filterOutAll()…
Browse files Browse the repository at this point in the history
…` as symmetric counterpart of `serializeAll()` (#3819)
  • Loading branch information
JooHyukKim authored Mar 11, 2023
1 parent 05cb9a6 commit 119addc
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,16 @@ public static SimpleBeanPropertyFilter serializeAll() {
return SerializeExceptFilter.INCLUDE_ALL;
}

/**
* Convenience factory method that will return a filter that will
* simply filter out everything.
*
* @since 2.15
*/
public static SimpleBeanPropertyFilter filterOutAll() {
return FilterExceptFilter.EXCLUDE_ALL;
}

/**
* Factory method that was accidentally added in 2.5 with arguments; basically
* works just as an alias of {@link #filterOutAllExcept(Set)} which is not
Expand Down Expand Up @@ -258,11 +268,17 @@ public static class FilterExceptFilter
{
private static final long serialVersionUID = 1L;

static final FilterExceptFilter EXCLUDE_ALL = new FilterExceptFilter();

/**
* Set of property names to serialize.
*/
protected final Set<String> _propertiesToInclude;

FilterExceptFilter() {
_propertiesToInclude = Collections.emptySet();
}

public FilterExceptFilter(Set<String> properties) {
_propertiesToInclude = properties;
}
Expand Down Expand Up @@ -313,4 +329,4 @@ protected boolean include(PropertyWriter writer) {
return !_propertiesToExclude.contains(writer.getName());
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@
import com.fasterxml.jackson.databind.ser.PropertyWriter;
import com.fasterxml.jackson.databind.ser.impl.*;

import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;

/**
* Tests for verifying that bean property filtering using JsonFilter
* works as expected.
Expand Down Expand Up @@ -40,6 +44,20 @@ static class C {
}
}

@JsonFilter("filterB")
@JsonPropertyOrder({ "a", "b", "c"})
static class BeanB {
public String a;
public String b;
public String c;

public BeanB(String a, String b, String c) {
this.a = a;
this.b = b;
this.c = c;
}
}

static class CheckSiblingContextFilter extends SimpleBeanPropertyFilter {
@Override
public void serializeAsField(Object bean, JsonGenerator jgen, SerializerProvider prov, PropertyWriter writer) throws Exception {
Expand Down Expand Up @@ -132,6 +150,13 @@ public void testIncludeAllFilter() throws Exception
assertEquals("{\"a\":\"a\",\"b\":\"b\"}", MAPPER.writer(prov).writeValueAsString(new Bean()));
}

public void testExcludeAllFilter() throws Exception
{
FilterProvider prov = new SimpleFilterProvider().addFilter("RootFilter",
SimpleBeanPropertyFilter.filterOutAll());
assertEquals("{}", MAPPER.writer(prov).writeValueAsString(new Bean()));
}

public void testSimpleExclusionFilter() throws Exception
{
FilterProvider prov = new SimpleFilterProvider().addFilter("RootFilter",
Expand Down Expand Up @@ -191,4 +216,44 @@ public void testFilterOnProperty() throws Exception
assertEquals("{\"first\":{\"a\":\"a\"},\"second\":{\"b\":\"b\"}}",
MAPPER.writer(prov).writeValueAsString(new FilteredProps()));
}

public void testAllFiltersWithSameOutput() throws Exception
{
// Setup
SimpleBeanPropertyFilter[] allPossibleFilters = new SimpleBeanPropertyFilter[]{
// Parent class : SimpleBeanPropertyFilter
SimpleBeanPropertyFilter.filterOutAllExcept("a", "b"),
SimpleBeanPropertyFilter.filterOutAllExcept(setOf("a", "b")),
SimpleBeanPropertyFilter.serializeAllExcept("c"),
SimpleBeanPropertyFilter.serializeAllExcept(setOf("c")),
// Subclass : SerializeExceptFilter
new SimpleBeanPropertyFilter.SerializeExceptFilter(setOf("c")),
SimpleBeanPropertyFilter.SerializeExceptFilter.serializeAllExcept("c"),
SimpleBeanPropertyFilter.SerializeExceptFilter.serializeAllExcept(setOf("c")),
SimpleBeanPropertyFilter.SerializeExceptFilter.filterOutAllExcept("a", "b"),
SimpleBeanPropertyFilter.SerializeExceptFilter.filterOutAllExcept(setOf("a", "b")),
// Subclass : FilterExceptFilter
new SimpleBeanPropertyFilter.FilterExceptFilter(setOf("a", "b")),
SimpleBeanPropertyFilter.FilterExceptFilter.serializeAllExcept("c"),
SimpleBeanPropertyFilter.FilterExceptFilter.serializeAllExcept(setOf("c")),
SimpleBeanPropertyFilter.FilterExceptFilter.filterOutAllExcept(setOf("a", "b")),
SimpleBeanPropertyFilter.FilterExceptFilter.filterOutAllExcept("a", "b")
};

// Tests
for (SimpleBeanPropertyFilter filter : allPossibleFilters) {
BeanB beanB = new BeanB("aa", "bb", "cc");
SimpleFilterProvider prov = new SimpleFilterProvider().addFilter("filterB", filter);

String jsonStr = MAPPER.writer(prov).writeValueAsString(beanB);

assertEquals(a2q("{'a':'aa','b':'bb'}"), jsonStr);
}
}

private Set<String> setOf(String... properties) {
Set<String> set = new HashSet<>(properties.length);
set.addAll(Arrays.asList(properties));
return set;
}
}

0 comments on commit 119addc

Please sign in to comment.