1515import tools .jackson .databind .util .AccessPattern ;
1616import tools .jackson .dataformat .ion .IonFactory ;
1717import tools .jackson .dataformat .ion .IonObjectMapper ;
18+ import tools .jackson .dataformat .ion .IonReadFeature ;
1819
1920import static org .junit .jupiter .api .Assertions .assertEquals ;
2021import static org .junit .jupiter .api .Assertions .assertNull ;
@@ -73,6 +74,11 @@ static class IonValueData extends Data<IonValue> {
7374 .propertyNamingStrategy (PropertyNamingStrategies .SNAKE_CASE )
7475 .build ();
7576
77+ private static final IonObjectMapper ION_MAPPER_READ_NULL_DISABLED = IonObjectMapper .builder (ionF )
78+ .propertyNamingStrategy (PropertyNamingStrategies .SNAKE_CASE )
79+ .disable (IonReadFeature .READ_NULL_AS_IONVALUE )
80+ .build ();
81+
7682 @ Test
7783 public void shouldBeAbleToDeserialize () throws Exception {
7884 IonValue ion = ion ("{a:1, b:2, c:3}" );
@@ -97,24 +103,48 @@ public void shouldBeAbleToDeserializeIncludingNullList() throws Exception {
97103 assertEquals (ion ("null.list" ), data .getAllData ().get ("c" ));
98104 }
99105
106+ @ Test
107+ public void shouldBeAbleToDeserializeNullToIonNull () throws Exception {
108+ verifyNullDeserialization ("{c:null}" , SYSTEM .newNull (), null );
109+ }
110+
100111 @ Test
101112 public void shouldBeAbleToDeserializeNullList () throws Exception {
102- IonValue ion = ion ("{c:null.list}" );
113+ verifyNullDeserialization ("{c:null.list}" , SYSTEM .newNullList ());
114+ }
103115
104- IonValueData data = ION_VALUE_MAPPER .readValue (ion , IonValueData .class );
105116
106- assertEquals (1 , data .getAllData ().size ());
107- assertEquals (SYSTEM .newNullList (), data .getAllData ().get ("c" ));
108- }
109117
110118 @ Test
111119 public void shouldBeAbleToDeserializeNullStruct () throws Exception {
112- IonValue ion = ion ("{c:null.struct}" );
120+ verifyNullDeserialization ("{c:null.struct}" , SYSTEM .newNullStruct ());
121+ }
113122
114- IonValueData data = ION_VALUE_MAPPER .readValue (ion , IonValueData .class );
123+ @ Test
124+ public void shouldBeAbleToDeserializeNullSexp () throws Exception {
125+ verifyNullDeserialization ("{c:null.sexp}" , SYSTEM .newNullSexp ());
126+ }
127+
128+ private void verifyNullDeserialization (String ionString , IonValue expected ) throws Exception {
129+ verifyNullDeserialization (ionString , expected , expected );
130+ }
131+
132+ private void verifyNullDeserialization (String ionString , IonValue expected , IonValue expectedReadNullDisabled ) throws Exception {
133+ verifyNullDeserialization (ION_VALUE_MAPPER , ionString , expected );
134+ verifyNullDeserialization (ION_MAPPER_READ_NULL_DISABLED , ionString , expectedReadNullDisabled );
135+ }
136+
137+ private void verifyNullDeserialization (IonObjectMapper mapper , String ionString , IonValue expected ) throws Exception {
138+ IonValueData data = mapper .readValue (ionString , IonValueData .class );
115139
116140 assertEquals (1 , data .getAllData ().size ());
117- assertEquals (SYSTEM .newNullStruct (), data .getAllData ().get ("c" ));
141+ assertEquals (expected , data .getAllData ().get ("c" ));
142+
143+ IonValue ion = ion (ionString );
144+ data = mapper .readValue (ion , IonValueData .class );
145+
146+ assertEquals (1 , data .getAllData ().size ());
147+ assertEquals (expected , data .getAllData ().get ("c" ));
118148 }
119149
120150 @ Test
@@ -160,6 +190,27 @@ public void shouldBeAbleToSerializeAndDeserializePojo() throws Exception {
160190 assertEquals (source , result );
161191 }
162192
193+ @ Test
194+ public void shouldBeAbleToSerializeAndDeserializeIonValueDataWithIncludeNonNull () throws Exception {
195+ IonValueData source = new IonValueData ();
196+ source .put ("a" , SYSTEM .newInt (1 ));
197+ source .put ("b" , SYSTEM .newNull ());
198+ source .put ("c" , null );
199+ IonObjectMapper mapper = ION_VALUE_MAPPER
200+ .rebuild ()
201+ .changeDefaultPropertyInclusion (
202+ incl -> incl .withValueInclusion (JsonInclude .Include .NON_NULL )
203+ .withContentInclusion (JsonInclude .Include .NON_NULL ))
204+ .build ();
205+
206+ String data = mapper .writeValueAsString (source );
207+ assertEquals ("{a:1,b:null}" , data );
208+ // Now remove the null element for the comparison below.
209+ source .getAllData ().remove ("c" );
210+ IonValueData result = mapper .readValue (data , IonValueData .class );
211+ assertEquals (source , result );
212+ }
213+
163214 @ Test
164215 public void shouldBeAbleToSerializeAndDeserializeStringData () throws Exception {
165216 StringData source = new StringData ();
@@ -168,7 +219,17 @@ public void shouldBeAbleToSerializeAndDeserializeStringData() throws Exception {
168219
169220 IonValue data = ION_VALUE_MAPPER .writeValueAsIonValue (source );
170221 StringData result = ION_VALUE_MAPPER .readValue (data , StringData .class );
222+ assertEquals (source , result );
223+ }
224+
225+ @ Test
226+ public void shouldBeAbleToSerializeAndDeserializeStringDataAsString () throws Exception {
227+ StringData source = new StringData ();
228+ source .put ("a" , "1" );
229+ source .put ("b" , null );
171230
231+ String data = ION_VALUE_MAPPER .writeValueAsString (source );
232+ StringData result = ION_VALUE_MAPPER .readValue (data , StringData .class );
172233 assertEquals (source , result );
173234 }
174235
0 commit comments