11package com .fasterxml .jackson .dataformat .ion .ionvalue ;
22
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 ;
510
6- import com .amazon .ion .*;
7- import com .amazon .ion .system .IonSystemBuilder ;
811import org .junit .jupiter .api .Test ;
912
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 ;
1119import com .fasterxml .jackson .databind .util .AccessPattern ;
1220import com .fasterxml .jackson .dataformat .ion .IonObjectMapper ;
21+ import com .fasterxml .jackson .dataformat .ion .IonParser ;
1322
14- import static com .fasterxml .jackson .databind .PropertyNamingStrategies .SNAKE_CASE ;
1523import static org .junit .jupiter .api .Assertions .assertEquals ;
1624import static org .junit .jupiter .api .Assertions .assertNull ;
1725
@@ -65,7 +73,10 @@ static class IonValueData extends Data<IonValue> {
6573 }
6674
6775 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 );
6980
7081 @ Test
7182 public void shouldBeAbleToDeserialize () throws Exception {
@@ -91,24 +102,48 @@ public void shouldBeAbleToDeserializeIncludingNullList() throws Exception {
91102 assertEquals (ion ("null.list" ), data .getAllData ().get ("c" ));
92103 }
93104
105+ @ Test
106+ public void shouldBeAbleToDeserializeNullToIonNull () throws Exception {
107+ verifyNullDeserialization ("{c:null}" , SYSTEM .newNull (), null );
108+ }
109+
94110 @ Test
95111 public void shouldBeAbleToDeserializeNullList () throws Exception {
96- IonValue ion = ion ("{c:null.list}" );
112+ verifyNullDeserialization ("{c:null.list}" , SYSTEM .newNullList ());
113+ }
97114
98- IonValueData data = ION_VALUE_MAPPER .readValue (ion , IonValueData .class );
99115
100- assertEquals (1 , data .getAllData ().size ());
101- assertEquals (SYSTEM .newNullList (), data .getAllData ().get ("c" ));
102- }
103116
104117 @ Test
105118 public void shouldBeAbleToDeserializeNullStruct () throws Exception {
106- IonValue ion = ion ("{c:null.struct}" );
119+ verifyNullDeserialization ("{c:null.struct}" , SYSTEM .newNullStruct ());
120+ }
107121
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 );
109144
110145 assertEquals (1 , data .getAllData ().size ());
111- assertEquals (SYSTEM . newNullStruct () , data .getAllData ().get ("c" ));
146+ assertEquals (expected , data .getAllData ().get ("c" ));
112147 }
113148
114149 @ Test
@@ -154,6 +189,22 @@ public void shouldBeAbleToSerializeAndDeserializePojo() throws Exception {
154189 assertEquals (source , result );
155190 }
156191
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+
157208 @ Test
158209 public void shouldBeAbleToSerializeAndDeserializeStringData () throws Exception {
159210 StringData source = new StringData ();
@@ -162,7 +213,17 @@ public void shouldBeAbleToSerializeAndDeserializeStringData() throws Exception {
162213
163214 IonValue data = ION_VALUE_MAPPER .writeValueAsIonValue (source );
164215 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 );
165224
225+ String data = ION_VALUE_MAPPER .writeValueAsString (source );
226+ StringData result = ION_VALUE_MAPPER .readValue (data , StringData .class );
166227 assertEquals (source , result );
167228 }
168229
@@ -180,7 +241,7 @@ static class MyBean {
180241 }
181242
182243 @ Test
183- public void testWithMissingProperty () throws IOException
244+ public void testWithMissingProperty () throws Exception
184245 {
185246 IonSystem ionSystem = IonSystemBuilder .standard ().build ();
186247 IonObjectMapper ionObjectMapper = IonObjectMapper .builder (ionSystem )
0 commit comments