Skip to content

Commit 80c1bbb

Browse files
committed
Add a key serializer unit test from #838
1 parent dc53ca4 commit 80c1bbb

File tree

1 file changed

+65
-3
lines changed

1 file changed

+65
-3
lines changed

src/test/java/com/fasterxml/jackson/databind/ser/TestKeySerializers.java

+65-3
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,12 @@
33
import java.io.IOException;
44
import java.util.*;
55

6+
import com.fasterxml.jackson.annotation.*;
67
import com.fasterxml.jackson.core.JsonGenerator;
7-
8+
import com.fasterxml.jackson.core.type.TypeReference;
89
import com.fasterxml.jackson.databind.*;
910
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
11+
import com.fasterxml.jackson.databind.jsontype.TypeResolverBuilder;
1012
import com.fasterxml.jackson.databind.module.SimpleModule;
1113

1214
public class TestKeySerializers extends BaseMapTest
@@ -40,7 +42,7 @@ enum ABC {
4042
A, B, C
4143
}
4244

43-
static class ABCSerializer extends JsonSerializer<ABC> {
45+
static class ABCKeySerializer extends JsonSerializer<ABC> {
4446
@Override
4547
public void serialize(ABC value, JsonGenerator jgen,
4648
SerializerProvider provider) throws IOException {
@@ -55,6 +57,26 @@ public ABCMapWrapper() {
5557
}
5658
}
5759

60+
static class BAR<T>{
61+
T value;
62+
63+
public BAR(T value) {
64+
this.value = value;
65+
}
66+
67+
@JsonValue
68+
public T getValue() {
69+
return value;
70+
}
71+
72+
@Override
73+
public String toString() {
74+
return this.getClass().getSimpleName()
75+
+ ", value:" + value
76+
;
77+
}
78+
}
79+
5880
/*
5981
/**********************************************************
6082
/* Unit tests
@@ -88,10 +110,50 @@ public void testCustomForEnum() throws IOException
88110
{
89111
final ObjectMapper mapper = new ObjectMapper();
90112
SimpleModule mod = new SimpleModule("test");
91-
mod.addKeySerializer(ABC.class, new ABCSerializer());
113+
mod.addKeySerializer(ABC.class, new ABCKeySerializer());
92114
mapper.registerModule(mod);
93115

94116
String json = mapper.writeValueAsString(new ABCMapWrapper());
95117
assertEquals("{\"stuff\":{\"xxxB\":\"bar\"}}", json);
96118
}
119+
120+
// [databind#838]
121+
public void testUnWrappedMapWithDefaultType() throws Exception{
122+
final ObjectMapper mapper = new ObjectMapper();
123+
SimpleModule mod = new SimpleModule("test");
124+
mod.addKeySerializer(ABC.class, new ABCKeySerializer());
125+
mapper.registerModule(mod);
126+
127+
TypeResolverBuilder<?> typer = new ObjectMapper.DefaultTypeResolverBuilder(ObjectMapper.DefaultTyping.NON_FINAL);
128+
typer = typer.init(JsonTypeInfo.Id.NAME, null);
129+
typer = typer.inclusion(JsonTypeInfo.As.PROPERTY);
130+
//typer = typer.typeProperty(TYPE_FIELD);
131+
typer = typer.typeIdVisibility(true);
132+
mapper.setDefaultTyping(typer);
133+
134+
Map<ABC,String> stuff = new HashMap<ABC,String>();
135+
stuff.put(ABC.B, "bar");
136+
String json = mapper.writerFor(new TypeReference<Map<ABC, String>>() {})
137+
.writeValueAsString(stuff);
138+
assertEquals("{\"@type\":\"HashMap\",\"xxxB\":\"bar\"}", json);
139+
}
140+
141+
// [databind#838]
142+
public void testUnWrappedMapWithKeySerializer() throws Exception{
143+
SimpleModule mod = new SimpleModule("test");
144+
mod.addKeySerializer(ABC.class, new ABCKeySerializer());
145+
final ObjectMapper mapper = new ObjectMapper()
146+
.registerModule(mod)
147+
.enable(DeserializationFeature.ACCEPT_EMPTY_STRING_AS_NULL_OBJECT)
148+
.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS)
149+
.disable(SerializationFeature.WRITE_NULL_MAP_VALUES)
150+
.setSerializationInclusion(JsonInclude.Include.NON_EMPTY)
151+
;
152+
153+
Map<ABC,BAR<?>> stuff = new HashMap<ABC,BAR<?>>();
154+
stuff.put(ABC.B, new BAR<String>("bar"));
155+
String json = mapper.writerFor(new TypeReference<Map<ABC,BAR<?>>>() {})
156+
.writeValueAsString(stuff);
157+
assertEquals("{\"xxxB\":\"bar\"}", json);
158+
}
97159
}

0 commit comments

Comments
 (0)