Skip to content

Commit

Permalink
bug fixed for ThrowableDeserializer, fix #3217
Browse files Browse the repository at this point in the history
  • Loading branch information
wenshao committed Apr 5, 2021
1 parent daec680 commit 275da29
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 0 deletions.
Expand Up @@ -12,6 +12,8 @@
import com.alibaba.fastjson.parser.JSONLexer;
import com.alibaba.fastjson.parser.JSONToken;
import com.alibaba.fastjson.parser.ParserConfig;
import com.alibaba.fastjson.util.FieldInfo;
import com.alibaba.fastjson.util.TypeUtils;

public class ThrowableDeserializer extends JavaBeanDeserializer {

Expand Down Expand Up @@ -146,6 +148,10 @@ public <T> T deserialze(DefaultJSONParser parser, Type type, Object fieldName) {

FieldDeserializer fieldDeserializer = exBeanDeser.getFieldDeserializer(key);
if (fieldDeserializer != null) {
FieldInfo fieldInfo = fieldDeserializer.fieldInfo;
if (!fieldInfo.fieldClass.isInstance(value)) {
value = TypeUtils.cast(value, fieldInfo.fieldType, parser.getConfig());
}
fieldDeserializer.setValue(ex, value);
}
}
Expand Down
72 changes: 72 additions & 0 deletions src/test/java/com/alibaba/json/bvt/issue_3200/Issue3217.java
@@ -0,0 +1,72 @@
package com.alibaba.json.bvt.issue_3200;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import junit.framework.TestCase;

public class Issue3217 extends TestCase {
public void testException(){
MyException myException = new MyException();
myException.enumTest = EnumTest.FIRST;
TestClass testClass = new TestClass();
testClass.setMyException(myException);

String jsonString = JSON.toJSONString(testClass, SerializerFeature.NotWriteDefaultValue);
System.out.println(jsonString);

TestClass testClass1 = JSON.parseObject(jsonString, TestClass.class);
System.out.println(testClass1);
}

public static enum EnumTest{
FIRST("111","111"),
SECOND("222","222");
private String key;
private String value;

EnumTest(String key, String value) {
this.key = key;
this.value = value;
}

public String getKey() {
return key;
}

public void setKey(String key) {
this.key = key;
}

public String getValue() {
return value;
}

public void setValue(String value) {
this.value = value;
}
}

public static class MyException extends Exception {
private EnumTest enumTest;

public EnumTest getEnumTest() {
return enumTest;
}

public void setEnumTest(EnumTest enumTest) {
this.enumTest = enumTest;
}
}

public static class TestClass{
private MyException myException;

public MyException getMyException() {
return myException;
}

public void setMyException(MyException myException) {
this.myException = myException;
}
}
}

0 comments on commit 275da29

Please sign in to comment.