Merge pull request #22 from rachelshu/master

Map ignore null value and null key serializer
This commit is contained in:
Eugen 2014-06-26 00:08:07 +03:00
commit a0a5397699
2 changed files with 98 additions and 0 deletions

View File

@ -0,0 +1,19 @@
package org.baeldung.jackson.dtos;
import java.io.IOException;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;
public class MyDtoNullKeySerializer extends JsonSerializer<Object> {
@Override
public void serialize(final Object value, final JsonGenerator jgen, final SerializerProvider provider) throws IOException, JsonProcessingException {
jgen.writeFieldName("");
}
}

View File

@ -5,9 +5,12 @@ import static org.hamcrest.Matchers.not;
import static org.junit.Assert.assertThat; import static org.junit.Assert.assertThat;
import java.io.IOException; import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import org.baeldung.jackson.dtos.MyDto; import org.baeldung.jackson.dtos.MyDto;
import org.baeldung.jackson.dtos.MyDtoIncludeNonDefault; import org.baeldung.jackson.dtos.MyDtoIncludeNonDefault;
import org.baeldung.jackson.dtos.MyDtoNullKeySerializer;
import org.baeldung.jackson.dtos.MyDtoWithFilter; import org.baeldung.jackson.dtos.MyDtoWithFilter;
import org.baeldung.jackson.dtos.MyMixInForString; import org.baeldung.jackson.dtos.MyMixInForString;
import org.baeldung.jackson.dtos.ignore.MyDtoIgnoreField; import org.baeldung.jackson.dtos.ignore.MyDtoIgnoreField;
@ -20,6 +23,7 @@ import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonParseException; import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.databind.SerializerProvider; import com.fasterxml.jackson.databind.SerializerProvider;
import com.fasterxml.jackson.databind.ser.BeanPropertyWriter; import com.fasterxml.jackson.databind.ser.BeanPropertyWriter;
import com.fasterxml.jackson.databind.ser.FilterProvider; import com.fasterxml.jackson.databind.ser.FilterProvider;
@ -183,4 +187,79 @@ public class JacksonSerializationIgnoreUnitTest {
System.out.println(dtoAsString); System.out.println(dtoAsString);
} }
@Test
public final void givenIgnoringMapNullValue_whenWritingMapObjectWithNullValue_thenIgnored() throws JsonProcessingException {
final ObjectMapper mapper = new ObjectMapper();
mapper.configure(SerializationFeature.WRITE_NULL_MAP_VALUES, false);
final MyDto dtoObject1 = new MyDto();
final Map<String, MyDto> dtoMap = new HashMap<String, MyDto>();
dtoMap.put("dtoObject1", dtoObject1);
dtoMap.put("dtoObject2", null);
final String dtoMapAsString = mapper.writeValueAsString(dtoMap);
assertThat(dtoMapAsString, containsString("dtoObject1"));
assertThat(dtoMapAsString, not(containsString("dtoObject2")));
System.out.println(dtoMapAsString);
}
@Test
public final void givenIgnoringMapValueObjectWithNullField_whenWritingMapValueObjectWithNullField_thenIgnored() throws JsonProcessingException {
final ObjectMapper mapper = new ObjectMapper();
mapper.setSerializationInclusion(Include.NON_NULL);
final MyDto dtoObject = new MyDto();
final Map<String, MyDto> dtoMap = new HashMap<String, MyDto>();
dtoMap.put("dtoObject", dtoObject);
final String dtoMapAsString = mapper.writeValueAsString(dtoMap);
assertThat(dtoMapAsString, containsString("dtoObject"));
assertThat(dtoMapAsString, not(containsString("stringValue")));
System.out.println(dtoMapAsString);
}
@Test
public final void givenAllowingMapObjectWithNullKey_whenWritingMapObjectWithNullKey_thenAllowed() throws JsonProcessingException {
final ObjectMapper mapper = new ObjectMapper();
mapper.getSerializerProvider().setNullKeySerializer(new MyDtoNullKeySerializer());
final MyDto dtoObject = new MyDto();
dtoObject.setStringValue("dtoObjectString");
final Map<String, MyDto> dtoMap = new HashMap<String, MyDto>();
dtoMap.put(null, dtoObject);
final String dtoMapAsString = mapper.writeValueAsString(dtoMap);
assertThat(dtoMapAsString, containsString(""));
assertThat(dtoMapAsString, containsString("dtoObjectString"));
System.out.println(dtoMapAsString);
}
@Test
public final void givenAllowingMapObjectOneNullKey_whenWritingMapObjectWithTwoNullKeys_thenOverride() throws JsonProcessingException {
final ObjectMapper mapper = new ObjectMapper();
mapper.getSerializerProvider().setNullKeySerializer(new MyDtoNullKeySerializer());
final MyDto dtoObject1 = new MyDto();
dtoObject1.setStringValue("dtoObject1String");
final MyDto dtoObject2 = new MyDto();
dtoObject2.setStringValue("dtoObject2String");
final Map<String, MyDto> dtoMap = new HashMap<String, MyDto>();
dtoMap.put(null, dtoObject1);
dtoMap.put(null, dtoObject2);
final String dtoMapAsString = mapper.writeValueAsString(dtoMap);
assertThat(dtoMapAsString, not(containsString("dtoObject1String")));
assertThat(dtoMapAsString, containsString("dtoObject2String"));
System.out.println(dtoMapAsString);
}
} }