Skip to content

Commit 04cb084

Browse files
committed
Further fixes to #943
1 parent 4a28397 commit 04cb084

File tree

5 files changed

+33
-49
lines changed

5 files changed

+33
-49
lines changed

src/main/java/com/fasterxml/jackson/databind/ser/BasicSerializerFactory.java

-16
Original file line numberDiff line numberDiff line change
@@ -770,22 +770,6 @@ protected JsonSerializer<?> buildMapSerializer(SerializerProvider prov,
770770
if (ser == null) {
771771
ser = findSerializerByAnnotations(prov, type, beanDesc); // (2) Annotations
772772
if (ser == null) {
773-
// 08-Nov-2014, tatu: As per [databind#601], better just use default Map serializer
774-
/*
775-
if (EnumMap.class.isAssignableFrom(type.getRawClass())
776-
&& ((keySerializer == null) || ClassUtil.isJacksonStdImpl(keySerializer))) {
777-
JavaType keyType = type.getKeyType();
778-
// Need to find key enum values...
779-
EnumValues enums = null;
780-
if (keyType.isEnumType()) { // non-enum if we got it as type erased class (from instance)
781-
@SuppressWarnings("unchecked")
782-
Class<Enum<?>> enumClass = (Class<Enum<?>>) keyType.getRawClass();
783-
enums = EnumValues.construct(config, enumClass);
784-
}
785-
ser = new EnumMapSerializer(type.getContentType(), staticTyping, enums,
786-
elementTypeSerializer, elementValueSerializer);
787-
} else {
788-
*/
789773
Object filterId = findFilterId(config, beanDesc);
790774
AnnotationIntrospector ai = config.getAnnotationIntrospector();
791775
MapSerializer mapSer = MapSerializer.construct(ai.findPropertiesToIgnore(beanDesc.getClassInfo(), true),

src/main/java/com/fasterxml/jackson/databind/ser/std/MapSerializer.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -335,7 +335,7 @@ public JsonSerializer<?> createContextual(SerializerProvider provider,
335335
if (ser == null) {
336336
ser = _valueSerializer;
337337
}
338-
// [Issue#124]: May have a content converter
338+
// [databind#124]: May have a content converter
339339
ser = findConvertingContentSerializer(provider, property, ser);
340340
if (ser == null) {
341341
// 30-Sep-2012, tatu: One more thing -- if explicit content type is annotated,
@@ -533,6 +533,7 @@ public void serializeFields(Map<?,?> value, JsonGenerator gen, SerializerProvide
533533
Object valueElem = entry.getValue();
534534
// First, serialize key
535535
Object keyElem = entry.getKey();
536+
536537
if (keyElem == null) {
537538
provider.findNullKeySerializer(_keyType, _property).serialize(null, gen, provider);
538539
} else {

src/main/java/com/fasterxml/jackson/databind/ser/std/StdKeySerializers.java

+9-3
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,8 @@ public static JsonSerializer<Object> getStdKeySerializer(SerializationConfig con
3434
// [databind#943: Use a dynamic key serializer if we are not given actual
3535
// type declaration
3636
if ((rawKeyType == null) || (rawKeyType == Object.class)) {
37-
// !!! TODO
3837
return new Dynamic();
3938
}
40-
4139
if (rawKeyType == String.class) {
4240
return DEFAULT_STRING_SERIALIZER;
4341
}
@@ -69,6 +67,13 @@ public static JsonSerializer<Object> getStdKeySerializer(SerializationConfig con
6967
public static JsonSerializer<Object> getFallbackKeySerializer(SerializationConfig config,
7068
Class<?> rawKeyType) {
7169
if (rawKeyType != null) {
70+
// 29-Sep-2015, tatu: Odd case here, of `Enum`, which we may get for `EnumMap`; not sure
71+
// if that is a bug or feature. Regardless, it seems to require dynamic handling
72+
// (compared to getting actual fully typed Enum).
73+
// Note that this might even work from the earlier point, but let's play it safe for now
74+
if (rawKeyType == Enum.class) {
75+
return new Dynamic();
76+
}
7277
if (rawKeyType.isEnum()) {
7378
return new Default(Default.TYPE_ENUM, rawKeyType);
7479
}
@@ -168,7 +173,8 @@ Object readResolve() {
168173

169174
@Override
170175
public void serialize(Object value, JsonGenerator g, SerializerProvider provider)
171-
throws IOException {
176+
throws IOException
177+
{
172178
Class<?> cls = value.getClass();
173179
PropertySerializerMap m = _dynamicSerializers;
174180
JsonSerializer<Object> ser = m.serializerFor(cls);

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

+6-6
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,11 @@ private EnumWithJsonValue(String n) {
5555
name = n;
5656
}
5757

58-
@JsonValue
5958
@Override
6059
public String toString() { return name; }
60+
61+
@JsonValue
62+
public String external() { return "value:"+name; }
6163
}
6264

6365
protected static interface ToStringMixin {
@@ -254,7 +256,7 @@ public void testSubclassedEnums() throws Exception
254256

255257
public void testEnumsWithJsonValue() throws Exception
256258
{
257-
assertEquals("\"bar\"", MAPPER.writeValueAsString(EnumWithJsonValue.B));
259+
assertEquals("\"value:bar\"", MAPPER.writeValueAsString(EnumWithJsonValue.B));
258260
}
259261

260262
public void testEnumsWithJsonValueUsingMixin() throws Exception
@@ -271,11 +273,9 @@ public void testEnumsWithJsonValueInMap() throws Exception
271273
EnumMap<EnumWithJsonValue,String> input = new EnumMap<EnumWithJsonValue,String>(EnumWithJsonValue.class);
272274
input.put(EnumWithJsonValue.B, "x");
273275
// 24-Sep-2015, tatu: SHOULD actually use annotated method, as per:
274-
// assertEquals("{\"bar\":\"x\"}", MAPPER.writeValueAsString(input));
275-
// but currently (2.6) will just rely on default serialization, via name()
276-
assertEquals("{\"B\":\"x\"}", MAPPER.writeValueAsString(input));
276+
assertEquals("{\"value:bar\":\"x\"}", MAPPER.writeValueAsString(input));
277277
}
278-
278+
279279
/**
280280
* Test for ensuring that @JsonSerializable is used with Enum types as well
281281
* as with any other types.

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

+16-23
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,9 @@
77
import com.fasterxml.jackson.core.JsonGenerator;
88
import com.fasterxml.jackson.core.type.TypeReference;
99
import com.fasterxml.jackson.databind.*;
10-
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
1110
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
1211
import com.fasterxml.jackson.databind.jsontype.TypeResolverBuilder;
1312
import com.fasterxml.jackson.databind.module.SimpleModule;
14-
import com.fasterxml.jackson.databind.module.SimpleSerializers;
15-
import org.junit.Test;
1613

1714
public class TestKeySerializers extends BaseMapTest
1815
{
@@ -56,12 +53,19 @@ public String toLC() {
5653

5754
static class ABCKeySerializer extends JsonSerializer<ABC> {
5855
@Override
59-
public void serialize(ABC value, JsonGenerator jgen,
56+
public void serialize(ABC value, JsonGenerator gen,
6057
SerializerProvider provider) throws IOException {
61-
jgen.writeFieldName("xxx"+value);
58+
gen.writeFieldName("xxx"+value);
6259
}
6360
}
6461

62+
@JsonSerialize(keyUsing = ABCKeySerializer.class)
63+
public static enum ABCMixin { }
64+
65+
public static enum Outer {
66+
inner;
67+
}
68+
6569
static class ABCMapWrapper {
6670
public Map<ABC,String> stuff = new HashMap<ABC,String>();
6771
public ABCMapWrapper() {
@@ -120,7 +124,7 @@ public void testKarl() throws IOException {
120124
assertEquals("{\"map\":{\"Karl\":1}}", serialized);
121125
}
122126

123-
// [Issue#75]: caching of KeySerializers
127+
// [databind#75]: caching of KeySerializers
124128
public void testBoth() throws IOException
125129
{
126130
// Let's NOT use shared one, to ensure caching starts from clean slate
@@ -144,32 +148,21 @@ public void testCustomForEnum() throws IOException
144148
assertEquals("{\"stuff\":{\"xxxB\":\"bar\"}}", json);
145149
}
146150

147-
@JsonSerialize(keyUsing = ABCKeySerializer.class)
148-
public static enum ABCMixin { }
149-
150-
public static enum Outer {
151-
inner;
152-
}
153-
154-
public void testCustomEnumInnerMapKey() {
151+
public void testCustomEnumInnerMapKey() throws Exception {
155152
Map<Outer, Object> outerMap = new HashMap<Outer, Object>();
156153
Map<ABC, Map<String, String>> map = new EnumMap<ABC, Map<String, String>>(ABC.class);
157154
Map<String, String> innerMap = new HashMap<String, String>();
158155
innerMap.put("one", "1");
159156
map.put(ABC.A, innerMap);
160157
outerMap.put(Outer.inner, map);
161158
final ObjectMapper mapper = new ObjectMapper();
162-
SimpleModule mod = new SimpleModule("test") {
163-
@Override
164-
public void setupModule(SetupContext context) {
165-
context.setMixInAnnotations(ABC.class, ABCMixin.class);
166-
SimpleSerializers keySerializers = new SimpleSerializers();
167-
keySerializers.addSerializer(ABC.class, new ABCKeySerializer());
168-
context.addKeySerializers(keySerializers);
169-
}
170-
};
159+
SimpleModule mod = new SimpleModule("test");
160+
mod.setMixInAnnotation(ABC.class, ABCMixin.class);
161+
mod.addKeySerializer(ABC.class, new ABCKeySerializer());
171162
mapper.registerModule(mod);
163+
172164
JsonNode tree = mapper.convertValue(outerMap, JsonNode.class);
165+
173166
JsonNode innerNode = tree.get("inner");
174167
String key = innerNode.fieldNames().next();
175168
assertEquals("xxxA", key);

0 commit comments

Comments
 (0)