Skip to content

Commit 8f5d181

Browse files
committed
fix #109 treat wildcard generics variable as Object
1 parent e51faad commit 8f5d181

File tree

5 files changed

+40
-1
lines changed

5 files changed

+40
-1
lines changed

src/main/java/com/jsoniter/Codegen.java

+3
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import java.io.OutputStreamWriter;
99
import java.lang.reflect.ParameterizedType;
1010
import java.lang.reflect.Type;
11+
import java.lang.reflect.WildcardType;
1112
import java.util.*;
1213

1314
class Codegen {
@@ -127,6 +128,8 @@ private static Type chooseImpl(Type type) {
127128
ParameterizedType pType = (ParameterizedType) type;
128129
clazz = (Class) pType.getRawType();
129130
typeArgs = pType.getActualTypeArguments();
131+
} else if (type instanceof WildcardType) {
132+
clazz = Object.class;
130133
} else {
131134
clazz = (Class) type;
132135
}

src/main/java/com/jsoniter/spi/ClassInfo.java

+4
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import java.lang.reflect.ParameterizedType;
44
import java.lang.reflect.Type;
5+
import java.lang.reflect.WildcardType;
56

67
public class ClassInfo {
78

@@ -15,6 +16,9 @@ public ClassInfo(Type type) {
1516
ParameterizedType pType = (ParameterizedType) type;
1617
clazz = (Class) pType.getRawType();
1718
typeArgs = pType.getActualTypeArguments();
19+
} else if (type instanceof WildcardType) {
20+
clazz = Object.class;
21+
typeArgs = new Type[0];
1822
} else {
1923
clazz = (Class) type;
2024
typeArgs = new Type[0];

src/main/java/com/jsoniter/spi/TypeLiteral.java

+6
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import java.lang.reflect.GenericArrayType;
66
import java.lang.reflect.ParameterizedType;
77
import java.lang.reflect.Type;
8+
import java.lang.reflect.WildcardType;
89
import java.math.BigDecimal;
910
import java.math.BigInteger;
1011
import java.util.HashMap;
@@ -120,6 +121,8 @@ private static String generateCacheKey(Type type, String prefix) {
120121
Type compType = gaType.getGenericComponentType();
121122
decoderClassName.append(formatTypeWithoutSpecialCharacter(compType));
122123
decoderClassName.append("_array");
124+
} else if (type instanceof WildcardType) {
125+
decoderClassName.append("_wildcard");
123126
} else {
124127
throw new UnsupportedOperationException("do not know how to handle: " + type);
125128
}
@@ -144,6 +147,9 @@ private static String formatTypeWithoutSpecialCharacter(Type type) {
144147
GenericArrayType gaType = (GenericArrayType) type;
145148
return formatTypeWithoutSpecialCharacter(gaType.getGenericComponentType()) + "_array";
146149
}
150+
if (type instanceof WildcardType) {
151+
return type.toString();
152+
}
147153
throw new JsonException("unsupported type: " + type + ", of class " + type.getClass());
148154
}
149155

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

+13-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
package com.jsoniter;
22

3-
import com.jsoniter.spi.*;
3+
import com.jsoniter.output.JsonStream;
4+
import com.jsoniter.spi.Binding;
5+
import com.jsoniter.spi.ClassDescriptor;
6+
import com.jsoniter.spi.ClassInfo;
7+
import com.jsoniter.spi.TypeLiteral;
48
import junit.framework.TestCase;
59

610
import java.io.IOException;
@@ -132,4 +136,12 @@ public void test_issue_103() {
132136
assertEquals(User.class, res.results.getClass());
133137
}
134138

139+
public static class TestObject7 {
140+
public List<?> field;
141+
}
142+
143+
public void test_wildcard() throws IOException {
144+
TestObject7 obj = JsonIterator.deserialize("{\"field\":[1]}", TestObject7.class);
145+
assertEquals(Double.valueOf(1), obj.field.get(0));
146+
}
135147
}

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

+14
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
import java.io.ByteArrayOutputStream;
66
import java.io.IOException;
7+
import java.util.ArrayList;
8+
import java.util.List;
79

810
public class TestGenerics extends TestCase {
911
static {
@@ -34,4 +36,16 @@ public void test_inherited_getter_is_not_duplicate() throws IOException {
3436
stream.close();
3537
assertEquals("{\"hello\":0}", baos.toString());
3638
}
39+
40+
public static class TestObject7 {
41+
public List<?> field;
42+
}
43+
44+
public void test_wildcard() throws IOException {
45+
TestObject7 obj = new TestObject7();
46+
ArrayList<Integer> list = new ArrayList<Integer>();
47+
list.add(1);
48+
obj.field = list;
49+
assertEquals("{\"field\":[1]}", JsonStream.serialize(obj));
50+
}
3751
}

0 commit comments

Comments
 (0)