Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
kimmking committed Jun 16, 2017
1 parent e851149 commit e1ef242
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.alibaba.fastjson.parser.deserializer;

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Type;
import java.util.HashMap;
import java.util.Map;
Expand Down Expand Up @@ -113,6 +114,22 @@ public <T> T deserialze(DefaultJSONParser parser, Type type, Object fieldName) {
ex = createException(message, cause, exClass);
if (ex == null) {
ex = new Exception(message, cause);
}else{
// process otherValues
for(String key : otherValues.keySet()){
try {
Field field = exClass.getDeclaredField(key);
field.setAccessible(true);
Object val = TypeUtils.cast(otherValues.get(key), field.getType(), ParserConfig.getGlobalInstance());
System.out.println(key+":"+val);
field.set(ex,val);
}catch (Exception exception){
// log and ignore
exception.printStackTrace();;
}


}
}
} catch (Exception e) {
throw new JSONException("create instance error", e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ public String process(Object object, String name, Object value) {

}

@JSONType()
public static class User {
private int id;
private String name;
Expand Down
81 changes: 81 additions & 0 deletions src/test/java/com/alibaba/json/bvt/issue_1200/Issue1276.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
package com.alibaba.json.bvt.issue_1200;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.annotation.JSONType;
import com.alibaba.fastjson.serializer.JSONSerializer;
import com.alibaba.fastjson.serializer.NameFilter;
import junit.framework.TestCase;

/**
* Created by kimmking on 15/06/2017.
*/
public class Issue1276 extends TestCase {
public void test_for_issue() throws Exception {

MyException myException = new MyException(100,"error msg");
String str= JSON.toJSONString(myException);
System.out.println(str);

Object me = JSON.parseObject(str, MyException.class);
MyException myExceptionJson = (MyException)me;
assertEquals(myException.code, myExceptionJson.code);

}

public static class MyException extends RuntimeException{
private static final long serialVersionUID = 7815426752583648734L;
private long code;

public MyException() {
super();
}

public MyException(String message, Throwable cause) {
super(message, cause);
}

public MyException(String message) {
super(message);
}

public MyException(Throwable cause) {
super(cause);
}

public MyException(long code) {
super();
this.code = code;
}

public MyException(long code, String message, Throwable cause) {
super(message, cause);
this.code = code;
}

public MyException(long code, String message) {
super(message);
this.code = code;
}

public MyException(long code, Throwable cause) {
super(cause);
this.code = code;
}

public void setCode(long code) {
this.code = code;
}

public long getCode() {
return code;
}

@Override
public String toString() {
return "MyException{" +
"code=" + code +
'}';
}
}

}

0 comments on commit e1ef242

Please sign in to comment.