@@ -117,29 +117,43 @@ static class Issue506DateBean {
117
117
@ JsonTypeInfo (use = Id .NAME , include = As .PROPERTY , property = "type2" )
118
118
public Date date ;
119
119
}
120
-
120
+
121
121
static class Issue506NumberBean
122
122
{
123
123
@ JsonTypeInfo (use = Id .NAME , include = As .PROPERTY , property = "type3" )
124
124
@ JsonSubTypes ({ @ Type (Long .class ),
125
125
@ Type (Integer .class ) })
126
126
public Number number ;
127
127
}
128
-
128
+
129
+ @ JsonTypeInfo (use = JsonTypeInfo .Id .NAME , include = JsonTypeInfo .As .WRAPPER_ARRAY )
130
+ @ JsonSubTypes ({ @ Type (value = Issue1751ArrImpl .class , name = "0" ) })
131
+ static interface Issue1751ArrBase { }
132
+
133
+ static class Issue1751ArrImpl implements Issue1751ArrBase { }
134
+
135
+ @ JsonTypeInfo (use = JsonTypeInfo .Id .NAME , include = JsonTypeInfo .As .PROPERTY ,
136
+ property = "type" )
137
+ @ JsonSubTypes ({ @ Type (value = Issue1751PropImpl .class , name = "1" ) })
138
+ static interface Issue1751PropBase { }
139
+
140
+ static class Issue1751PropImpl implements Issue1751PropBase { }
141
+
129
142
/*
130
143
/**********************************************************
131
144
/* Unit tests
132
145
/**********************************************************
133
146
*/
147
+
148
+ private final ObjectMapper MAPPER = newJsonMapper ();
134
149
135
150
/**
136
151
* First things first, let's ensure we can serialize using
137
152
* class name, written as main-level property name
138
153
*/
139
154
public void testSimpleClassAsProperty () throws Exception
140
155
{
141
- ObjectMapper m = new ObjectMapper ();
142
- Animal a = m .readValue (asJSONObjectValueString ("@classy" , Cat .class .getName (),
156
+ Animal a = MAPPER .readValue (asJSONObjectValueString ("@classy" , Cat .class .getName (),
143
157
"furColor" , "tabby" , "name" , "Garfield" ), Animal .class );
144
158
assertNotNull (a );
145
159
assertEquals (Cat .class , a .getClass ());
@@ -181,7 +195,7 @@ public void testTypeAsArray() throws Exception
181
195
// Use basic Animal as contents of a regular List
182
196
public void testListAsArray () throws Exception
183
197
{
184
- ObjectMapper m = new ObjectMapper () ;
198
+ ObjectMapper m = MAPPER ;
185
199
// This time using PROPERTY style (default) again
186
200
String JSON = "[\n "
187
201
+asJSONObjectValueString (m , "@classy" , Cat .class .getName (), "name" , "Hello" , "furColor" , "white" )
@@ -213,11 +227,10 @@ public void testListAsArray() throws Exception
213
227
214
228
public void testCagedAnimal () throws Exception
215
229
{
216
- ObjectMapper m = new ObjectMapper ();
217
- String jsonCat = asJSONObjectValueString (m , "@classy" , Cat .class .getName (), "name" , "Nilson" , "furColor" , "black" );
230
+ String jsonCat = asJSONObjectValueString (MAPPER , "@classy" , Cat .class .getName (), "name" , "Nilson" , "furColor" , "black" );
218
231
String JSON = "{\" animal\" :" +jsonCat +"}" ;
219
232
220
- AnimalContainer cont = m .readValue (JSON , AnimalContainer .class );
233
+ AnimalContainer cont = MAPPER .readValue (JSON , AnimalContainer .class );
221
234
assertNotNull (cont );
222
235
Animal a = cont .animal ;
223
236
assertNotNull (a );
@@ -232,7 +245,7 @@ public void testCagedAnimal() throws Exception
232
245
*/
233
246
public void testAbstractEmptyBaseClass () throws Exception
234
247
{
235
- DummyBase result = new ObjectMapper () .readValue (
248
+ DummyBase result = MAPPER .readValue (
236
249
"[\" " +DummyImpl .class .getName ()+"\" ,{\" x\" :3}]" , DummyBase .class );
237
250
assertNotNull (result );
238
251
assertEquals (DummyImpl .class , result .getClass ());
@@ -245,10 +258,9 @@ public void testIssue506WithDate() throws Exception
245
258
Issue506DateBean input = new Issue506DateBean ();
246
259
input .date = new Date (1234L );
247
260
248
- ObjectMapper mapper = new ObjectMapper ();
249
- String json = mapper .writeValueAsString (input );
261
+ String json = MAPPER .writeValueAsString (input );
250
262
251
- Issue506DateBean output = mapper .readValue (json , Issue506DateBean .class );
263
+ Issue506DateBean output = MAPPER .readValue (json , Issue506DateBean .class );
252
264
assertEquals (input .date , output .date );
253
265
}
254
266
@@ -258,13 +270,42 @@ public void testIssue506WithNumber() throws Exception
258
270
Issue506NumberBean input = new Issue506NumberBean ();
259
271
input .number = Long .valueOf (4567L );
260
272
261
- ObjectMapper mapper = new ObjectMapper ();
262
- String json = mapper .writeValueAsString (input );
273
+ String json = MAPPER .writeValueAsString (input );
263
274
264
- Issue506NumberBean output = mapper .readValue (json , Issue506NumberBean .class );
275
+ Issue506NumberBean output = MAPPER .readValue (json , Issue506NumberBean .class );
265
276
assertEquals (input .number , output .number );
266
277
}
267
278
279
+ // [databind#1751]: allow ints as ids too
280
+ public void testIntAsTypeId1751Array () throws Exception
281
+ {
282
+ Issue1751ArrBase value ;
283
+
284
+ // Should allow both String and Int:
285
+ value = MAPPER .readValue ("[0, { }]" , Issue1751ArrBase .class );
286
+ assertNotNull (value );
287
+ assertEquals (Issue1751ArrImpl .class , value .getClass ());
288
+
289
+ value = MAPPER .readValue ("[\" 0\" , { }]" , Issue1751ArrBase .class );
290
+ assertNotNull (value );
291
+ assertEquals (Issue1751ArrImpl .class , value .getClass ());
292
+ }
293
+
294
+ // [databind#1751]: allow ints as ids too
295
+ public void testIntAsTypeId1751Prop () throws Exception
296
+ {
297
+ Issue1751PropBase value ;
298
+
299
+ // Should allow both String and Int:
300
+ value = MAPPER .readValue ("{\" type\" : \" 1\" }" , Issue1751PropBase .class );
301
+ assertNotNull (value );
302
+ assertEquals (Issue1751PropImpl .class , value .getClass ());
303
+
304
+ value = MAPPER .readValue ("{\" type\" : 1}" , Issue1751PropBase .class );
305
+ assertNotNull (value );
306
+ assertEquals (Issue1751PropImpl .class , value .getClass ());
307
+ }
308
+
268
309
// [databind#2467]: Allow missing "content" for as-array deserialization
269
310
public void testTypeAsArrayWithNullableType () throws Exception
270
311
{
0 commit comments