Skip to content

Commit c1f8950

Browse files
committed
fix #181 support enum as map key
1 parent b476680 commit c1f8950

File tree

5 files changed

+50
-2
lines changed

5 files changed

+50
-2
lines changed

src/main/java/com/jsoniter/MapKeyDecoders.java

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,14 @@ private static Decoder createMapKeyDecoder(Type mapKeyType) {
2222
if (String.class == mapKeyType) {
2323
return new StringKeyDecoder();
2424
}
25+
if (mapKeyType instanceof Class && ((Class) mapKeyType).isEnum()) {
26+
return new EnumKeyDecoder((Class) mapKeyType);
27+
}
2528
Decoder decoder = CodegenImplNative.NATIVE_DECODERS.get(mapKeyType);
2629
if (decoder != null) {
2730
return new NumberKeyDecoder(decoder);
2831
}
29-
throw new JsonException("can not encode map key type: " + mapKeyType);
32+
throw new JsonException("can not decode map key type: " + mapKeyType);
3033
}
3134

3235
private static class StringKeyDecoder implements Decoder {
@@ -37,6 +40,20 @@ public Object decode(JsonIterator iter) throws IOException {
3740
}
3841
}
3942

43+
private static class EnumKeyDecoder implements Decoder {
44+
45+
private final Class enumClass;
46+
47+
private EnumKeyDecoder(Class enumClass) {
48+
this.enumClass = enumClass;
49+
}
50+
51+
@Override
52+
public Object decode(JsonIterator iter) throws IOException {
53+
return iter.read(enumClass);
54+
}
55+
}
56+
4057
private static class NumberKeyDecoder implements Decoder {
4158

4259
private final Decoder decoder;

src/main/java/com/jsoniter/output/MapKeyEncoders.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@ private static Encoder createDefaultEncoder(Type mapKeyType) {
2929
if (mapKeyType instanceof WildcardType) {
3030
return new DynamicKeyEncoder();
3131
}
32+
if (mapKeyType instanceof Class && ((Class) mapKeyType).isEnum()) {
33+
return new StringKeyEncoder();
34+
}
3235
Encoder.ReflectionEncoder encoder = CodegenImplNative.NATIVE_ENCODERS.get(mapKeyType);
3336
if (encoder != null) {
3437
return new NumberKeyEncoder(encoder);

src/test/java/com/jsoniter/TestMap.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,18 @@ public void test_integer_key() throws IOException {
4141
}}, map);
4242
}
4343

44+
public static enum EnumKey {
45+
KeyA, KeyB
46+
}
47+
48+
public void test_enum_key() {
49+
Map<EnumKey, Object> map = JsonIterator.deserialize("{\"KeyA\":null}", new TypeLiteral<Map<EnumKey, Object>>() {
50+
});
51+
assertEquals(new HashMap<EnumKey, Object>() {{
52+
put(EnumKey.KeyA, null);
53+
}}, map);
54+
}
55+
4456
public static class TestObject1 {
4557
public int Field;
4658
}

src/test/java/com/jsoniter/output/TestMap.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.jsoniter.output;
22

3+
import com.jsoniter.JsonIterator;
34
import com.jsoniter.spi.Config;
45
import com.jsoniter.spi.Encoder;
56
import com.jsoniter.spi.JsoniterSpi;
@@ -65,6 +66,19 @@ public void test_integer_key() throws IOException {
6566
assertEquals("{\"100\":null}", baos.toString());
6667
}
6768

69+
public static enum EnumKey {
70+
KeyA, KeyB
71+
}
72+
73+
public void test_enum_key() throws IOException {
74+
HashMap<EnumKey, Object> obj = new HashMap<EnumKey, Object>();
75+
obj.put(EnumKey.KeyA, null);
76+
stream.writeVal(new TypeLiteral<Map<EnumKey, Object>>() {
77+
}, obj);
78+
stream.close();
79+
assertEquals("{\"KeyA\":null}", baos.toString());
80+
}
81+
6882
public static class TestObject1 {
6983
public int Field;
7084
}

src/test/java/com/jsoniter/suite/AllTestCases.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import com.jsoniter.TestObject;
99
import com.jsoniter.TestString;
1010
import com.jsoniter.any.TestList;
11+
import com.jsoniter.any.TestLong;
1112
import com.jsoniter.output.*;
1213
import com.jsoniter.output.TestInteger;
1314
import org.junit.runner.RunWith;
@@ -54,6 +55,7 @@
5455
TestStreamBuffer.class,
5556
TestCollection.class,
5657
TestList.class,
57-
TestAnnotationJsonObject.class})
58+
TestAnnotationJsonObject.class,
59+
TestLong.class})
5860
public abstract class AllTestCases {
5961
}

0 commit comments

Comments
 (0)