8
8
9
9
import org .w3c .dom .Element ;
10
10
11
- import com .fasterxml .jackson .annotation .JsonFilter ;
12
- import com .fasterxml .jackson .annotation .JsonFormat ;
13
- import com .fasterxml .jackson .annotation .JsonPropertyOrder ;
11
+ import com .fasterxml .jackson .annotation .*;
14
12
15
13
import com .fasterxml .jackson .core .*;
16
14
import com .fasterxml .jackson .core .io .CharacterEscapes ;
23
21
import com .fasterxml .jackson .databind .ser .std .CollectionSerializer ;
24
22
import com .fasterxml .jackson .databind .ser .std .StdDelegatingSerializer ;
25
23
import com .fasterxml .jackson .databind .ser .std .StdScalarSerializer ;
24
+ import com .fasterxml .jackson .databind .type .TypeFactory ;
25
+ import com .fasterxml .jackson .databind .util .Converter ;
26
26
import com .fasterxml .jackson .databind .util .StdConverter ;
27
27
28
28
/**
@@ -187,6 +187,55 @@ public String getId() {
187
187
}
188
188
}
189
189
190
+ // [databind#4575]
191
+ @ JsonTypeInfo (use = JsonTypeInfo .Id .NAME , property = "@type" )
192
+ @ JsonSubTypes (
193
+ {
194
+ @ JsonSubTypes .Type (Sub4575 .class )
195
+ }
196
+ )
197
+ @ JsonTypeName ("Super" )
198
+ static class Super4575 {
199
+ public static final Super4575 NULL = new Super4575 ();
200
+ }
201
+
202
+ @ JsonTypeName ("Sub" )
203
+ static class Sub4575 extends Super4575 { }
204
+
205
+ static class NullSerializer4575 extends StdDelegatingSerializer {
206
+ public NullSerializer4575 (Converter <Object , ?> converter , JavaType delegateType , JsonSerializer <?> delegateSerializer ) {
207
+ super (converter , delegateType , delegateSerializer );
208
+ }
209
+
210
+ public NullSerializer4575 (TypeFactory typeFactory , JsonSerializer <?> delegateSerializer ) {
211
+ this (
212
+ new Converter <Object , Object >() {
213
+ @ Override
214
+ public Object convert (Object value ) {
215
+ return value == Super4575 .NULL ? null : value ;
216
+ }
217
+
218
+ @ Override
219
+ public JavaType getInputType (TypeFactory typeFactory ) {
220
+ return typeFactory .constructType (delegateSerializer .handledType ());
221
+ }
222
+
223
+ @ Override
224
+ public JavaType getOutputType (TypeFactory typeFactory ) {
225
+ return typeFactory .constructType (delegateSerializer .handledType ());
226
+ }
227
+ },
228
+ typeFactory .constructType (delegateSerializer .handledType () == null ? Object .class : delegateSerializer .handledType ()),
229
+ delegateSerializer
230
+ );
231
+ }
232
+
233
+ @ Override
234
+ protected StdDelegatingSerializer withDelegate (Converter <Object , ?> converter , JavaType delegateType , JsonSerializer <?> delegateSerializer ) {
235
+ return new NullSerializer4575 (converter , delegateType , delegateSerializer );
236
+ }
237
+ }
238
+
190
239
/*
191
240
/**********************************************************
192
241
/* Unit tests
@@ -208,7 +257,6 @@ public void testCustomization() throws Exception
208
257
@ SuppressWarnings ({ "unchecked" , "rawtypes" })
209
258
public void testCustomLists () throws Exception
210
259
{
211
- ObjectMapper mapper = new ObjectMapper ();
212
260
SimpleModule module = new SimpleModule ("test" , Version .unknownVersion ());
213
261
JsonSerializer <?> ser = new CollectionSerializer (null , false , null , null );
214
262
final JsonSerializer <Object > collectionSerializer = (JsonSerializer <Object >) ser ;
@@ -225,14 +273,15 @@ public void serialize(Collection value, JsonGenerator gen, SerializerProvider pr
225
273
}
226
274
}
227
275
});
228
- mapper .registerModule (module );
276
+ ObjectMapper mapper = jsonMapperBuilder ()
277
+ .addModule (module )
278
+ .build ();
229
279
assertEquals ("null" , mapper .writeValueAsString (new ArrayList <Object >()));
230
280
}
231
281
232
282
// [databind#87]: delegating serializer
233
283
public void testDelegating () throws Exception
234
284
{
235
- ObjectMapper mapper = new ObjectMapper ();
236
285
SimpleModule module = new SimpleModule ("test" , Version .unknownVersion ());
237
286
module .addSerializer (new StdDelegatingSerializer (Immutable .class ,
238
287
new StdConverter <Immutable , Map <String ,Integer >>() {
@@ -245,7 +294,9 @@ public Map<String, Integer> convert(Immutable value)
245
294
return map ;
246
295
}
247
296
}));
248
- mapper .registerModule (module );
297
+ ObjectMapper mapper = jsonMapperBuilder ()
298
+ .addModule (module )
299
+ .build ();
249
300
assertEquals ("{\" x\" :3,\" y\" :7}" , mapper .writeValueAsString (new Immutable ()));
250
301
}
251
302
@@ -279,8 +330,9 @@ public void testWithCustomElements() throws Exception
279
330
280
331
SimpleModule module = new SimpleModule ("test" , Version .unknownVersion ());
281
332
module .addSerializer (String .class , new UCStringSerializer ());
282
- ObjectMapper mapper = new ObjectMapper ()
283
- .registerModule (module );
333
+ ObjectMapper mapper = jsonMapperBuilder ()
334
+ .addModule (module )
335
+ .build ();
284
336
285
337
assertEquals (q ("FOOBAR" ), mapper .writeValueAsString ("foobar" ));
286
338
assertEquals (a2q ("['FOO',null]" ),
@@ -306,4 +358,28 @@ public void testIssue2475() throws Exception {
306
358
assertEquals (a2q ("{'id':'ID-2','set':[]}" ),
307
359
writer .writeValueAsString (new Item2475 (new HashSet <String >(), "ID-2" )));
308
360
}
361
+
362
+ // [databind#4575]
363
+ public void testIssue4575 () throws Exception {
364
+ com .fasterxml .jackson .databind .Module module = new SimpleModule () {
365
+ {
366
+ setSerializerModifier (new BeanSerializerModifier () {
367
+ @ Override
368
+ public JsonSerializer <?> modifySerializer (
369
+ SerializationConfig config , BeanDescription beanDesc , JsonSerializer <?> serializer
370
+ ) {
371
+ return new NullSerializer4575 (config .getTypeFactory (), serializer );
372
+ }
373
+ });
374
+ }
375
+ };
376
+
377
+ ObjectMapper mapper = jsonMapperBuilder ()
378
+ .addModule (module )
379
+ .build ();
380
+
381
+ assertEquals ("{\" @type\" :\" Super\" }" , mapper .writeValueAsString (new Super4575 ()));
382
+ assertEquals ("{\" @type\" :\" Sub\" }" , mapper .writeValueAsString (new Sub4575 ()));
383
+ assertEquals ("null" , mapper .writeValueAsString (Super4575 .NULL ));
384
+ }
309
385
}
0 commit comments