2
2
3
3
import java .io .IOException ;
4
4
import java .util .Arrays ;
5
+ import java .util .HashMap ;
5
6
import java .util .List ;
6
7
import java .util .Map ;
7
8
8
-
9
+ import com . fasterxml . jackson . annotation . JsonAnySetter ;
9
10
import com .fasterxml .jackson .core .*;
10
11
import com .fasterxml .jackson .databind .*;
11
12
import com .fasterxml .jackson .databind .module .SimpleModule ;
@@ -23,12 +24,51 @@ public String deserialize(JsonParser jp, DeserializationContext ctxt) throws IOE
23
24
public String getNullValue (DeserializationContext ctxt ) { return "funny" ; }
24
25
}
25
26
27
+ static class AnySetter {
28
+
29
+ private Map <String ,String > any = new HashMap <String ,String >();
30
+
31
+ @ JsonAnySetter
32
+ public void setAny (String name , String value ){
33
+ this .any .put (name ,value );
34
+ }
35
+
36
+ public Map <String ,String > getAny (){
37
+ return this .any ;
38
+ }
39
+ }
40
+
26
41
/*
27
42
/**********************************************************
28
43
/* Test methods
29
44
/**********************************************************
30
45
*/
31
46
47
+ public void testAnySetterNulls () throws Exception {
48
+ ObjectMapper mapper = new ObjectMapper ();
49
+ SimpleModule module = new SimpleModule ("test" , Version .unknownVersion ());
50
+ module .addDeserializer (String .class , new FunnyNullDeserializer ());
51
+ mapper .registerModule (module );
52
+
53
+ String fieldName = "fieldName" ;
54
+ String nullValue = "{\" " +fieldName +"\" :null}" ;
55
+
56
+ // should get non-default null directly:
57
+ AnySetter result = mapper .readValue (nullValue , AnySetter .class );
58
+
59
+ assertEquals (1 , result .getAny ().size ());
60
+ assertNotNull (result .getAny ().get (fieldName ));
61
+ assertEquals ("funny" , result .getAny ().get (fieldName ));
62
+
63
+ // as well as via ObjectReader
64
+ ObjectReader reader = mapper .readerFor (AnySetter .class );
65
+ result = reader .readValue (nullValue );
66
+
67
+ assertEquals (1 , result .getAny ().size ());
68
+ assertNotNull (result .getAny ().get (fieldName ));
69
+ assertEquals ("funny" , result .getAny ().get (fieldName ));
70
+ }
71
+
32
72
// Test for [JACKSON-643]
33
73
public void testCustomRootNulls () throws Exception
34
74
{
0 commit comments