Skip to content

Commit

Permalink
fix #109 treat wildcard generics variable as Object
Browse files Browse the repository at this point in the history
  • Loading branch information
taowen committed Nov 18, 2017
1 parent e51faad commit 8f5d181
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 1 deletion.
3 changes: 3 additions & 0 deletions src/main/java/com/jsoniter/Codegen.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import java.io.OutputStreamWriter;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.lang.reflect.WildcardType;
import java.util.*;

class Codegen {
Expand Down Expand Up @@ -127,6 +128,8 @@ private static Type chooseImpl(Type type) {
ParameterizedType pType = (ParameterizedType) type;
clazz = (Class) pType.getRawType();
typeArgs = pType.getActualTypeArguments();
} else if (type instanceof WildcardType) {
clazz = Object.class;
} else {
clazz = (Class) type;
}
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/com/jsoniter/spi/ClassInfo.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.lang.reflect.WildcardType;

public class ClassInfo {

Expand All @@ -15,6 +16,9 @@ public ClassInfo(Type type) {
ParameterizedType pType = (ParameterizedType) type;
clazz = (Class) pType.getRawType();
typeArgs = pType.getActualTypeArguments();
} else if (type instanceof WildcardType) {
clazz = Object.class;
typeArgs = new Type[0];
} else {
clazz = (Class) type;
typeArgs = new Type[0];
Expand Down
6 changes: 6 additions & 0 deletions src/main/java/com/jsoniter/spi/TypeLiteral.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import java.lang.reflect.GenericArrayType;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.lang.reflect.WildcardType;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.HashMap;
Expand Down Expand Up @@ -120,6 +121,8 @@ private static String generateCacheKey(Type type, String prefix) {
Type compType = gaType.getGenericComponentType();
decoderClassName.append(formatTypeWithoutSpecialCharacter(compType));
decoderClassName.append("_array");
} else if (type instanceof WildcardType) {
decoderClassName.append("_wildcard");
} else {
throw new UnsupportedOperationException("do not know how to handle: " + type);
}
Expand All @@ -144,6 +147,9 @@ private static String formatTypeWithoutSpecialCharacter(Type type) {
GenericArrayType gaType = (GenericArrayType) type;
return formatTypeWithoutSpecialCharacter(gaType.getGenericComponentType()) + "_array";
}
if (type instanceof WildcardType) {
return type.toString();
}
throw new JsonException("unsupported type: " + type + ", of class " + type.getClass());
}

Expand Down
14 changes: 13 additions & 1 deletion src/test/java/com/jsoniter/TestGenerics.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package com.jsoniter;

import com.jsoniter.spi.*;
import com.jsoniter.output.JsonStream;
import com.jsoniter.spi.Binding;
import com.jsoniter.spi.ClassDescriptor;
import com.jsoniter.spi.ClassInfo;
import com.jsoniter.spi.TypeLiteral;
import junit.framework.TestCase;

import java.io.IOException;
Expand Down Expand Up @@ -132,4 +136,12 @@ public void test_issue_103() {
assertEquals(User.class, res.results.getClass());
}

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

public void test_wildcard() throws IOException {
TestObject7 obj = JsonIterator.deserialize("{\"field\":[1]}", TestObject7.class);
assertEquals(Double.valueOf(1), obj.field.get(0));
}
}
14 changes: 14 additions & 0 deletions src/test/java/com/jsoniter/output/TestGenerics.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class TestGenerics extends TestCase {
static {
Expand Down Expand Up @@ -34,4 +36,16 @@ public void test_inherited_getter_is_not_duplicate() throws IOException {
stream.close();
assertEquals("{\"hello\":0}", baos.toString());
}

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

public void test_wildcard() throws IOException {
TestObject7 obj = new TestObject7();
ArrayList<Integer> list = new ArrayList<Integer>();
list.add(1);
obj.field = list;
assertEquals("{\"field\":[1]}", JsonStream.serialize(obj));
}
}

0 comments on commit 8f5d181

Please sign in to comment.