gpt4 book ai didi

c# - 序列化中的 System.OverflowException

转载 作者:行者123 更新时间:2023-11-30 20:54:12 25 4
gpt4 key购买 nike

我有一些用于解析网络响应的类:

[DataContract]
public abstract class GYResponse
{
[DataMember(Name = "code")]
public int Code { get; set; }
[DataMember(Name = "message")]
public string Message { get; set; }
}

[DataContract]
public class ResponseGetUser : GYResponse
{
[DataMember(Name = "result")]
public GYUser Result { get; set; }
}

[DataContract]
public class ResponseGetVenue : GYResponse
{
[DataMember(Name = "result")]
public GYVenue Result { get; set; }
}

我使用DataContractJsonSerializer:

GYResponse response_obj = request.Creater.CreateResponseObj(); 
//CreateResponseObj() return ResponseGetUser, ResponseGetVenue etc

using (MemoryStream stream = new MemoryStream(Encoding.Unicode.GetBytes(gyResponse)))
{
DataContractJsonSerializer serializer = new DataContractJsonSerializer(response_obj.GetType());
response_obj = (GYResponse)serializer.ReadObject(stream);
}

我有 "System.OverflowException" 行:

response_obj = (GYResponse)serializer.ReadObject(stream); 

/checked 编译器选项已关闭。而 ReadObject 不能抛出这个异常。

但如果我重写这些类,它就可以工作了:

[DataContract]
public abstract class GYResponse
{

}

[DataContract]
public class ResponseGetUser : GYResponse
{
[DataMember(Name = "result")]
public GYUser Result { get; set; }
[DataMember(Name = "code")]
public int Code { get; set; }
[DataMember(Name = "message")]
public string Message { get; set; }
}

[DataContract]
public class ResponseGetVenue : GYResponse
{
[DataMember(Name = "result")]
public GYVenue Result { get; set; }
[DataMember(Name = "code")]
public int Code { get; set; }
[DataMember(Name = "message")]
public string Message { get; set; }
}

我知道这是一个模糊的问题,但不明白可能是什么问题。什么时候可以抛出这个异常?

更新异常信息:

留言:

 There was an error deserializing the object of type GotYou.RequesterService.ResponseGetInfo. Value was either too large or too small for an Int32.

内部异常:

at System.Number.ParseInt32(String s, NumberStyles style, NumberFormatInfo info)
at System.String.System.IConvertible.ToInt32(IFormatProvider provider)
at System.Convert.ChangeType(Object value, Type conversionType, IFormatProvider provider)
at System.Runtime.Serialization.Json.DataContractJsonSerializer.ConvertObjectToPrimitiveDataContract(DataContract contract, Object value, XmlObjectSerializerReadContextComplexJson context)
at System.Runtime.Serialization.Json.DataContractJsonSerializer.ConvertObjectToDataContract(DataContract contract, Object value, XmlObjectSerializerReadContextComplexJson context)
at System.Runtime.Serialization.Json.ObjectToDataContractConverter.ReadClassDataContractMembers(DataContractJsonSerializer serializer, ClassDataContract dataContract, Dictionary`2 deserialzedValue, Object newInstance, XmlObjectSerializerReadContextComplexJson context)
at System.Runtime.Serialization.Json.ObjectToDataContractConverter.ReadClassDataContractMembers(DataContractJsonSerializer serializer, ClassDataContract dataContract, Dictionary`2 deserialzedValue, Object newInstance, XmlObjectSerializerReadContextComplexJson context)
at System.Runtime.Serialization.Json.ObjectToDataContractConverter.ConvertDictionaryToClassDataContract(DataContractJsonSerializer serializer, ClassDataContract dataContract, Dictionary`2 deserialzedValue, XmlObjectSerializerReadContextComplexJson context)
at System.Runtime.Serialization.Json.DataContractJsonSerializer.ConvertObjectToDataContract(DataContract contract, Object value, XmlObjectSerializerReadContextComplexJson context)
at System.Runtime.Serialization.Json.ObjectToDataContractConverter.ReadClassDataContractMembers(DataContractJsonSerializer serializer, ClassDataContract dataContract, Dictionary`2 deserialzedValue, Object newInstance, XmlObjectSerializerReadContextComplexJson context)
at System.Runtime.Serialization.Json.ObjectToDataContractConverter.ConvertDictionaryToClassDataContract(DataContractJsonSerializer serializer, ClassDataContract dataContract, Dictionary`2 deserialzedValue, XmlObjectSerializerReadContextComplexJson context)
at System.Runtime.Serialization.Json.DataContractJsonSerializer.ConvertObjectToDataContract(DataContract contract, Object value, XmlObjectSerializerReadContextComplexJson context)
at System.Runtime.Serialization.Json.ObjectToDataContractConverter.ReadClassDataContractMembers(DataContractJsonSerializer serializer, ClassDataContract dataContract, Dictionary`2 deserialzedValue, Object newInstance, XmlObjectSerializerReadContextComplexJson context)
at System.Runtime.Serialization.Json.ObjectToDataContractConverter.ConvertDictionaryToClassDataContract(DataContractJsonSerializer serializer, ClassDataContract dataContract, Dictionary`2 deserialzedValue, XmlObjectSerializerReadContextComplexJson context)
at System.Runtime.Serialization.Json.DataContractJsonSerializer.ConvertObjectToDataContract(DataContract contract, Object value, XmlObjectSerializerReadContextComplexJson context)
at System.Runtime.Serialization.Json.DataContractJsonSerializer.ReadObject(Stream stream)

轨迹跟踪:

 at System.Runtime.Serialization.Json.DataContractJsonSerializer.ReadObject(Stream stream)
at GotYou.RequesterService.PostRequester.getResponseCallback(IAsyncResult asynchronousResult)

JSON 响应:

{
"code": 0,
"message": "",
"result": {
"uid": "51da997decaa3ca351000000",
"url": "",
"nickname": "Smileman",
"avatar": "\/img\/profile_pics\/users\/51da997decaa3ca351000000\/cache\/avatar48x48.jpg?lastUpdate=1381215566",
"age": {
"number": "22",
"suffix": "года",
"birthday": "1991-04-17"
},
"sex": 1,
"aboutself": "",
"premium": true,
"isFriend": 3,
"online": {
"lastVisit": "7 минут назад",
"status": true
},
"features": {
"car": false,
"house": false,
"cash": false,
"group": true
},
"location": {
"longitude": 36.570838832,
"latitude": 50.571069472,
"timestamp": 1381319677
},
"bonuses": 8515,
"address": "",
"city": {
"code": "3100000100000",
"title": ""
},
"cover": "\/img\/profile_pics\/users\/51da997decaa3ca351000000\/cache\/hero480x290.jpg?lastUpdate=1381215566",
"distance": 63.671441859659,
"paidServices": {
"vip": true,
"theming": true,
"invisible": false,
"incognito": false,
"closed": true
},
"regdate": "19.08.2013",
"purpose": {
"id": "500fa718ecaa3c6e3b000002",
"title": ""
}
}
}

最佳答案

您数据中的这个数字:3100000100000 对于 Int32 来说太大了拿着它。 Int32 的最大正值是 2147483647。您可以考虑使用 Int64 (UInt32 也太小了,因为它的范围结束于 4294967295)。

但是,code 字段通常被解释为字符串,所以也许 string 数据类型是更好的选择,例如:

[DataMember(Name = "code")]
public string Code { get; set; }

关于c# - 序列化中的 System.OverflowException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19274014/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com