5
5
6
6
import com .fasterxml .jackson .annotation .*;
7
7
import com .fasterxml .jackson .core .*;
8
-
9
8
import com .fasterxml .jackson .databind .*;
10
9
import com .fasterxml .jackson .databind .introspect .AnnotatedField ;
11
10
import com .fasterxml .jackson .databind .introspect .POJOPropertyBuilder ;
26
25
@ SuppressWarnings ("serial" )
27
26
public class TestBeanSerializer extends BaseMapTest
28
27
{
29
- /*
30
- /********************************************************
31
- /* Helper types
32
- /********************************************************
33
- */
34
-
35
- static class ModuleImpl extends SimpleModule
28
+ static class SerializerModifierModule extends SimpleModule
36
29
{
37
30
protected BeanSerializerModifier modifier ;
38
31
39
- public ModuleImpl (BeanSerializerModifier modifier )
32
+ public SerializerModifierModule (BeanSerializerModifier modifier )
40
33
{
41
34
super ("test" , Version .unknownVersion ());
42
35
this .modifier = modifier ;
@@ -178,6 +171,24 @@ public List<BeanPropertyWriter> changeProperties(SerializationConfig config,
178
171
}
179
172
}
180
173
174
+ // [Issue#539]: use post-modifier
175
+ static class EmptyBeanModifier539 extends BeanSerializerModifier
176
+ {
177
+ @ Override
178
+ public List <BeanPropertyWriter > changeProperties (SerializationConfig config ,
179
+ BeanDescription beanDesc , List <BeanPropertyWriter > beanProperties )
180
+ {
181
+ System .err .println ("DEBUG: changeProperties!" );
182
+ return beanProperties ;
183
+ }
184
+
185
+ @ Override
186
+ public JsonSerializer <?> modifySerializer (SerializationConfig config ,
187
+ BeanDescription beanDesc , JsonSerializer <?> serializer ) {
188
+ System .err .println ("DEBUG: modifySer!" );
189
+ return new BogusBeanSerializer (42 );
190
+ }
191
+ }
181
192
// [Issue#120], arrays, collections, maps
182
193
183
194
static class ArraySerializerModifier extends BeanSerializerModifier {
@@ -251,30 +262,30 @@ enum EnumABC { A, B, C };
251
262
public void testPropertyRemoval () throws Exception
252
263
{
253
264
ObjectMapper mapper = new ObjectMapper ();
254
- mapper .registerModule (new ModuleImpl (new RemovingModifier ("a" )));
265
+ mapper .registerModule (new SerializerModifierModule (new RemovingModifier ("a" )));
255
266
Bean bean = new Bean ();
256
267
assertEquals ("{\" b\" :\" b\" }" , mapper .writeValueAsString (bean ));
257
268
}
258
269
259
270
public void testPropertyReorder () throws Exception
260
271
{
261
272
ObjectMapper mapper = new ObjectMapper ();
262
- mapper .registerModule (new ModuleImpl (new ReorderingModifier ()));
273
+ mapper .registerModule (new SerializerModifierModule (new ReorderingModifier ()));
263
274
Bean bean = new Bean ();
264
275
assertEquals ("{\" a\" :\" a\" ,\" b\" :\" b\" }" , mapper .writeValueAsString (bean ));
265
276
}
266
277
267
278
public void testBuilderReplacement () throws Exception
268
279
{
269
280
ObjectMapper mapper = new ObjectMapper ();
270
- mapper .registerModule (new ModuleImpl (new BuilderModifier (new BogusBeanSerializer (17 ))));
281
+ mapper .registerModule (new SerializerModifierModule (new BuilderModifier (new BogusBeanSerializer (17 ))));
271
282
Bean bean = new Bean ();
272
283
assertEquals ("17" , mapper .writeValueAsString (bean ));
273
284
}
274
285
public void testSerializerReplacement () throws Exception
275
286
{
276
287
ObjectMapper mapper = new ObjectMapper ();
277
- mapper .registerModule (new ModuleImpl (new ReplacingModifier (new BogusBeanSerializer (123 ))));
288
+ mapper .registerModule (new SerializerModifierModule (new ReplacingModifier (new BogusBeanSerializer (123 ))));
278
289
Bean bean = new Bean ();
279
290
assertEquals ("123" , mapper .writeValueAsString (bean ));
280
291
}
@@ -295,6 +306,22 @@ public void setupModule(SetupContext context)
295
306
assertEquals ("{\" bogus\" :\" foo\" }" , json );
296
307
}
297
308
309
+ // [Issue#539]
310
+ public void testEmptyBean539 () throws Exception
311
+ {
312
+ ObjectMapper mapper = new ObjectMapper ();
313
+ mapper .registerModule (new SimpleModule ("test" , Version .unknownVersion ()) {
314
+ @ Override
315
+ public void setupModule (SetupContext context )
316
+ {
317
+ super .setupModule (context );
318
+ context .addBeanSerializerModifier (new EmptyBeanModifier539 ());
319
+ }
320
+ });
321
+ String json = mapper .writeValueAsString (new EmptyBean ());
322
+ assertEquals ("42" , json );
323
+ }
324
+
298
325
// [Issue#121]
299
326
300
327
public void testModifyArraySerializer () throws Exception
0 commit comments