|
1 | 1 | package com.fasterxml.jackson.dataformat.ion.ionvalue; |
2 | 2 |
|
| 3 | +import com.amazon.ion.IonList; |
3 | 4 | import com.amazon.ion.IonSystem; |
4 | 5 | import com.amazon.ion.IonValue; |
5 | 6 | import com.amazon.ion.IonStruct; |
6 | 7 | import com.amazon.ion.system.IonSystemBuilder; |
7 | 8 | import com.fasterxml.jackson.annotation.JsonAnyGetter; |
8 | 9 | import com.fasterxml.jackson.annotation.JsonAnySetter; |
| 10 | +import com.fasterxml.jackson.annotation.JsonInclude; |
9 | 11 | import com.fasterxml.jackson.annotation.JsonProperty; |
| 12 | +import com.fasterxml.jackson.databind.ObjectWriter; |
10 | 13 | import com.fasterxml.jackson.databind.util.AccessPattern; |
11 | 14 | import com.fasterxml.jackson.dataformat.ion.IonObjectMapper; |
12 | 15 | import com.fasterxml.jackson.dataformat.ion.IonParser; |
@@ -73,7 +76,8 @@ static class IonValueData extends Data<IonValue> { |
73 | 76 | } |
74 | 77 |
|
75 | 78 | private static final IonSystem SYSTEM = IonSystemBuilder.standard().build(); |
76 | | - private final IonValueMapper ION_VALUE_MAPPER = new IonValueMapper(SYSTEM, SNAKE_CASE); |
| 79 | + private static final IonValueMapper ION_VALUE_MAPPER = new IonValueMapper(SYSTEM, SNAKE_CASE); |
| 80 | + private static final IonValueMapper ION_MAPPER_READ_NULL_DISABLED = (IonValueMapper) new IonValueMapper(SYSTEM, SNAKE_CASE).disable(IonParser.Feature.READ_NULL_AS_IONVALUE); |
77 | 81 |
|
78 | 82 | @Test |
79 | 83 | public void shouldBeAbleToDeserialize() throws Exception { |
@@ -101,45 +105,43 @@ public void shouldBeAbleToDeserializeIncludingNullList() throws Exception { |
101 | 105 |
|
102 | 106 | @Test |
103 | 107 | public void shouldBeAbleToDeserializeNullToIonNull() throws Exception { |
104 | | - String ion = "{c:null}"; |
105 | | - verifyNullDeserialization(ion, SYSTEM.newNull()); |
106 | | - ION_VALUE_MAPPER.disable(IonParser.Feature.READ_NULL_AS_IONVALUE); |
107 | | - verifyNullDeserialization(ion, null); |
| 108 | + verifyNullDeserialization("{c:null}", SYSTEM.newNull(), null); |
108 | 109 | } |
109 | 110 |
|
110 | 111 | @Test |
111 | 112 | public void shouldBeAbleToDeserializeNullList() throws Exception { |
112 | | - String ion = "{c:null.list}"; |
113 | | - verifyNullDeserialization(ion, SYSTEM.newNullList()); |
114 | | - ION_VALUE_MAPPER.disable(IonParser.Feature.READ_NULL_AS_IONVALUE); |
115 | | - verifyNullDeserialization(ion, SYSTEM.newNullList()); |
| 113 | + verifyNullDeserialization("{c:null.list}", SYSTEM.newNullList()); |
116 | 114 | } |
117 | 115 |
|
| 116 | + |
| 117 | + |
118 | 118 | @Test |
119 | 119 | public void shouldBeAbleToDeserializeNullStruct() throws Exception { |
120 | | - String ion = "{c:null.struct}"; |
121 | | - verifyNullDeserialization(ion, SYSTEM.newNullStruct()); |
122 | | - ION_VALUE_MAPPER.disable(IonParser.Feature.READ_NULL_AS_IONVALUE); |
123 | | - verifyNullDeserialization(ion, SYSTEM.newNullStruct()); |
| 120 | + verifyNullDeserialization("{c:null.struct}", SYSTEM.newNullStruct()); |
124 | 121 | } |
125 | 122 |
|
126 | 123 | @Test |
127 | 124 | public void shouldBeAbleToDeserializeNullSexp() throws Exception { |
128 | | - String ion = "{c:null.sexp}"; |
129 | | - verifyNullDeserialization(ion, SYSTEM.newNullSexp()); |
130 | | - ION_VALUE_MAPPER.disable(IonParser.Feature.READ_NULL_AS_IONVALUE); |
131 | | - verifyNullDeserialization(ion, SYSTEM.newNullSexp()); |
| 125 | + verifyNullDeserialization("{c:null.sexp}", SYSTEM.newNullSexp()); |
132 | 126 | } |
133 | 127 |
|
134 | 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 | + } |
135 | 136 |
|
136 | | - IonValueData data = ION_VALUE_MAPPER.readValue(ionString, IonValueData.class); |
| 137 | + private void verifyNullDeserialization(IonValueMapper mapper, String ionString, IonValue expected) throws Exception { |
| 138 | + IonValueData data = mapper.readValue(ionString, IonValueData.class); |
137 | 139 |
|
138 | 140 | assertEquals(1, data.getAllData().size()); |
139 | 141 | assertEquals(expected, data.getAllData().get("c")); |
140 | 142 |
|
141 | 143 | IonValue ion = ion(ionString); |
142 | | - data = ION_VALUE_MAPPER.readValue(ion, IonValueData.class); |
| 144 | + data = mapper.readValue(ion, IonValueData.class); |
143 | 145 |
|
144 | 146 | assertEquals(1, data.getAllData().size()); |
145 | 147 | assertEquals(expected, data.getAllData().get("c")); |
@@ -188,6 +190,22 @@ public void shouldBeAbleToSerializeAndDeserializePojo() throws Exception { |
188 | 190 | assertEquals(source, result); |
189 | 191 | } |
190 | 192 |
|
| 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 | + IonValueMapper mapper = (IonValueMapper) ION_VALUE_MAPPER.copy().setSerializationInclusion(JsonInclude.Include.NON_NULL); |
| 200 | + |
| 201 | + String data = mapper.writeValueAsString(source); |
| 202 | + assertEquals("{a:1,b:null}", data); |
| 203 | + // Now remove the null element for the comparison below. |
| 204 | + source.getAllData().remove("c"); |
| 205 | + IonValueData result = mapper.readValue(data, IonValueData.class); |
| 206 | + assertEquals(source, result); |
| 207 | + } |
| 208 | + |
191 | 209 | @Test |
192 | 210 | public void shouldBeAbleToSerializeAndDeserializeStringData() throws Exception { |
193 | 211 | StringData source = new StringData(); |
|
0 commit comments