• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 package com.fasterxml.jackson.databind.ser;
2 
3 import java.io.IOException;
4 
5 import com.fasterxml.jackson.annotation.JsonInclude;
6 import com.fasterxml.jackson.core.JsonGenerator;
7 import com.fasterxml.jackson.databind.*;
8 import com.fasterxml.jackson.databind.annotation.JsonSerialize;
9 
10 public class TestEmptyClass
11     extends BaseMapTest
12 {
13     static class Empty { }
14 
15     @JsonSerialize
16     static class EmptyWithAnno { }
17 
18     // for [JACKSON-695]:
19 
20     @JsonSerialize(using=NonZeroSerializer.class)
21     static class NonZero {
22         public int nr;
23 
NonZero(int i)24         public NonZero(int i) { nr = i; }
25     }
26 
27     @JsonInclude(JsonInclude.Include.NON_EMPTY)
28     static class NonZeroWrapper {
29         public NonZero value;
30 
NonZeroWrapper(int i)31         public NonZeroWrapper(int i) {
32             value = new NonZero(i);
33         }
34     }
35 
36     static class NonZeroSerializer extends JsonSerializer<NonZero>
37     {
38         @Override
serialize(NonZero value, JsonGenerator jgen, SerializerProvider provider)39         public void serialize(NonZero value, JsonGenerator jgen, SerializerProvider provider) throws IOException
40         {
41             jgen.writeNumber(value.nr);
42         }
43 
44         @Override
isEmpty(SerializerProvider provider, NonZero value)45         public boolean isEmpty(SerializerProvider provider, NonZero value) {
46             if (value == null) return true;
47             return (value.nr == 0);
48         }
49     }
50 
51     /*
52     /**********************************************************
53     /* Test methods
54     /**********************************************************
55      */
56 
57     protected final ObjectMapper mapper = new ObjectMapper();
58 
59     /**
60      * Test to check that [JACKSON-201] works if there is a recognized
61      * annotation (which indicates type is serializable)
62      */
testEmptyWithAnnotations()63     public void testEmptyWithAnnotations() throws Exception
64     {
65         // First: without annotations, should complain
66         try {
67             serializeAsString(mapper, new Empty());
68         } catch (JsonMappingException e) {
69             verifyException(e, "No serializer found for class");
70         }
71 
72         // But not if there is a recognized annotation
73         assertEquals("{}", serializeAsString(mapper, new EmptyWithAnno()));
74 
75         // Including class annotation through mix-ins
76         ObjectMapper m2 = new ObjectMapper();
77         m2.addMixIn(Empty.class, EmptyWithAnno.class);
78         assertEquals("{}", m2.writeValueAsString(new Empty()));
79     }
80 
81     /**
82      * Alternative it is possible to use a feature to allow
83      * serializing empty classes, too
84      */
testEmptyWithFeature()85     public void testEmptyWithFeature() throws Exception
86     {
87         // should be enabled by default
88         assertTrue(mapper.getSerializationConfig().isEnabled(SerializationFeature.FAIL_ON_EMPTY_BEANS));
89         mapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
90         assertEquals("{}", serializeAsString(mapper, new Empty()));
91     }
92 
93     // [JACKSON-695], JsonSerializer.isEmpty()
testCustomNoEmpty()94     public void testCustomNoEmpty() throws Exception
95     {
96         // first non-empty:
97         assertEquals("{\"value\":123}", mapper.writeValueAsString(new NonZeroWrapper(123)));
98         // then empty:
99         assertEquals("{}", mapper.writeValueAsString(new NonZeroWrapper(0)));
100     }
101 }
102