Skip to content

Commit e21a29f

Browse files
committed
fix #154 support map of integer typed key
1 parent bc9681c commit e21a29f

File tree

6 files changed

+16
-5
lines changed

6 files changed

+16
-5
lines changed

src/main/java/com/jsoniter/CodegenAccess.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,11 @@ public static final Slice readSlice(JsonIterator iter) throws IOException {
143143

144144
public static final Object readMapKey(String cacheKey, JsonIterator iter) throws IOException {
145145
Decoder mapKeyDecoder = JsoniterSpi.getMapKeyDecoder(cacheKey);
146-
return mapKeyDecoder.decode(iter);
146+
Object key = mapKeyDecoder.decode(iter);
147+
if (IterImpl.nextToken(iter) != ':') {
148+
throw iter.reportError("readMapKey", "expect :");
149+
}
150+
return key;
147151
}
148152

149153
final static boolean skipWhitespacesWithoutLoadMore(JsonIterator iter) throws IOException {

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,9 @@ private static void genWriteMapKey(CodegenResult ctx, Type keyType, boolean noIn
8181
if (keyType == String.class) {
8282
ctx.append("stream.writeVal((java.lang.String)entry.getKey());");
8383
} else if (CodegenImplNative.NATIVE_ENCODERS.containsKey(keyType)) {
84+
ctx.append("stream.write('\"');");
8485
ctx.append(String.format("stream.writeVal((%s)entry.getKey());", CodegenImplNative.getTypeName(keyType)));
86+
ctx.append("stream.write('\"');");
8587
} else {
8688
String mapCacheKey = JsoniterSpi.getMapKeyEncoderCacheKey(keyType);
8789
ctx.append(String.format("com.jsoniter.output.CodegenAccess.writeMapKey(\"%s\", entry.getKey(), stream);", mapCacheKey));

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
import java.io.IOException;
66
import java.lang.reflect.Type;
7+
import java.lang.reflect.WildcardType;
78

89
class MapKeyEncoders {
910

@@ -25,6 +26,9 @@ private static Encoder createDefaultEncoder(Type mapKeyType) {
2526
if (mapKeyType == Object.class) {
2627
return new DynamicKeyEncoder();
2728
}
29+
if (mapKeyType instanceof WildcardType) {
30+
return new DynamicKeyEncoder();
31+
}
2832
Encoder.ReflectionEncoder encoder = CodegenImplNative.NATIVE_ENCODERS.get(mapKeyType);
2933
if (encoder != null) {
3034
return new NumberKeyEncoder(encoder);

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
package com.jsoniter;
22

33
import com.jsoniter.extra.GsonCompatibilityMode;
4-
import com.jsoniter.spi.*;
4+
import com.jsoniter.spi.Decoder;
5+
import com.jsoniter.spi.JsoniterSpi;
6+
import com.jsoniter.spi.TypeLiteral;
57
import junit.framework.TestCase;
68

79
import java.io.IOException;
@@ -66,5 +68,4 @@ public Object decode(JsonIterator iter) throws IOException {
6668
assertEquals(1, keys.size());
6769
assertEquals(100, keys.get(0).Field);
6870
}
69-
7071
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public void test_inherited_getter_is_not_duplicate() throws IOException {
4141

4242
public static class TestObject7 {
4343
public List<?> field;
44-
public Map<?,?> field2;
44+
public Map<?, ?> field2;
4545
}
4646

4747
public void test_wildcard() throws IOException {

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ public void test_int_as_map_key() {
128128
}, m));
129129
}
130130

131-
public void test_int_obj_as_map_key() {
131+
public void test_object_key() {
132132
HashMap<Integer, Integer> m = new HashMap<Integer, Integer>();
133133
m.put(1, 2);
134134
assertEquals("{\"1\":2}", JsonStream.serialize(m));

0 commit comments

Comments
 (0)