1 package com.fasterxml.jackson.databind.ser.filter; 2 3 import java.util.*; 4 5 import com.fasterxml.jackson.annotation.*; 6 7 import com.fasterxml.jackson.core.JsonGenerator; 8 9 import com.fasterxml.jackson.databind.BaseMapTest; 10 import com.fasterxml.jackson.databind.ObjectMapper; 11 import com.fasterxml.jackson.databind.SerializerProvider; 12 import com.fasterxml.jackson.databind.ser.FilterProvider; 13 import com.fasterxml.jackson.databind.ser.PropertyWriter; 14 import com.fasterxml.jackson.databind.ser.impl.SimpleBeanPropertyFilter; 15 import com.fasterxml.jackson.databind.ser.impl.SimpleFilterProvider; 16 17 /** 18 * Unit tests for ensuring that entries accessible via "any filter" 19 * can also be filtered with JSON Filter functionality. 20 */ 21 public class TestAnyGetterFiltering extends BaseMapTest 22 { 23 @JsonFilter("anyFilter") 24 public static class AnyBean 25 { 26 private Map<String, String> properties = new HashMap<String, String>(); 27 { 28 properties.put("a", "1"); 29 properties.put("b", "2"); 30 } 31 32 @JsonAnyGetter anyProperties()33 public Map<String, String> anyProperties() 34 { 35 return properties; 36 } 37 } 38 39 public static class AnyBeanWithIgnores 40 { 41 private Map<String, String> properties = new LinkedHashMap<String, String>(); 42 { 43 properties.put("a", "1"); 44 properties.put("bogus", "2"); 45 properties.put("b", "3"); 46 } 47 48 @JsonAnyGetter 49 @JsonIgnoreProperties({ "bogus" }) anyProperties()50 public Map<String, String> anyProperties() 51 { 52 return properties; 53 } 54 } 55 56 // [databind#1655] 57 @JsonFilter("CustomFilter") 58 static class OuterObject { getExplicitProperty()59 public int getExplicitProperty() { 60 return 42; 61 } 62 63 @JsonAnyGetter getAny()64 public Map<String, Object> getAny() { 65 Map<String, Object> extra = new HashMap<>(); 66 extra.put("dynamicProperty", "I will not serialize"); 67 return extra; 68 } 69 } 70 71 static class CustomFilter extends SimpleBeanPropertyFilter { 72 @Override serializeAsField(Object pojo, JsonGenerator gen, SerializerProvider provider, PropertyWriter writer)73 public void serializeAsField(Object pojo, JsonGenerator gen, SerializerProvider provider, 74 PropertyWriter writer) throws Exception 75 { 76 if (pojo instanceof OuterObject) { 77 writer.serializeAsField(pojo, gen, provider); 78 } 79 } 80 } 81 82 /* 83 /********************************************************** 84 /* Test methods 85 /********************************************************** 86 */ 87 88 private final ObjectMapper MAPPER = new ObjectMapper(); 89 testAnyGetterFiltering()90 public void testAnyGetterFiltering() throws Exception 91 { 92 FilterProvider prov = new SimpleFilterProvider().addFilter("anyFilter", 93 SimpleBeanPropertyFilter.filterOutAllExcept("b")); 94 assertEquals("{\"b\":\"2\"}", MAPPER.writer(prov).writeValueAsString(new AnyBean())); 95 } 96 97 // for [databind#1142] testAnyGetterIgnore()98 public void testAnyGetterIgnore() throws Exception 99 { 100 assertEquals(aposToQuotes("{'a':'1','b':'3'}"), 101 MAPPER.writeValueAsString(new AnyBeanWithIgnores())); 102 } 103 104 // [databind#1655] testAnyGetterPojo1655()105 public void testAnyGetterPojo1655() throws Exception 106 { 107 FilterProvider filters = new SimpleFilterProvider().addFilter("CustomFilter", new CustomFilter()); 108 String json = MAPPER.writer(filters).writeValueAsString(new OuterObject()); 109 Map<?,?> stuff = MAPPER.readValue(json, Map.class); 110 if (stuff.size() != 2) { 111 fail("Should have 2 properties, got: "+stuff); 112 } 113 } 114 } 115