反序列化对象时出现意外结束:自定义对象序列化/反序列化失败

时间:2017-11-15 19:54:04

标签: c# json json.net

以下是我的代码:

private void serializationTest()
{
    DataTable dt = new DataTable("Customers");
    dsCustomer.Tables.Add(dt);
    dt.Columns.Add(new DataColumn("ID", typeof(int)));
    dt.Columns.Add(new DataColumn("Name", typeof(string)));
    dt.Columns.Add(new DataColumn("Height", typeof(decimal)));
    dt.Columns.Add(new DataColumn("Weight", typeof(double)));
    DataRow row = dt.NewRow();
    row["ID"] = 1;
    row["Name"] = "Susan";
    row["Height"] = 15.2m;
    row["Weight"] = 16.777;
    dt.Rows.Add(row);

    row = dt.NewRow();
    row["ID"] = 2;
    row["Name"] = "Bill";
    row["Height"] = 215.2m;
    row["Weight"] = 22.2222;
    dt.Rows.Add(row);

    row = dt.NewRow();
    row["ID"] = 3;
    row["Name"] = "Kris";
    row["Height"] = 974382.1658m;
    row["Weight"] = 3.33333333;
    dt.Rows.Add(row);  

    Request request = new Request();
    request.RequestName = "data";
    request.TableName = dt.TableName;
    request.dt = dt;

    var x = JsonConvert.SerializeObject(request);

    Request response = (Request)JsonConvert.DeserializeObject<Request>(x, new RequestConverter());
}

public class Request
{
    public string RequestName { get; set; }
    public string TableName { get; set; }
    public DataTable dt { get; set; }
}

public class RequestConverter : CustomCreationConverter<Request>
{
    public override Request Create(Type objectType)
    {
        return new Request();
    }
}

如果序列化然后反序列化同一个对象作为测试我们将来如何管理我们的通信,那么我所做的一切。但是,时间:

Request response = (Request)JsonConvert.DeserializeObject<Request>(x, new RequestConverter());

执行,它返回错误:

  

反序列化对象时出现意外结束

有谁知道可能会发生什么?

X.ToString()(在某些换行符中添加以便于阅读):

{"RequestName":"data","TableName":"Customers","dt":
[{"ID":1,"Name":"Susan","Height":15.2,"Weight":16.777,"RS":"4"},
{"ID":2,"Name":"Bill","Height":215.2,"Weight":22.2222,"RS":"4"},
{"ID":3,"Name":"Kris","Height":974382.1658,"Weight":3.33333333,"RS":"4"}]}

编辑:这是内部堆栈跟踪。

  

在d:\ Development \ Releases \ Json \ Working \ Src \ Newtonsoft.Json \ Serialization \ JsonSerializerInternalReader.cs中的Newtonsoft.Json.Serialization.JsonSerializerInternalReader.PopulateObject(Object newObject,JsonReader reader,JsonObjectContract contract,String id)中:第904行      在U:\ Development \ Releases \ Json \ Working \ Src \ Newtonsoft.Json \ Serialization \ JsonSerializerInternalReader.cs中的Newtonsoft.Json.Serialization.JsonSerializerInternalReader.Populate(JsonReader reader,Object target):第83行      在D:\ Development \ Releases \ Json \ Working \ Src \ Newtonsoft.Json \ Serialization \ JsonSerializerProxy.cs中的Newtonsoft.Json.Serialization.JsonSerializerProxy.PopulateInternal(JsonReader reader,Object target):第155行      at Newtonoft.Json.Converters.CustomCreationConverter`1.ReadJson(JsonReader reader,Type objectType,Object existingValue,JsonSerializer serializer)在d:\ Development \ Releases \ Json \ Working \ Src \ Newtonsoft.Json \ Converters \ CustomCreationConverter.cs:line 64      在Newtonoft.Json.Serialization.JsonSerializerInternalReader.CreateValueNonProperty(JsonReader reader,Type objectType,JsonContract contract)中的d:\ Development \ Releases \ Json \ Working \ Src \ Newtonsoft.Json \ Serialization \ JsonSerializerInternalReader.cs:第203行      在U:\ Development \ Releases \ Json \ Working \ Src \ Newtonsoft.Json \ Serialization \ JsonSerializerInternalReader.cs中的Newtonsoft.Json.Serialization.JsonSerializerInternalReader.Deserialize(JsonReader reader,Type objectType):第111行      在U:\ Development \ Releases \ Json \ Working \ Src \ Newtonsoft.Json \ JsonSerializer.cs中的Newtonsoft.Json.JsonSerializer.DeserializeInternal(JsonReader reader,Type objectType):第418行      at Newtonsoft.Json.JsonConvert.DeserializeObject(String value,Type type,

其余的是线程/按钮点击堆栈。

0 个答案:

没有答案