4
4
import java .util .*;
5
5
6
6
import com .fasterxml .jackson .annotation .JsonCreator ;
7
+ import com .fasterxml .jackson .annotation .JsonInclude ;
7
8
import com .fasterxml .jackson .annotation .JsonProperty ;
9
+
8
10
import com .fasterxml .jackson .core .type .TypeReference ;
9
11
import com .fasterxml .jackson .databind .ObjectMapper ;
10
12
@@ -43,8 +45,22 @@ public int hashCode() {
43
45
44
46
static class OptionalStringBean {
45
47
public Optional <String > value ;
48
+
49
+ public OptionalStringBean () { }
50
+ OptionalStringBean (String str ) {
51
+ value = Optional .ofNullable (str );
52
+ }
46
53
}
47
54
55
+ static class OptionalLongBean {
56
+ public OptionalLong value ;
57
+
58
+ public OptionalLongBean () { value = OptionalLong .empty (); }
59
+ OptionalLongBean (long v ) {
60
+ value = OptionalLong .of (v );
61
+ }
62
+ }
63
+
48
64
// [issue#4]
49
65
static class Issue4Entity {
50
66
private final Optional <String > data ;
@@ -70,13 +86,13 @@ public boolean equals(Object o) {
70
86
}
71
87
}
72
88
73
- private ObjectMapper mapper ;
89
+ private ObjectMapper MAPPER ;
74
90
75
91
@ Override
76
92
protected void setUp () throws Exception
77
93
{
78
94
super .setUp ();
79
- mapper = mapperWithModule ();
95
+ MAPPER = mapperWithModule ();
80
96
}
81
97
82
98
/*
@@ -97,32 +113,32 @@ public void testStringPresent() throws Exception
97
113
98
114
public void testIntAbsent () throws Exception
99
115
{
100
- assertFalse (mapper .readValue (mapper .writeValueAsBytes (OptionalInt .empty ()), OptionalInt .class ).isPresent ());
116
+ assertFalse (MAPPER .readValue (MAPPER .writeValueAsBytes (OptionalInt .empty ()), OptionalInt .class ).isPresent ());
101
117
}
102
118
103
119
public void testIntPresent () throws Exception
104
120
{
105
- assertEquals (5 , mapper .readValue (mapper .writeValueAsBytes (OptionalInt .of (5 )), OptionalInt .class ).getAsInt ());
121
+ assertEquals (5 , MAPPER .readValue (MAPPER .writeValueAsBytes (OptionalInt .of (5 )), OptionalInt .class ).getAsInt ());
106
122
}
107
123
108
124
public void testLongAbsent () throws Exception
109
125
{
110
- assertFalse (mapper .readValue (mapper .writeValueAsBytes (OptionalLong .empty ()), OptionalLong .class ).isPresent ());
126
+ assertFalse (MAPPER .readValue (MAPPER .writeValueAsBytes (OptionalLong .empty ()), OptionalLong .class ).isPresent ());
111
127
}
112
128
113
129
public void testLongPresent () throws Exception
114
130
{
115
- assertEquals (Long .MAX_VALUE , mapper .readValue (mapper .writeValueAsBytes (OptionalLong .of (Long .MAX_VALUE )), OptionalLong .class ).getAsLong ());
131
+ assertEquals (Long .MAX_VALUE , MAPPER .readValue (MAPPER .writeValueAsBytes (OptionalLong .of (Long .MAX_VALUE )), OptionalLong .class ).getAsLong ());
116
132
}
117
133
118
134
public void testDoubleAbsent () throws Exception
119
135
{
120
- assertFalse (mapper .readValue (mapper .writeValueAsBytes (OptionalInt .empty ()), OptionalInt .class ).isPresent ());
136
+ assertFalse (MAPPER .readValue (MAPPER .writeValueAsBytes (OptionalInt .empty ()), OptionalInt .class ).isPresent ());
121
137
}
122
138
123
139
public void testDoublePresent () throws Exception
124
140
{
125
- assertEquals (Double .MIN_VALUE , mapper .readValue (mapper .writeValueAsBytes (OptionalDouble .of (Double .MIN_VALUE )), OptionalDouble .class ).getAsDouble ());
141
+ assertEquals (Double .MIN_VALUE , MAPPER .readValue (MAPPER .writeValueAsBytes (OptionalDouble .of (Double .MIN_VALUE )), OptionalDouble .class ).getAsDouble ());
126
142
}
127
143
128
144
public void testBeanAbsent () throws Exception
@@ -140,9 +156,9 @@ public void testBeanPresent() throws Exception
140
156
public void testBeanWithCreator () throws Exception
141
157
{
142
158
final Issue4Entity emptyEntity = new Issue4Entity (Optional .empty ());
143
- final String json = mapper .writeValueAsString (emptyEntity );
159
+ final String json = MAPPER .writeValueAsString (emptyEntity );
144
160
145
- final Issue4Entity deserialisedEntity = mapper .readValue (json , Issue4Entity .class );
161
+ final Issue4Entity deserialisedEntity = MAPPER .readValue (json , Issue4Entity .class );
146
162
if (!deserialisedEntity .equals (emptyEntity )) {
147
163
throw new IOException ("Entities not equal" );
148
164
}
@@ -151,11 +167,50 @@ public void testBeanWithCreator() throws Exception
151
167
// [issue#4]
152
168
public void testOptionalStringInBean () throws Exception
153
169
{
154
- OptionalStringBean bean = mapper .readValue ("{\" value\" :\" xyz\" }" , OptionalStringBean .class );
170
+ OptionalStringBean bean = MAPPER .readValue ("{\" value\" :\" xyz\" }" , OptionalStringBean .class );
155
171
assertNotNull (bean .value );
156
172
assertEquals ("xyz" , bean .value .get ());
157
173
}
158
174
175
+ // To support [issue#8]
176
+ public void testExcludeIfOptionalAbsent () throws Exception
177
+ {
178
+ ObjectMapper mapper = mapperWithModule ()
179
+ .setSerializationInclusion (JsonInclude .Include .NON_NULL );
180
+ assertEquals (aposToQuotes ("{'value':'foo'}" ),
181
+ mapper .writeValueAsString (new OptionalStringBean ("foo" )));
182
+ // absent is not strictly null so
183
+ assertEquals (aposToQuotes ("{'value':null}" ),
184
+ mapper .writeValueAsString (new OptionalStringBean (null )));
185
+
186
+ // however:
187
+ mapper = mapperWithModule ()
188
+ .setSerializationInclusion (JsonInclude .Include .NON_ABSENT );
189
+ assertEquals (aposToQuotes ("{'value':'foo'}" ),
190
+ mapper .writeValueAsString (new OptionalStringBean ("foo" )));
191
+ assertEquals (aposToQuotes ("{}" ),
192
+ mapper .writeValueAsString (new OptionalStringBean (null )));
193
+ }
194
+
195
+ public void testExcludeIfOptionalLongAbsent () throws Exception
196
+ {
197
+ ObjectMapper mapper = mapperWithModule ()
198
+ .setSerializationInclusion (JsonInclude .Include .NON_NULL );
199
+ assertEquals (aposToQuotes ("{'value':123}" ),
200
+ mapper .writeValueAsString (new OptionalLongBean (123L )));
201
+ // absent is not strictly null so
202
+ assertEquals (aposToQuotes ("{'value':null}" ),
203
+ mapper .writeValueAsString (new OptionalLongBean ()));
204
+
205
+ // however:
206
+ mapper = mapperWithModule ()
207
+ .setSerializationInclusion (JsonInclude .Include .NON_ABSENT );
208
+ assertEquals (aposToQuotes ("{'value':456}" ),
209
+ mapper .writeValueAsString (new OptionalLongBean (456L )));
210
+ assertEquals (aposToQuotes ("{}" ),
211
+ mapper .writeValueAsString (new OptionalLongBean ()));
212
+ }
213
+
159
214
/*
160
215
/**********************************************************
161
216
/* Helper methods
@@ -164,6 +219,6 @@ public void testOptionalStringInBean() throws Exception
164
219
165
220
private <T > Optional <T > roundtrip (Optional <T > obj , TypeReference <Optional <T >> type ) throws IOException
166
221
{
167
- return mapper .readValue (mapper .writeValueAsBytes (obj ), type );
222
+ return MAPPER .readValue (MAPPER .writeValueAsBytes (obj ), type );
168
223
}
169
224
}
0 commit comments