我有一些用于解析Web响应的类:
[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.
的InnerException:
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)
StrackTrace:
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": ""
}
}
}