|
5 | 5 | import static org.junit.Assert.assertThat; |
6 | 6 |
|
7 | 7 | import java.io.IOException; |
| 8 | +import java.util.HashMap; |
| 9 | +import java.util.Map; |
8 | 10 |
|
9 | 11 | import org.baeldung.jackson.dtos.MyDto; |
10 | 12 | import org.baeldung.jackson.dtos.MyDtoIncludeNonDefault; |
| 13 | +import org.baeldung.jackson.dtos.MyDtoNullKeySerializer; |
11 | 14 | import org.baeldung.jackson.dtos.MyDtoWithFilter; |
12 | 15 | import org.baeldung.jackson.dtos.MyMixInForString; |
13 | 16 | import org.baeldung.jackson.dtos.ignore.MyDtoIgnoreField; |
|
20 | 23 | import com.fasterxml.jackson.core.JsonParseException; |
21 | 24 | import com.fasterxml.jackson.core.JsonProcessingException; |
22 | 25 | import com.fasterxml.jackson.databind.ObjectMapper; |
| 26 | +import com.fasterxml.jackson.databind.SerializationFeature; |
23 | 27 | import com.fasterxml.jackson.databind.SerializerProvider; |
24 | 28 | import com.fasterxml.jackson.databind.ser.BeanPropertyWriter; |
25 | 29 | import com.fasterxml.jackson.databind.ser.FilterProvider; |
@@ -183,4 +187,79 @@ public final void givenIgnoringNullFieldsGlobally_whenWritingObjectWithNullField |
183 | 187 | System.out.println(dtoAsString); |
184 | 188 | } |
185 | 189 |
|
| 190 | + @Test |
| 191 | + public final void givenIgnoringMapNullValue_whenWritingMapObjectWithNullValue_thenIgnored() throws JsonProcessingException { |
| 192 | + final ObjectMapper mapper = new ObjectMapper(); |
| 193 | + mapper.configure(SerializationFeature.WRITE_NULL_MAP_VALUES, false); |
| 194 | + |
| 195 | + final MyDto dtoObject1 = new MyDto(); |
| 196 | + |
| 197 | + final Map<String, MyDto> dtoMap = new HashMap<String, MyDto>(); |
| 198 | + dtoMap.put("dtoObject1", dtoObject1); |
| 199 | + dtoMap.put("dtoObject2", null); |
| 200 | + |
| 201 | + final String dtoMapAsString = mapper.writeValueAsString(dtoMap); |
| 202 | + |
| 203 | + assertThat(dtoMapAsString, containsString("dtoObject1")); |
| 204 | + assertThat(dtoMapAsString, not(containsString("dtoObject2"))); |
| 205 | + System.out.println(dtoMapAsString); |
| 206 | + } |
| 207 | + |
| 208 | + @Test |
| 209 | + public final void givenIgnoringMapValueObjectWithNullField_whenWritingMapValueObjectWithNullField_thenIgnored() throws JsonProcessingException { |
| 210 | + final ObjectMapper mapper = new ObjectMapper(); |
| 211 | + mapper.setSerializationInclusion(Include.NON_NULL); |
| 212 | + |
| 213 | + final MyDto dtoObject = new MyDto(); |
| 214 | + |
| 215 | + final Map<String, MyDto> dtoMap = new HashMap<String, MyDto>(); |
| 216 | + dtoMap.put("dtoObject", dtoObject); |
| 217 | + |
| 218 | + final String dtoMapAsString = mapper.writeValueAsString(dtoMap); |
| 219 | + |
| 220 | + assertThat(dtoMapAsString, containsString("dtoObject")); |
| 221 | + assertThat(dtoMapAsString, not(containsString("stringValue"))); |
| 222 | + System.out.println(dtoMapAsString); |
| 223 | + } |
| 224 | + |
| 225 | + @Test |
| 226 | + public final void givenAllowingMapObjectWithNullKey_whenWritingMapObjectWithNullKey_thenAllowed() throws JsonProcessingException { |
| 227 | + final ObjectMapper mapper = new ObjectMapper(); |
| 228 | + mapper.getSerializerProvider().setNullKeySerializer(new MyDtoNullKeySerializer()); |
| 229 | + |
| 230 | + final MyDto dtoObject = new MyDto(); |
| 231 | + dtoObject.setStringValue("dtoObjectString"); |
| 232 | + |
| 233 | + final Map<String, MyDto> dtoMap = new HashMap<String, MyDto>(); |
| 234 | + dtoMap.put(null, dtoObject); |
| 235 | + |
| 236 | + final String dtoMapAsString = mapper.writeValueAsString(dtoMap); |
| 237 | + |
| 238 | + assertThat(dtoMapAsString, containsString("")); |
| 239 | + assertThat(dtoMapAsString, containsString("dtoObjectString")); |
| 240 | + System.out.println(dtoMapAsString); |
| 241 | + } |
| 242 | + |
| 243 | + @Test |
| 244 | + public final void givenAllowingMapObjectOneNullKey_whenWritingMapObjectWithTwoNullKeys_thenOverride() throws JsonProcessingException { |
| 245 | + final ObjectMapper mapper = new ObjectMapper(); |
| 246 | + mapper.getSerializerProvider().setNullKeySerializer(new MyDtoNullKeySerializer()); |
| 247 | + |
| 248 | + final MyDto dtoObject1 = new MyDto(); |
| 249 | + dtoObject1.setStringValue("dtoObject1String"); |
| 250 | + |
| 251 | + final MyDto dtoObject2 = new MyDto(); |
| 252 | + dtoObject2.setStringValue("dtoObject2String"); |
| 253 | + |
| 254 | + final Map<String, MyDto> dtoMap = new HashMap<String, MyDto>(); |
| 255 | + dtoMap.put(null, dtoObject1); |
| 256 | + dtoMap.put(null, dtoObject2); |
| 257 | + |
| 258 | + final String dtoMapAsString = mapper.writeValueAsString(dtoMap); |
| 259 | + |
| 260 | + assertThat(dtoMapAsString, not(containsString("dtoObject1String"))); |
| 261 | + assertThat(dtoMapAsString, containsString("dtoObject2String")); |
| 262 | + System.out.println(dtoMapAsString); |
| 263 | + } |
| 264 | + |
186 | 265 | } |
0 commit comments