Skip to content

Commit

Permalink
fix #154 support map of integer typed key
Browse files Browse the repository at this point in the history
  • Loading branch information
taowen committed Feb 24, 2018
1 parent bc9681c commit e21a29f
Show file tree
Hide file tree
Showing 6 changed files with 16 additions and 5 deletions.
6 changes: 5 additions & 1 deletion src/main/java/com/jsoniter/CodegenAccess.java
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,11 @@ public static final Slice readSlice(JsonIterator iter) throws IOException {

public static final Object readMapKey(String cacheKey, JsonIterator iter) throws IOException {
Decoder mapKeyDecoder = JsoniterSpi.getMapKeyDecoder(cacheKey);
return mapKeyDecoder.decode(iter);
Object key = mapKeyDecoder.decode(iter);
if (IterImpl.nextToken(iter) != ':') {
throw iter.reportError("readMapKey", "expect :");
}
return key;
}

final static boolean skipWhitespacesWithoutLoadMore(JsonIterator iter) throws IOException {
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/com/jsoniter/output/CodegenImplMap.java
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,9 @@ private static void genWriteMapKey(CodegenResult ctx, Type keyType, boolean noIn
if (keyType == String.class) {
ctx.append("stream.writeVal((java.lang.String)entry.getKey());");
} else if (CodegenImplNative.NATIVE_ENCODERS.containsKey(keyType)) {
ctx.append("stream.write('\"');");
ctx.append(String.format("stream.writeVal((%s)entry.getKey());", CodegenImplNative.getTypeName(keyType)));
ctx.append("stream.write('\"');");
} else {
String mapCacheKey = JsoniterSpi.getMapKeyEncoderCacheKey(keyType);
ctx.append(String.format("com.jsoniter.output.CodegenAccess.writeMapKey(\"%s\", entry.getKey(), stream);", mapCacheKey));
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/com/jsoniter/output/MapKeyEncoders.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import java.io.IOException;
import java.lang.reflect.Type;
import java.lang.reflect.WildcardType;

class MapKeyEncoders {

Expand All @@ -25,6 +26,9 @@ private static Encoder createDefaultEncoder(Type mapKeyType) {
if (mapKeyType == Object.class) {
return new DynamicKeyEncoder();
}
if (mapKeyType instanceof WildcardType) {
return new DynamicKeyEncoder();
}
Encoder.ReflectionEncoder encoder = CodegenImplNative.NATIVE_ENCODERS.get(mapKeyType);
if (encoder != null) {
return new NumberKeyEncoder(encoder);
Expand Down
5 changes: 3 additions & 2 deletions src/test/java/com/jsoniter/TestMap.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package com.jsoniter;

import com.jsoniter.extra.GsonCompatibilityMode;
import com.jsoniter.spi.*;
import com.jsoniter.spi.Decoder;
import com.jsoniter.spi.JsoniterSpi;
import com.jsoniter.spi.TypeLiteral;
import junit.framework.TestCase;

import java.io.IOException;
Expand Down Expand Up @@ -66,5 +68,4 @@ public Object decode(JsonIterator iter) throws IOException {
assertEquals(1, keys.size());
assertEquals(100, keys.get(0).Field);
}

}
2 changes: 1 addition & 1 deletion src/test/java/com/jsoniter/output/TestGenerics.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public void test_inherited_getter_is_not_duplicate() throws IOException {

public static class TestObject7 {
public List<?> field;
public Map<?,?> field2;
public Map<?, ?> field2;
}

public void test_wildcard() throws IOException {
Expand Down
2 changes: 1 addition & 1 deletion src/test/java/com/jsoniter/output/TestMap.java
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ public void test_int_as_map_key() {
}, m));
}

public void test_int_obj_as_map_key() {
public void test_object_key() {
HashMap<Integer, Integer> m = new HashMap<Integer, Integer>();
m.put(1, 2);
assertEquals("{\"1\":2}", JsonStream.serialize(m));
Expand Down

0 comments on commit e21a29f

Please sign in to comment.