Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix #1500 #1506

Merged
merged 2 commits into from
Oct 4, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,20 @@ public <T> T deserialze(DefaultJSONParser parser, Type type, Object fieldName) {
FieldDeserializer fieldDeserializer = this.getFieldDeserializer(key);
if (fieldDeserializer != null) {
fieldDeserializer.setValue(ex, value);
}else{ // 嵌套execption对象时,拿不到fieldDeserializer
try {
Field field = ex.getClass().getDeclaredField(key);
field.setAccessible(true);
field.set(ex, TypeUtils.cast(value,field.getType(),null));
} catch (NoSuchFieldException e) {
// e.printStackTrace();
// ignore
} catch (IllegalAccessException e) {
// e.printStackTrace();
// ignore
}
}

}

return (T) ex;
Expand Down
63 changes: 63 additions & 0 deletions src/test/java/com/alibaba/json/bvt/issue_1500/Issue1500.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package com.alibaba.json.bvt.issue_1500;

import clojure.lang.Obj;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.parser.ParserConfig;
import com.alibaba.fastjson.serializer.SerializerFeature;
import junit.framework.TestCase;
import org.junit.Assert;

import java.util.HashMap;
import java.util.Map;

public class Issue1500 extends TestCase {


public void test_for_issue() throws Exception {

// test aa
Aa aa = new Aa();
aa.setName("aa");
String jsonAa = JSON.toJSONString(aa);
Aa aa1 = JSON.parseObject(jsonAa, Aa.class);
Assert.assertEquals("aa",aa1.getName());

// test C
C c = new C();
c.setE(aa);
String jsonC = JSON.toJSONString(c, SerializerFeature.WriteClassName);
C c2 = JSON.parseObject(jsonC, C.class);
Assert.assertEquals("Aa",c2.getE().getClass().getSimpleName());
Assert.assertEquals("aa",((Aa)c2.getE()).getName());
}

public static class Aa extends Exception {

public Aa(){
}

private String name;

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}
}
public static class C {

private Exception e;

public Exception getE() {
return e;
}

public void setE(Exception e) {
this.e = e;
}

}

}