9
9
import org .junit .jupiter .api .Test ;
10
10
import org .w3c .dom .Element ;
11
11
12
- import com .fasterxml .jackson .annotation .JsonFilter ;
13
- import com .fasterxml .jackson .annotation .JsonFormat ;
14
- import com .fasterxml .jackson .annotation .JsonPropertyOrder ;
12
+ import com .fasterxml .jackson .annotation .*;
15
13
16
14
import com .fasterxml .jackson .core .*;
17
15
import com .fasterxml .jackson .core .io .CharacterEscapes ;
25
23
import com .fasterxml .jackson .databind .ser .std .StdDelegatingSerializer ;
26
24
import com .fasterxml .jackson .databind .ser .std .StdScalarSerializer ;
27
25
import com .fasterxml .jackson .databind .testutil .DatabindTestUtil ;
26
+ import com .fasterxml .jackson .databind .type .TypeFactory ;
27
+ import com .fasterxml .jackson .databind .util .Converter ;
28
28
import com .fasterxml .jackson .databind .util .StdConverter ;
29
29
30
30
import static org .junit .jupiter .api .Assertions .*;
@@ -191,6 +191,55 @@ public String getId() {
191
191
}
192
192
}
193
193
194
+ // [databind#4575]
195
+ @ JsonTypeInfo (use = JsonTypeInfo .Id .NAME , property = "@type" )
196
+ @ JsonSubTypes (
197
+ {
198
+ @ JsonSubTypes .Type (Sub4575 .class )
199
+ }
200
+ )
201
+ @ JsonTypeName ("Super" )
202
+ static class Super4575 {
203
+ public static final Super4575 NULL = new Super4575 ();
204
+ }
205
+
206
+ @ JsonTypeName ("Sub" )
207
+ static class Sub4575 extends Super4575 { }
208
+
209
+ static class NullSerializer4575 extends StdDelegatingSerializer {
210
+ public NullSerializer4575 (Converter <Object , ?> converter , JavaType delegateType , JsonSerializer <?> delegateSerializer ) {
211
+ super (converter , delegateType , delegateSerializer );
212
+ }
213
+
214
+ public NullSerializer4575 (TypeFactory typeFactory , JsonSerializer <?> delegateSerializer ) {
215
+ this (
216
+ new Converter <Object , Object >() {
217
+ @ Override
218
+ public Object convert (Object value ) {
219
+ return value == Super4575 .NULL ? null : value ;
220
+ }
221
+
222
+ @ Override
223
+ public JavaType getInputType (TypeFactory typeFactory ) {
224
+ return typeFactory .constructType (delegateSerializer .handledType ());
225
+ }
226
+
227
+ @ Override
228
+ public JavaType getOutputType (TypeFactory typeFactory ) {
229
+ return typeFactory .constructType (delegateSerializer .handledType ());
230
+ }
231
+ },
232
+ typeFactory .constructType (delegateSerializer .handledType () == null ? Object .class : delegateSerializer .handledType ()),
233
+ delegateSerializer
234
+ );
235
+ }
236
+
237
+ @ Override
238
+ protected StdDelegatingSerializer withDelegate (Converter <Object , ?> converter , JavaType delegateType , JsonSerializer <?> delegateSerializer ) {
239
+ return new NullSerializer4575 (converter , delegateType , delegateSerializer );
240
+ }
241
+ }
242
+
194
243
/*
195
244
/**********************************************************
196
245
/* Unit tests
@@ -214,7 +263,6 @@ public void testCustomization() throws Exception
214
263
@ Test
215
264
public void testCustomLists () throws Exception
216
265
{
217
- ObjectMapper mapper = new ObjectMapper ();
218
266
SimpleModule module = new SimpleModule ("test" , Version .unknownVersion ());
219
267
JsonSerializer <?> ser = new CollectionSerializer (null , false , null , null );
220
268
final JsonSerializer <Object > collectionSerializer = (JsonSerializer <Object >) ser ;
@@ -231,15 +279,16 @@ public void serialize(Collection value, JsonGenerator gen, SerializerProvider pr
231
279
}
232
280
}
233
281
});
234
- mapper .registerModule (module );
282
+ ObjectMapper mapper = jsonMapperBuilder ()
283
+ .addModule (module )
284
+ .build ();
235
285
assertEquals ("null" , mapper .writeValueAsString (new ArrayList <Object >()));
236
286
}
237
287
238
288
// [databind#87]: delegating serializer
239
289
@ Test
240
290
public void testDelegating () throws Exception
241
291
{
242
- ObjectMapper mapper = new ObjectMapper ();
243
292
SimpleModule module = new SimpleModule ("test" , Version .unknownVersion ());
244
293
module .addSerializer (new StdDelegatingSerializer (Immutable .class ,
245
294
new StdConverter <Immutable , Map <String ,Integer >>() {
@@ -252,7 +301,9 @@ public Map<String, Integer> convert(Immutable value)
252
301
return map ;
253
302
}
254
303
}));
255
- mapper .registerModule (module );
304
+ ObjectMapper mapper = jsonMapperBuilder ()
305
+ .addModule (module )
306
+ .build ();
256
307
assertEquals ("{\" x\" :3,\" y\" :7}" , mapper .writeValueAsString (new Immutable ()));
257
308
}
258
309
@@ -290,8 +341,9 @@ public void testWithCustomElements() throws Exception
290
341
291
342
SimpleModule module = new SimpleModule ("test" , Version .unknownVersion ());
292
343
module .addSerializer (String .class , new UCStringSerializer ());
293
- ObjectMapper mapper = new ObjectMapper ()
294
- .registerModule (module );
344
+ ObjectMapper mapper = jsonMapperBuilder ()
345
+ .addModule (module )
346
+ .build ();
295
347
296
348
assertEquals (q ("FOOBAR" ), mapper .writeValueAsString ("foobar" ));
297
349
assertEquals (a2q ("['FOO',null]" ),
@@ -318,4 +370,28 @@ public void testIssue2475() throws Exception {
318
370
assertEquals (a2q ("{'id':'ID-2','set':[]}" ),
319
371
writer .writeValueAsString (new Item2475 (new HashSet <String >(), "ID-2" )));
320
372
}
373
+
374
+ @ Test
375
+ public void testIssue4575 () throws Exception {
376
+ com .fasterxml .jackson .databind .Module module = new SimpleModule () {
377
+ {
378
+ setSerializerModifier (new BeanSerializerModifier () {
379
+ @ Override
380
+ public JsonSerializer <?> modifySerializer (
381
+ SerializationConfig config , BeanDescription beanDesc , JsonSerializer <?> serializer
382
+ ) {
383
+ return new NullSerializer4575 (config .getTypeFactory (), serializer );
384
+ }
385
+ });
386
+ }
387
+ };
388
+
389
+ ObjectMapper mapper = jsonMapperBuilder ()
390
+ .addModule (module )
391
+ .build ();
392
+
393
+ assertEquals ("{\" @type\" :\" Super\" }" , mapper .writeValueAsString (new Super4575 ()));
394
+ assertEquals ("{\" @type\" :\" Sub\" }" , mapper .writeValueAsString (new Sub4575 ()));
395
+ assertEquals ("null" , mapper .writeValueAsString (Super4575 .NULL ));
396
+ }
321
397
}
0 commit comments