Skip to content

Commit 9f5b119

Browse files
committed
support dynamic map key
1 parent 05d397c commit 9f5b119

File tree

4 files changed

+28
-3
lines changed

4 files changed

+28
-3
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ public static CodegenResult genMap(String cacheKey, ClassInfo classInfo) {
1313
if (cacheKey.endsWith("__value_not_nullable")) {
1414
isCollectionValueNullable = false;
1515
}
16-
Type keyType = String.class;
16+
Type keyType = Object.class;
1717
Type valueType = Object.class;
1818
if (typeArgs.length == 2) {
1919
keyType = typeArgs[0];

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@ private static Encoder createDefaultEncoder(Type mapKeyType) {
2222
if (mapKeyType == String.class) {
2323
return new StringKeyEncoder();
2424
}
25+
if (mapKeyType == Object.class) {
26+
return new DynamicKeyEncoder();
27+
}
2528
if (mapKeyType instanceof Class) {
2629
if (Number.class.isAssignableFrom((Class<?>) mapKeyType)) {
2730
return new NumberKeyEncoder();
@@ -47,4 +50,17 @@ public void encode(Object obj, JsonStream stream) throws IOException {
4750
stream.write('"');
4851
}
4952
}
53+
54+
private static class DynamicKeyEncoder implements Encoder {
55+
56+
@Override
57+
public void encode(Object obj, JsonStream stream) throws IOException {
58+
Class<?> clazz = obj.getClass();
59+
if (clazz == Object.class) {
60+
throw new JsonException("map key type is Object.class, can not be encoded");
61+
}
62+
Encoder mapKeyEncoder = registerOrGetExisting(clazz);
63+
mapKeyEncoder.encode(obj, stream);
64+
}
65+
}
5066
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ class ReflectionMapEncoder implements Encoder.ReflectionEncoder {
1414
private final Encoder mapKeyEncoder;
1515

1616
public ReflectionMapEncoder(Class clazz, Type[] typeArgs) {
17-
Type keyType = String.class;
17+
Type keyType = Object.class;
1818
Type valueType = Object.class;
1919
if (typeArgs.length == 2) {
2020
keyType = typeArgs[0];

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

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
package com.jsoniter.output;
22

3-
import com.jsoniter.spi.*;
3+
import com.jsoniter.spi.Config;
4+
import com.jsoniter.spi.Encoder;
5+
import com.jsoniter.spi.JsoniterSpi;
6+
import com.jsoniter.spi.TypeLiteral;
47
import junit.framework.TestCase;
58

69
import java.io.ByteArrayOutputStream;
@@ -124,4 +127,10 @@ public void test_int_as_map_key() {
124127
assertEquals("{\"1\":2}", JsonStream.serialize(new TypeLiteral<Map<Integer, Integer>>(){
125128
}, m));
126129
}
130+
131+
public void test_int_obj_as_map_key() {
132+
HashMap<Integer, Integer> m = new HashMap<Integer, Integer>();
133+
m.put(1, 2);
134+
assertEquals("{\"1\":2}", JsonStream.serialize(m));
135+
}
127136
}

0 commit comments

Comments
 (0)