1
1
package com .fasterxml .jackson .dataformat .ion .ionvalue ;
2
2
3
- import java .io .IOException ;
4
- import java .util .*;
3
+ import java .util .HashMap ;
4
+ import java .util .Map ;
5
+ import java .util .Objects ;
6
+
7
+ import com .amazon .ion .IonSystem ;
8
+ import com .amazon .ion .IonValue ;
9
+ import com .amazon .ion .IonStruct ;
5
10
6
- import com .amazon .ion .*;
7
- import com .amazon .ion .system .IonSystemBuilder ;
8
11
import org .junit .jupiter .api .Test ;
9
12
10
- import com .fasterxml .jackson .annotation .*;
13
+ import com .amazon .ion .system .IonSystemBuilder ;
14
+ import com .fasterxml .jackson .annotation .JsonAnyGetter ;
15
+ import com .fasterxml .jackson .annotation .JsonAnySetter ;
16
+ import com .fasterxml .jackson .annotation .JsonInclude ;
17
+ import com .fasterxml .jackson .annotation .JsonProperty ;
18
+ import com .fasterxml .jackson .databind .PropertyNamingStrategies ;
11
19
import com .fasterxml .jackson .databind .util .AccessPattern ;
12
20
import com .fasterxml .jackson .dataformat .ion .IonObjectMapper ;
21
+ import com .fasterxml .jackson .dataformat .ion .IonParser ;
13
22
14
- import static com .fasterxml .jackson .databind .PropertyNamingStrategies .SNAKE_CASE ;
15
23
import static org .junit .jupiter .api .Assertions .assertEquals ;
16
24
import static org .junit .jupiter .api .Assertions .assertNull ;
17
25
@@ -65,7 +73,10 @@ static class IonValueData extends Data<IonValue> {
65
73
}
66
74
67
75
private static final IonSystem SYSTEM = IonSystemBuilder .standard ().build ();
68
- private static final IonValueMapper ION_VALUE_MAPPER = new IonValueMapper (SYSTEM , SNAKE_CASE );
76
+ private static final IonValueMapper ION_VALUE_MAPPER
77
+ = new IonValueMapper (SYSTEM , PropertyNamingStrategies .SNAKE_CASE );
78
+ private static final IonValueMapper ION_MAPPER_READ_NULL_DISABLED
79
+ = (IonValueMapper ) new IonValueMapper (SYSTEM , PropertyNamingStrategies .SNAKE_CASE ).disable (IonParser .Feature .READ_NULL_AS_IONVALUE );
69
80
70
81
@ Test
71
82
public void shouldBeAbleToDeserialize () throws Exception {
@@ -91,24 +102,48 @@ public void shouldBeAbleToDeserializeIncludingNullList() throws Exception {
91
102
assertEquals (ion ("null.list" ), data .getAllData ().get ("c" ));
92
103
}
93
104
105
+ @ Test
106
+ public void shouldBeAbleToDeserializeNullToIonNull () throws Exception {
107
+ verifyNullDeserialization ("{c:null}" , SYSTEM .newNull (), null );
108
+ }
109
+
94
110
@ Test
95
111
public void shouldBeAbleToDeserializeNullList () throws Exception {
96
- IonValue ion = ion ("{c:null.list}" );
112
+ verifyNullDeserialization ("{c:null.list}" , SYSTEM .newNullList ());
113
+ }
97
114
98
- IonValueData data = ION_VALUE_MAPPER .readValue (ion , IonValueData .class );
99
115
100
- assertEquals (1 , data .getAllData ().size ());
101
- assertEquals (SYSTEM .newNullList (), data .getAllData ().get ("c" ));
102
- }
103
116
104
117
@ Test
105
118
public void shouldBeAbleToDeserializeNullStruct () throws Exception {
106
- IonValue ion = ion ("{c:null.struct}" );
119
+ verifyNullDeserialization ("{c:null.struct}" , SYSTEM .newNullStruct ());
120
+ }
107
121
108
- IonValueData data = ION_VALUE_MAPPER .readValue (ion , IonValueData .class );
122
+ @ Test
123
+ public void shouldBeAbleToDeserializeNullSexp () throws Exception {
124
+ verifyNullDeserialization ("{c:null.sexp}" , SYSTEM .newNullSexp ());
125
+ }
126
+
127
+ private void verifyNullDeserialization (String ionString , IonValue expected ) throws Exception {
128
+ verifyNullDeserialization (ionString , expected , expected );
129
+ }
130
+
131
+ private void verifyNullDeserialization (String ionString , IonValue expected , IonValue expectedReadNullDisabled ) throws Exception {
132
+ verifyNullDeserialization (ION_VALUE_MAPPER , ionString , expected );
133
+ verifyNullDeserialization (ION_MAPPER_READ_NULL_DISABLED , ionString , expectedReadNullDisabled );
134
+ }
135
+
136
+ private void verifyNullDeserialization (IonValueMapper mapper , String ionString , IonValue expected ) throws Exception {
137
+ IonValueData data = mapper .readValue (ionString , IonValueData .class );
138
+
139
+ assertEquals (1 , data .getAllData ().size ());
140
+ assertEquals (expected , data .getAllData ().get ("c" ));
141
+
142
+ IonValue ion = ion (ionString );
143
+ data = mapper .readValue (ion , IonValueData .class );
109
144
110
145
assertEquals (1 , data .getAllData ().size ());
111
- assertEquals (SYSTEM . newNullStruct () , data .getAllData ().get ("c" ));
146
+ assertEquals (expected , data .getAllData ().get ("c" ));
112
147
}
113
148
114
149
@ Test
@@ -154,6 +189,22 @@ public void shouldBeAbleToSerializeAndDeserializePojo() throws Exception {
154
189
assertEquals (source , result );
155
190
}
156
191
192
+ @ Test
193
+ public void shouldBeAbleToSerializeAndDeserializeIonValueDataWithIncludeNonNull () throws Exception {
194
+ IonValueData source = new IonValueData ();
195
+ source .put ("a" , SYSTEM .newInt (1 ));
196
+ source .put ("b" , SYSTEM .newNull ());
197
+ source .put ("c" , null );
198
+ IonValueMapper mapper = (IonValueMapper ) ION_VALUE_MAPPER .copy ().setSerializationInclusion (JsonInclude .Include .NON_NULL );
199
+
200
+ String data = mapper .writeValueAsString (source );
201
+ assertEquals ("{a:1,b:null}" , data );
202
+ // Now remove the null element for the comparison below.
203
+ source .getAllData ().remove ("c" );
204
+ IonValueData result = mapper .readValue (data , IonValueData .class );
205
+ assertEquals (source , result );
206
+ }
207
+
157
208
@ Test
158
209
public void shouldBeAbleToSerializeAndDeserializeStringData () throws Exception {
159
210
StringData source = new StringData ();
@@ -162,7 +213,17 @@ public void shouldBeAbleToSerializeAndDeserializeStringData() throws Exception {
162
213
163
214
IonValue data = ION_VALUE_MAPPER .writeValueAsIonValue (source );
164
215
StringData result = ION_VALUE_MAPPER .parse (data , StringData .class );
216
+ assertEquals (source , result );
217
+ }
218
+
219
+ @ Test
220
+ public void shouldBeAbleToSerializeAndDeserializeStringDataAsString () throws Exception {
221
+ StringData source = new StringData ();
222
+ source .put ("a" , "1" );
223
+ source .put ("b" , null );
165
224
225
+ String data = ION_VALUE_MAPPER .writeValueAsString (source );
226
+ StringData result = ION_VALUE_MAPPER .readValue (data , StringData .class );
166
227
assertEquals (source , result );
167
228
}
168
229
@@ -180,7 +241,7 @@ static class MyBean {
180
241
}
181
242
182
243
@ Test
183
- public void testWithMissingProperty () throws IOException
244
+ public void testWithMissingProperty () throws Exception
184
245
{
185
246
IonSystem ionSystem = IonSystemBuilder .standard ().build ();
186
247
IonObjectMapper ionObjectMapper = IonObjectMapper .builder (ionSystem )
0 commit comments