public void testMapFilteringViaProps() throws Exception { FilterProvider prov = new SimpleFilterProvider() .addFilter("filterX", SimpleBeanPropertyFilter.filterOutAllExcept("b")); String json = MAPPER.writer(prov).writeValueAsString(new MapBean()); assertEquals(aposToQuotes("{'values':{'b':5}}"), json); }
@Override public PropertyFilter findPropertyFilter(final Object filterId, final Object valueToFilter) { if (filterId instanceof String) { final String id = (String) filterId; // FilterId should represent a class only in case of root entity is marshalled. if (id.equals(root.getEntityClass().getName())) { stack.clear(); return stack.push(root); } while (!stack.isEmpty()) { final FilteringPropertyFilter peek = stack.peek(); final FilteringPropertyFilter subfilter = peek.findSubfilter(id); if (subfilter != null) { stack.push(subfilter); // Need special handling for maps here - map keys can be filtered as well so we just say // that every key is // allowed. if (valueToFilter instanceof Map) { final Map<String, ?> map = (Map<String, ?>) valueToFilter; return new FilteringPropertyFilter( Map.class, map.keySet(), Collections.<String, FilteringPropertyFilter>emptyMap()); } return subfilter; } else { stack.pop(); } } } return SimpleBeanPropertyFilter.filterOutAllExcept(); }
public void testMapFilteringViaClass() throws Exception { FilteredBean bean = new FilteredBean(); bean.put("a", 4); bean.put("b", 3); FilterProvider prov = new SimpleFilterProvider() .addFilter("filterForMaps", SimpleBeanPropertyFilter.filterOutAllExcept("b")); String json = MAPPER.writer(prov).writeValueAsString(bean); assertEquals(aposToQuotes("{'b':3}"), json); }