Skip to content
Permalink
Browse files

bug fixed for multi-level gerneric failed, issue 569. #569

  • Loading branch information...
wenshao committed May 11, 2016
1 parent 1de387c commit 8f8540825c95a8cf22636799906d8864366c0658
@@ -33,24 +33,26 @@ public void parseField(DefaultJSONParser parser, Object object, Type objectType,
fieldValueDeserilizer = parser.getConfig().getDeserializer(fieldInfo);
}

Type fieldType = fieldInfo.fieldType;
if (objectType instanceof ParameterizedType) {
ParseContext objContext = parser.getContext();
objContext.type = objectType;
fieldType= FieldInfo.getFieldType(this.clazz, objectType, fieldType);
}

// ContextObjectDeserializer
Object value;
if (fieldValueDeserilizer instanceof JavaBeanDeserializer) {
JavaBeanDeserializer javaBeanDeser = (JavaBeanDeserializer) fieldValueDeserilizer;
value = javaBeanDeser.deserialze(parser, fieldInfo.fieldType, fieldInfo.name, fieldInfo.parserFeatures);
value = javaBeanDeser.deserialze(parser, fieldType, fieldInfo.name, fieldInfo.parserFeatures);
} else {
if (this.fieldInfo.format != null
&& fieldValueDeserilizer instanceof ContextObjectDeserializer
) {
value = ((ContextObjectDeserializer)fieldValueDeserilizer) //
.deserialze(parser, fieldInfo.fieldType, fieldInfo.name, fieldInfo.format, fieldInfo.parserFeatures);
.deserialze(parser, fieldType, fieldInfo.name, fieldInfo.format, fieldInfo.parserFeatures);
} else {
value = fieldValueDeserilizer.deserialze(parser, fieldInfo.fieldType, fieldInfo.name);
value = fieldValueDeserilizer.deserialze(parser, fieldType, fieldInfo.name);
}
}
if (parser.getResolveStatus() == DefaultJSONParser.NeedToResolve) {
@@ -1,5 +1,6 @@
package com.alibaba.fastjson.parser.deserializer;

import java.io.Serializable;
import java.lang.reflect.Array;
import java.lang.reflect.GenericArrayType;
import java.lang.reflect.Type;
@@ -36,6 +37,10 @@
}

}

if (type instanceof Class && type != Object.class && type != Serializable.class) {
return (T) parser.parseObject(type);
}

return (T) parser.parse(fieldName);
}
@@ -185,7 +185,7 @@ public FieldInfo(String name, //
Type genericFieldType = fieldType;

if (!(fieldType instanceof Class)) {
genericFieldType = getFieldType(clazz, type, fieldType);
genericFieldType = getFieldType(clazz, type != null ? type : clazz, fieldType);

if (genericFieldType != fieldType) {
if (genericFieldType instanceof ParameterizedType) {
@@ -272,23 +272,28 @@ public static Type getFieldType(final Class<?> clazz, final Type type, Type fiel
boolean changed = false;
TypeVariable<?>[] typeVariables = null;
Type[] actualTypes = null;
for (int i = 0; i < arguments.length; ++i) {

ParameterizedType paramType = null;
if (type instanceof ParameterizedType) {
paramType = (ParameterizedType) type;
typeVariables = clazz.getTypeParameters();
} else if(clazz.getGenericSuperclass() instanceof ParameterizedType) {
paramType = (ParameterizedType) clazz.getGenericSuperclass();
typeVariables = clazz.getSuperclass().getTypeParameters();
}

for (int i = 0; i < arguments.length && paramType != null; ++i) {
Type feildTypeArguement = arguments[i];
if (feildTypeArguement instanceof TypeVariable) {
TypeVariable<?> typeVar = (TypeVariable<?>) feildTypeArguement;

if (type instanceof ParameterizedType) {
if (typeVariables == null) {
typeVariables = clazz.getTypeParameters();
}
for (int j = 0; j < typeVariables.length; ++j) {
if (typeVariables[j].getName().equals(typeVar.getName())) {
if (actualTypes == null) {
actualTypes = ((ParameterizedType) type).getActualTypeArguments();
}
arguments[i] = actualTypes[j];
changed = true;
for (int j = 0; j < typeVariables.length; ++j) {
if (typeVariables[j].getName().equals(typeVar.getName())) {
if (actualTypes == null) {
actualTypes = paramType.getActualTypeArguments();
}
arguments[i] = actualTypes[j];
changed = true;
}
}
}
@@ -1,51 +1,42 @@
package com.alibaba.json.test.bug;
package com.alibaba.json.bvt.bug;

import org.junit.Assert;

import com.alibaba.fastjson.JSON;
import com.alibaba.json.test.bug.Bug_for_issue_569.LoginResponse.Body;
import com.alibaba.json.test.bug.Bug_for_issue_569.LoginResponse.MemberInfo;
import com.alibaba.json.bvt.bug.Bug_for_issue_569.LoginResponse.Body;
import com.alibaba.json.bvt.bug.Bug_for_issue_569.LoginResponse.MemberInfo;

import junit.framework.TestCase;

public class Bug_for_issue_569 extends TestCase {

public void test_for_issue() throws Exception {
LoginResponse loginResp = new LoginResponse();
loginResp.setResponse(new Response<LoginResponse.Body>());
loginResp.getResponse().setContent(new Body());
loginResp.getResponse().getContent().setMemberinfo(new MemberInfo());
loginResp.getResponse().getContent().getMemberinfo().name = "ding102992";
loginResp.getResponse().getContent().getMemberinfo().email = "ding102992@github.com";
loginResp.response = new Response<LoginResponse.Body>();
loginResp.response.content = new Body();
loginResp.response.content.setMemberinfo(new MemberInfo());
loginResp.response.content.getMemberinfo().name = "ding102992";
loginResp.response.content.getMemberinfo().email = "ding102992@github.com";

String text = JSON.toJSONString(loginResp);

LoginResponse loginResp2 = JSON.parseObject(text, LoginResponse.class);

Assert.assertEquals(loginResp.getResponse() //
Assert.assertEquals(loginResp.response //
.getContent() //
.getMemberinfo().name, //
loginResp2.getResponse() //
loginResp2.response //
.getContent() //
.getMemberinfo().name);
Assert.assertEquals(loginResp.getResponse() //
Assert.assertEquals(loginResp.response //
.getContent().getMemberinfo().email, //
loginResp2.getResponse().getContent().getMemberinfo().email);
loginResp2.response.getContent().getMemberinfo().email);

}

public static class BaseResponse<T> {

private Response<T> response;

public Response<T> getResponse() {
return response;
}

public void setResponse(Response<T> response) {
this.response = response;
}

public Response<T> response;


}
@@ -81,8 +72,8 @@ public void setMemberinfo(MemberInfo memberinfo) {

public static class MemberInfo {

private String name;
private String email;
public String name;
public String email;
/*
* 省略Getter,Setter
*/

0 comments on commit 8f85408

Please sign in to comment.
You can’t perform that action at this time.