5
5
import com .fasterxml .jackson .annotation .JsonTypeInfo ;
6
6
import com .fasterxml .jackson .core .*;
7
7
import com .fasterxml .jackson .databind .*;
8
+ import com .fasterxml .jackson .databind .jsontype .TypeDeserializer ;
9
+ import com .fasterxml .jackson .databind .module .SimpleModule ;
8
10
9
11
public class TestTreeWithType extends BaseMapTest
10
12
{
@@ -18,6 +20,36 @@ public Foo(String bar) {
18
20
}
19
21
}
20
22
23
+ // [Issue#353]
24
+ public class SavedCookie {
25
+ public String name , value ;
26
+
27
+ public SavedCookie () { }
28
+ public SavedCookie (String n , String v ) {
29
+ name = n ;
30
+ value = v ;
31
+ }
32
+ }
33
+
34
+ public class SavedCookieDeserializer extends JsonDeserializer <SavedCookie > {
35
+ @ Override
36
+ public SavedCookie deserialize (JsonParser jsonParser , DeserializationContext ctxt )
37
+ throws IOException {
38
+ ObjectCodec oc = jsonParser .getCodec ();
39
+ JsonNode node = oc .readTree (jsonParser );
40
+ return new SavedCookie (node .path ("name" ).textValue (),
41
+ node .path ("value" ).textValue ());
42
+ }
43
+
44
+ @ Override
45
+ public SavedCookie deserializeWithType (JsonParser jp , DeserializationContext ctxt ,
46
+ TypeDeserializer typeDeserializer )
47
+ throws IOException , JsonProcessingException
48
+ {
49
+ return (SavedCookie ) typeDeserializer .deserializeTypedFromObject (jp , ctxt );
50
+ }
51
+ }
52
+
21
53
/*
22
54
/**********************************************************
23
55
/* Unit tests
@@ -73,4 +105,22 @@ public void testValueToTreeWithDefaultTyping() throws Exception {
73
105
JsonNode jsonNode = mapper .valueToTree (foo );
74
106
assertEquals (jsonNode .get ("bar" ).textValue (), foo .bar );
75
107
}
108
+
109
+ public void testIssue353 () throws Exception
110
+ {
111
+ ObjectMapper mapper = new ObjectMapper ();
112
+
113
+ mapper .enableDefaultTypingAsProperty (ObjectMapper .DefaultTyping .NON_FINAL , "@class" );
114
+
115
+ SimpleModule testModule = new SimpleModule ("MyModule" , new Version (1 , 0 , 0 , null , "TEST" , "TEST" ));
116
+ testModule .addDeserializer (SavedCookie .class , new SavedCookieDeserializer ());
117
+ mapper .registerModule (testModule );
118
+
119
+ SavedCookie savedCookie = new SavedCookie ("key" , "v" );
120
+ String json = mapper .writeValueAsString (savedCookie );
121
+ SavedCookie out = mapper .reader (SavedCookie .class ).readValue (json );
122
+
123
+ assertEquals ("key" , out .name );
124
+ assertEquals ("v" , out .value );
125
+ }
76
126
}
0 commit comments