Json.net尝试反序列化,从异常继承的类时失败 [英] Json.net fails when trying to deserialize a class that inherits from Exception

查看:293
本文介绍了Json.net尝试反序列化,从异常继承的类时失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个类 SearchError 继承异常,并当过我尝试从一个有效的反序列化JSON我得到了以下异常:

  ISerializable的类型'SearchError'没有一个有效的构造函数。要正确实现ISerializable的一个构造函数的SerializationInfo和参数的StreamingContext应该存在。路径',1号线,81位



我试图实施建议的缺失构造函数,它没。'T的帮助



这是实施建议的构造后级:

 公共类ApiError中:异常
{
[JsonProperty(错误)]
公共字符串错误{搞定;组; }

[JsonProperty(@ HTTP_STATUS_CODE)]
公众诠释HttpStatusCode {搞定;组; }

[JsonProperty(警告)]
公开名单<串GT;警告{搞定;组; }

公共ApiError中(字符串错误,诠释httpStatusCode,列表与LT;字符串>警告):基地(错误)
{
this.Error =错误;
this.HttpStatusCode = httpStatusCode;
this.Warnings =警告;
}

公共ApiError中(System.Runtime.Serialization.SerializationInfo信息,System.Runtime.Serialization.StreamingContext上下文)
:基地(信息,上下文)
{
错误=(字符串)info.GetValue(错误,typeof运算(字符串));
HttpStatusCode =(INT)info.GetValue(@ HTTP_STATUS_CODE的typeof(INT));
警告=(列表<串GT;)info.GetValue(警告的typeof(名单<串GT;));
}
}

现在我得到了以下异常(也json.net代码):

 会员'类名'没有被发现。 



我也试过实施相同的解决方案中的此相关的问题,也得到上述同样的错误。


解决方案

此问题已经在这里找到答案:的 http://stackoverflow.com/a/3423037/504836



添加一个新的构造



<前类=郎-CS prettyprint-覆盖> 公共误差(的SerializationInfo信息,的StreamingContext上下文){}

$ b $ ; b

解决我的问题。



下面的完整代码:



<预类=朗CS prettyprint-覆盖> [Serializable接口]
公共类错误:异常
{

公共字符串的ErrorMessage {搞定;组; }

公共误差(的SerializationInfo信息,的StreamingContext上下文){
如果(信息!= NULL)
this.ErrorMessage = info.GetString(的ErrorMessage);
}
公共覆盖无效GetObjectData使用(的SerializationInfo信息,的StreamingContext上下文)
{
base.GetObjectData(信息,上下文);

如果(信息!= NULL)
info.AddValue(的ErrorMessage,this.ErrorMessage);
}
}


I have a class SearchError that inherits from Exception, and when ever I try to deserialize it from a valid json I get the following exception:

ISerializable type 'SearchError' does not have a valid constructor. To correctly implement ISerializable a constructor that takes SerializationInfo and StreamingContext parameters should be present. Path '', line 1, position 81.

I tried implementing the suggested missing constructor, and it didn't help.

This is the class after implementing the suggested constructor:

public class APIError : Exception
{
    [JsonProperty("error")]
    public string Error { get; set; }

    [JsonProperty("@http_status_code")]
    public int HttpStatusCode { get; set; }

    [JsonProperty("warnings")]
    public List<string> Warnings { get; set; }

    public APIError(string error, int httpStatusCode, List<string> warnings) : base(error)
    {
        this.Error = error;
        this.HttpStatusCode = httpStatusCode;
        this.Warnings = warnings;
    }

    public APIError(System.Runtime.Serialization.SerializationInfo info, System.Runtime.Serialization.StreamingContext context)
        : base(info, context)
    {
        Error = (string)info.GetValue("error", typeof(string));
        HttpStatusCode = (int)info.GetValue("@http_status_code", typeof(int));
        Warnings = (List<string>)info.GetValue("warnings", typeof(List<string>));
    }
}

Now I'm getting the following exception (also in json.net code):

Member 'ClassName' was not found.

I also tried implementing the same solution as in this related question, also got the same error above.

解决方案

This question has been answered here: http://stackoverflow.com/a/3423037/504836

Adding a new constructor

public Error(SerializationInfo info, StreamingContext context){}

solved my problem.

Here complete code:

[Serializable]
public class Error : Exception
{

    public string ErrorMessage { get; set; }

    public Error(SerializationInfo info, StreamingContext context) {
        if (info != null)
            this.ErrorMessage = info.GetString("ErrorMessage");
    }
    public override void GetObjectData(SerializationInfo info,StreamingContext context)
    {
        base.GetObjectData(info, context);

        if (info != null)
            info.AddValue("ErrorMessage", this.ErrorMessage);
    }
}

这篇关于Json.net尝试反序列化,从异常继承的类时失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆