diff --git a/src/test/java/com/fasterxml/jackson/databind/ser/TestKeySerializers.java b/src/test/java/com/fasterxml/jackson/databind/ser/TestKeySerializers.java index ff51f04199..01422543cf 100644 --- a/src/test/java/com/fasterxml/jackson/databind/ser/TestKeySerializers.java +++ b/src/test/java/com/fasterxml/jackson/databind/ser/TestKeySerializers.java @@ -3,10 +3,12 @@ import java.io.IOException; import java.util.*; +import com.fasterxml.jackson.annotation.*; import com.fasterxml.jackson.core.JsonGenerator; - +import com.fasterxml.jackson.core.type.TypeReference; import com.fasterxml.jackson.databind.*; import com.fasterxml.jackson.databind.annotation.JsonSerialize; +import com.fasterxml.jackson.databind.jsontype.TypeResolverBuilder; import com.fasterxml.jackson.databind.module.SimpleModule; public class TestKeySerializers extends BaseMapTest @@ -40,7 +42,7 @@ enum ABC { A, B, C } - static class ABCSerializer extends JsonSerializer { + static class ABCKeySerializer extends JsonSerializer { @Override public void serialize(ABC value, JsonGenerator jgen, SerializerProvider provider) throws IOException { @@ -55,6 +57,26 @@ public ABCMapWrapper() { } } + static class BAR{ + T value; + + public BAR(T value) { + this.value = value; + } + + @JsonValue + public T getValue() { + return value; + } + + @Override + public String toString() { + return this.getClass().getSimpleName() + + ", value:" + value + ; + } + } + /* /********************************************************** /* Unit tests @@ -88,10 +110,50 @@ public void testCustomForEnum() throws IOException { final ObjectMapper mapper = new ObjectMapper(); SimpleModule mod = new SimpleModule("test"); - mod.addKeySerializer(ABC.class, new ABCSerializer()); + mod.addKeySerializer(ABC.class, new ABCKeySerializer()); mapper.registerModule(mod); String json = mapper.writeValueAsString(new ABCMapWrapper()); assertEquals("{\"stuff\":{\"xxxB\":\"bar\"}}", json); } + + // [databind#838] + public void testUnWrappedMapWithDefaultType() throws Exception{ + final ObjectMapper mapper = new ObjectMapper(); + SimpleModule mod = new SimpleModule("test"); + mod.addKeySerializer(ABC.class, new ABCKeySerializer()); + mapper.registerModule(mod); + + TypeResolverBuilder typer = new ObjectMapper.DefaultTypeResolverBuilder(ObjectMapper.DefaultTyping.NON_FINAL); + typer = typer.init(JsonTypeInfo.Id.NAME, null); + typer = typer.inclusion(JsonTypeInfo.As.PROPERTY); + //typer = typer.typeProperty(TYPE_FIELD); + typer = typer.typeIdVisibility(true); + mapper.setDefaultTyping(typer); + + Map stuff = new HashMap(); + stuff.put(ABC.B, "bar"); + String json = mapper.writerFor(new TypeReference>() {}) + .writeValueAsString(stuff); + assertEquals("{\"@type\":\"HashMap\",\"xxxB\":\"bar\"}", json); + } + + // [databind#838] + public void testUnWrappedMapWithKeySerializer() throws Exception{ + SimpleModule mod = new SimpleModule("test"); + mod.addKeySerializer(ABC.class, new ABCKeySerializer()); + final ObjectMapper mapper = new ObjectMapper() + .registerModule(mod) + .enable(DeserializationFeature.ACCEPT_EMPTY_STRING_AS_NULL_OBJECT) + .disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS) + .disable(SerializationFeature.WRITE_NULL_MAP_VALUES) + .setSerializationInclusion(JsonInclude.Include.NON_EMPTY) + ; + + Map> stuff = new HashMap>(); + stuff.put(ABC.B, new BAR("bar")); + String json = mapper.writerFor(new TypeReference>>() {}) + .writeValueAsString(stuff); + assertEquals("{\"xxxB\":\"bar\"}", json); + } }