Skip to content
This repository has been archived by the owner on Oct 23, 2024. It is now read-only.

fastjson反序列化异常类时丢失信息 #1276

Closed
zackhu135 opened this issue Jun 16, 2017 · 2 comments
Closed

fastjson反序列化异常类时丢失信息 #1276

zackhu135 opened this issue Jun 16, 2017 · 2 comments
Labels
Milestone

Comments

@zackhu135
Copy link

zackhu135 commented Jun 16, 2017

自定义的一些字段并没有被反序列化:
我定义的类内容如下:

public 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 +
                '}';
    }
}

MyException myException = new MyException(100,"error msg");
     str= JSON.toJSONString(myException);
序列化后str的内容为:
{
	"@type": "MyException",
	"code": 100,
	"localizedMessage": "error msg",
	"message": "error msg",
	"stackTrace": [{
		"className": "Main",
		"fileName": "Main.java",
		"lineNumber": 103,
		"methodName": "main",
		"nativeMethod": false
	},
	{
		"className": "sun.reflect.NativeMethodAccessorImpl",
		"fileName": "NativeMethodAccessorImpl.java",
		"lineNumber": -2,
		"methodName": "invoke0",
		"nativeMethod": true
	},
	{
		"className": "sun.reflect.NativeMethodAccessorImpl",
		"fileName": "NativeMethodAccessorImpl.java",
		"lineNumber": 57,
		"methodName": "invoke",
		"nativeMethod": false
	},
	{
		"className": "sun.reflect.DelegatingMethodAccessorImpl",
		"fileName": "DelegatingMethodAccessorImpl.java",
		"lineNumber": 43,
		"methodName": "invoke",
		"nativeMethod": false
	},
	{
		"className": "java.lang.reflect.Method",
		"fileName": "Method.java",
		"lineNumber": 606,
		"methodName": "invoke",
		"nativeMethod": false
	},
	{
		"className": "com.intellij.rt.execution.application.AppMain",
		"fileName": "AppMain.java",
		"lineNumber": 144,
		"methodName": "main",
		"nativeMethod": false
	}],
	"suppressed": []
}

但通过MyException myExceptionJson = JSON.parseObject(str,MyException.class);
反序列化后,得到的myExceptionJson的code字段并没有被反序列化出来。

@kimmking
Copy link
Contributor

追了一下源码,解析的时候把不是标准Exception信息的key-value单独存到了一个HashMap了,然后赋值的时候,没用上。改改就好。

kimmking added a commit to kimmking/fastjson that referenced this issue Jun 16, 2017
@wenshao wenshao added the bug label Jun 18, 2017
@wenshao wenshao added this to the 1.2.34 milestone Jun 18, 2017
kimmking added a commit to kimmking/fastjson that referenced this issue Jun 18, 2017
bug fixed for Exception deserialize. for issue alibaba#1276
@wenshao
Copy link
Member

wenshao commented Jul 2, 2017

https://github.com/alibaba/fastjson/releases/tag/1.2.34 问题已经修复,请使用新版本。

@wenshao wenshao closed this as completed Jul 2, 2017
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

3 participants