Skip to content

Commit

Permalink
Merge pull request #407 from wpalmeri/null-deser-cleanup
Browse files Browse the repository at this point in the history
Fix deser of null values for collection/array type deserializers
  • Loading branch information
cowtowncoder committed Feb 26, 2014
2 parents 72ce52f + e602790 commit 5ad9622
Show file tree
Hide file tree
Showing 8 changed files with 64 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ public Collection<Object> deserialize(JsonParser jp, DeserializationContext ctxt
Object value;

if (t == JsonToken.VALUE_NULL) {
value = null;
value = valueDes.getNullValue();
} else if (typeDeser == null) {
value = valueDes.deserialize(jp, ctxt);
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ public Collection<Object> deserialize(JsonParser jp, DeserializationContext ctxt
try {
Object value;
if (t == JsonToken.VALUE_NULL) {
value = null;
value = valueDes.getNullValue();
} else if (typeDeser == null) {
value = valueDes.deserialize(jp, ctxt);
} else {
Expand Down Expand Up @@ -278,7 +278,7 @@ protected final Collection<Object> handleNonArray(JsonParser jp, Deserialization
Object value;

if (t == JsonToken.VALUE_NULL) {
value = null;
value = valueDes.getNullValue();
} else if (typeDeser == null) {
value = valueDes.deserialize(jp, ctxt);
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ public EnumMap<?,?> deserialize(JsonParser jp, DeserializationContext ctxt) thro
Object value;

if (t == JsonToken.VALUE_NULL) {
value = null;
value = valueDes.getNullValue();
} else if (typeDeser == null) {
value = valueDes.deserialize(jp, ctxt);
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -391,7 +391,7 @@ protected final void _readAndBind(JsonParser jp, DeserializationContext ctxt,
// Note: must handle null explicitly here; value deserializers won't
Object value;
if (t == JsonToken.VALUE_NULL) {
value = null;
value = valueDes.getNullValue();
} else if (typeDeser == null) {
value = valueDes.deserialize(jp, ctxt);
} else {
Expand Down Expand Up @@ -445,7 +445,7 @@ protected final void _readAndBindStringMap(JsonParser jp, DeserializationContext
// Note: must handle null explicitly here; value deserializers won't
Object value;
if (t == JsonToken.VALUE_NULL) {
value = null;
value = valueDes.getNullValue();
} else if (typeDeser == null) {
value = valueDes.deserialize(jp, ctxt);
} else {
Expand Down Expand Up @@ -507,7 +507,7 @@ public Map<Object,Object> _deserializeUsingCreator(JsonParser jp, Deserializatio
Object key = _keyDeserializer.deserializeKey(fieldName, ctxt);
Object value;
if (t == JsonToken.VALUE_NULL) {
value = null;
value = valueDes.getNullValue();
} else if (typeDeser == null) {
value = valueDes.deserialize(jp, ctxt);
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ public Object[] deserialize(JsonParser jp, DeserializationContext ctxt)
Object value;

if (t == JsonToken.VALUE_NULL) {
value = null;
value = _elementDeserializer.getNullValue();
} else if (typeDeser == null) {
value = _elementDeserializer.deserialize(jp, ctxt);
} else {
Expand Down Expand Up @@ -225,7 +225,7 @@ private final Object[] handleNonArray(JsonParser jp, DeserializationContext ctxt
Object value;

if (t == JsonToken.VALUE_NULL) {
value = null;
value = _elementDeserializer.getNullValue();
} else if (_elementTypeDeserializer == null) {
value = _elementDeserializer.deserialize(jp, ctxt);
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public String[] deserialize(JsonParser jp, DeserializationContext ctxt) throws I
if (t == JsonToken.VALUE_STRING) {
value = jp.getText();
} else if (t == JsonToken.VALUE_NULL) {
value = null;
value = _elementDeserializer.getNullValue();
} else {
value = _parseString(jp, ctxt);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ private Collection<String> deserializeUsingCustom(JsonParser jp, Deserialization
String value;

if (t == JsonToken.VALUE_NULL) {
value = null;
value = deser.getNullValue();
} else {
value = deser.deserialize(jp, ctxt);
}
Expand Down Expand Up @@ -216,7 +216,7 @@ private final Collection<String> handleNonArray(JsonParser jp, DeserializationCo
String value;

if (t == JsonToken.VALUE_NULL) {
value = null;
value = (valueDes == null) ? null : valueDes.getNullValue();
} else {
value = (valueDes == null) ? _parseString(jp, ctxt) : valueDes.deserialize(jp, ctxt);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
package com.fasterxml.jackson.databind.deser;

import java.io.IOException;
import java.util.Arrays;
import java.util.List;
import java.util.Map;


import com.fasterxml.jackson.core.*;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.*;
import com.fasterxml.jackson.databind.module.SimpleModule;

Expand Down Expand Up @@ -51,4 +55,52 @@ public void testCustomRootNulls() throws Exception
assertNotNull(str);
assertEquals("funny", str);
}

// Test for [jackson-databind-407]
public void testListOfNulls() throws Exception
{
ObjectMapper mapper = new ObjectMapper();
SimpleModule module = new SimpleModule("test", Version.unknownVersion());
module.addDeserializer(String.class, new FunnyNullDeserializer());
mapper.registerModule(module);

List<String> list = Arrays.asList("funny");
JavaType type = mapper.getTypeFactory().constructCollectionType(List.class, String.class);

// should get non-default null directly:
List deser = mapper.readValue("[null]", type);
assertNotNull(deser);
assertEquals(1, deser.size());
assertEquals(list.get(0), deser.get(0));

// as well as via ObjectReader
ObjectReader reader = mapper.reader(type);
deser = reader.readValue("[null]");
assertNotNull(deser);
assertEquals(1, deser.size());
assertEquals(list.get(0), deser.get(0));
}

// Test for [jackson-databind-407]
public void testMapOfNulls() throws Exception
{
ObjectMapper mapper = new ObjectMapper();
SimpleModule module = new SimpleModule("test", Version.unknownVersion());
module.addDeserializer(String.class, new FunnyNullDeserializer());
mapper.registerModule(module);

JavaType type = mapper.getTypeFactory().constructMapType(Map.class, String.class, String.class);
// should get non-default null directly:
Map deser = mapper.readValue("{\"key\":null}", type);
assertNotNull(deser);
assertEquals(1, deser.size());
assertEquals("funny", deser.get("key"));

// as well as via ObjectReader
ObjectReader reader = mapper.reader(type);
deser = mapper.readValue("{\"key\":null}", type);
assertNotNull(deser);
assertEquals(1, deser.size());
assertEquals("funny", deser.get("key"));
}
}

0 comments on commit 5ad9622

Please sign in to comment.