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.databind.*;
import com.fasterxml.jackson.databind.module.SimpleModule;
public class TestNullHandling extends BaseMapTest
{
static class FunnyNullDeserializer extends JsonDeserializer<String>
{
@Override
public String deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException {
return "text";
}
@Override
public String getNullValue() { return "funny"; }
}
/*
/**********************************************************
/* Test methods
/**********************************************************
*/
// Test for [JACKSON-643]
public void testCustomRootNulls() throws Exception
{
ObjectMapper mapper = new ObjectMapper();
SimpleModule module = new SimpleModule("test", Version.unknownVersion());
module.addDeserializer(String.class, new FunnyNullDeserializer());
mapper.registerModule(module);
// should get non-default null directly:
String str = mapper.readValue("null", String.class);
assertNotNull(str);
assertEquals("funny", str);
// as well as via ObjectReader
ObjectReader reader = mapper.reader(String.class);
str = reader.readValue("null");
assertNotNull(str);
assertEquals("funny", str);
}
// Test for [#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 [#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 = reader.readValue("{\"key\":null}");
assertNotNull(deser);
assertEquals(1, deser.size());
assertEquals("funny", deser.get("key"));
}
}