1
1
package com .fasterxml .jackson .databind ;
2
2
3
- import java .io .*;
4
3
import java .util .*;
5
4
6
- import com .fasterxml .jackson .annotation .JsonAnyGetter ;
7
- import com .fasterxml .jackson .annotation .JsonAnySetter ;
8
- import com .fasterxml .jackson .annotation .JsonPropertyOrder ;
5
+ import com .fasterxml .jackson .annotation .*;
9
6
10
7
import com .fasterxml .jackson .databind .type .TypeFactory ;
11
8
@@ -59,6 +56,37 @@ public Map<String,Object> properties() {
59
56
}
60
57
}
61
58
59
+
60
+ @ JsonTypeInfo (use = JsonTypeInfo .Id .CLASS )
61
+ @ JsonSubTypes ({@ JsonSubTypes .Type (value = FooClassImpl .class )})
62
+ public class FooClass { }
63
+ class FooClassImpl extends FooClass { }
64
+
65
+ @ JsonTypeInfo (use = JsonTypeInfo .Id .DEDUCTION )
66
+ @ JsonSubTypes ({@ JsonSubTypes .Type (value = FooDeductionImpl .class )})
67
+ public class FooDeduction { }
68
+ class FooDeductionImpl extends FooDeduction { }
69
+
70
+ @ JsonTypeInfo (use = JsonTypeInfo .Id .NONE )
71
+ @ JsonSubTypes ({@ JsonSubTypes .Type (value = FooNoneImpl .class )})
72
+ public class FooNone { }
73
+ class FooNoneImpl extends FooNone { }
74
+
75
+ @ JsonTypeInfo (use = JsonTypeInfo .Id .CUSTOM )
76
+ @ JsonSubTypes ({@ JsonSubTypes .Type (value = FooCustomImpl .class )})
77
+ public class FooCustom { }
78
+ class FooCustomImpl extends FooCustom { }
79
+
80
+ @ JsonTypeInfo (use = JsonTypeInfo .Id .MINIMAL_CLASS )
81
+ @ JsonSubTypes ({@ JsonSubTypes .Type (value = FooMinimalClassImpl .class )})
82
+ public class FooMinimalClass { }
83
+ class FooMinimalClassImpl extends FooMinimalClass { }
84
+
85
+ @ JsonTypeInfo (use = JsonTypeInfo .Id .NAME )
86
+ @ JsonSubTypes ({@ JsonSubTypes .Type (value = FooNameImpl .class )})
87
+ public class FooName { }
88
+ class FooNameImpl extends FooName { }
89
+
62
90
/*
63
91
/**********************************************************
64
92
/* Tests for individual objects
@@ -71,7 +99,7 @@ public Map<String,Object> properties() {
71
99
*/
72
100
private final ObjectMapper MAPPER = newJsonMapper ();
73
101
74
- public void testConfigs () throws IOException
102
+ public void testConfigs () throws Exception
75
103
{
76
104
byte [] base = jdkSerialize (MAPPER .getDeserializationConfig ().getBaseSettings ());
77
105
assertNotNull (jdkDeserialize (base ));
@@ -92,7 +120,7 @@ public void testConfigs() throws IOException
92
120
}
93
121
94
122
// for [databind#899]
95
- public void testEnumHandlers () throws IOException
123
+ public void testEnumHandlers () throws Exception
96
124
{
97
125
ObjectMapper mapper = newJsonMapper ();
98
126
// ensure we have serializers and/or deserializers, first
@@ -125,7 +153,7 @@ public void testEnumHandlers() throws IOException
125
153
assertNotNull (result2 );
126
154
}
127
155
128
- public void testObjectWriter () throws IOException
156
+ public void testObjectWriter () throws Exception
129
157
{
130
158
ObjectWriter origWriter = MAPPER .writer ();
131
159
final String EXP_JSON = "{\" x\" :2,\" y\" :3}" ;
@@ -139,7 +167,7 @@ public void testObjectWriter() throws IOException
139
167
assertEquals (EXP_JSON , writer2 .writeValueAsString (p ));
140
168
}
141
169
142
- public void testObjectReader () throws IOException
170
+ public void testObjectReader () throws Exception
143
171
{
144
172
ObjectReader origReader = MAPPER .readerFor (MyPojo .class );
145
173
String JSON = "{\" x\" :1,\" y\" :2}" ;
@@ -159,7 +187,7 @@ public void testObjectReader() throws IOException
159
187
assertEquals (Integer .valueOf (2 ), any2 .properties ().get ("y" ));
160
188
}
161
189
162
- public void testObjectMapper () throws IOException
190
+ public void testObjectMapper () throws Exception
163
191
{
164
192
final String EXP_JSON = "{\" x\" :2,\" y\" :3}" ;
165
193
final MyPojo p = new MyPojo (2 , 3 );
@@ -191,4 +219,29 @@ public void testTypeFactory() throws Exception
191
219
t = orig .constructType (JavaType .class );
192
220
assertEquals (JavaType .class , t .getRawClass ());
193
221
}
222
+
223
+ // [databind#4303]
224
+ public void testObjectReaderSerializationWithPolymorphism ()
225
+ throws Exception
226
+ {
227
+ Class <?>[] classes = new Class <?>[] {
228
+ FooClass .class ,
229
+ FooDeduction .class ,
230
+ FooNone .class ,
231
+ FooCustom .class ,
232
+ FooMinimalClass .class ,
233
+ FooName .class
234
+ };
235
+
236
+ for (Class <?> clazz : classes ) {
237
+ // Should be enough to ask for reader for polymorphic type
238
+ // (no need to actually serialize/deserialize)
239
+ ObjectReader reader = newJsonMapper ()
240
+ .readerFor (clazz );
241
+
242
+ byte [] bytes = jdkSerialize (reader );
243
+ ObjectReader result = jdkDeserialize (bytes );
244
+ assertNotNull (result );
245
+ }
246
+ }
194
247
}
0 commit comments