我正在使用Json.NET反序列化由iOS服务器(使用Monotouch)上的API服务器发送的json文件。我有一个非常奇怪的问题,因为有时反序列化会因为randoms异常而失败。我以前没有任何问题,我认为它开始,因为json文件变大了。当它发生时,我只是要求应用程序再次尝试并且在随机数次之后(和randoms不同的例外)反序列化最终无效。
以下是我得到的不同例外情况:
2013-02-12 19:14:58.307 client_ios[2176:4303] Failed to deserialize: Unexpected end of content while loading JObject.
at Newtonsoft.Json.Linq.JContainer.ReadTokenFrom (Newtonsoft.Json.JsonReader r) [0x00000] in <filename unknown>:0
at Newtonsoft.Json.Linq.JObject.Load (Newtonsoft.Json.JsonReader reader) [0x00000] in <filename unknown>:0
at shared.api.NotificationConverter.ReadJson (Newtonsoft.Json.JsonReader reader, System.Type objectType, System.Object existingValue, Newtonsoft.Json.JsonSerializer serializer) [0x00000] in /Users/xxx/Projects/xxx/shared/src/api/Notification.cs:23
at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateValueNonProperty (Newtonsoft.Json.JsonReader reader, System.Type objectType, Newtonsoft.Json.Serialization.JsonContract contract) [0x00000] in <filename unknown>:0
at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.PopulateList (IWrappedCollection wrappedList, Newtonsoft.Json.JsonReader reader, System.String reference, Newtonsoft.Json.Serialization.JsonArrayContract contract) [0x00000] in <filename unknown>:0
Failed to deserialize: Unterminated string. Expected delimiter: ". Line 2530, position 69. at Newtonsoft.Json.JsonTextReader.ReadStringIntoBuffer (Char quote) [0x00000] in <filename unknown>:0
at Newtonsoft.Json.JsonTextReader.ParseString (Char quote) [0x00000] in <filename unknown>:0
at Newtonsoft.Json.JsonTextReader.ParseValue (Char currentChar) [0x00000] in <filename unknown>:0
at Newtonsoft.Json.JsonTextReader.ReadInternal () [0x00000] in <filename unknown>:0
at Newtonsoft.Json.JsonTextReader.Read () [0x00000] in <filename unknown>:0
at Newtonsoft.Json.Linq.JContainer.ReadContentFrom (Newtonsoft.Json.JsonReader r) [0x00000] in <filename unknown>:0
at Newtonsoft.Json.Linq.JContainer.ReadTokenFrom (Newtonsoft.Json.JsonReader r) [0x00000] in <filename unknown>:0
at Newtonsoft.Json.Linq.JObject.Load (Newtonsoft.Json.JsonReader reader) [0x00000] in <filename unknown>:0
at shared.api.NotificationConverter.ReadJson (Newtonsoft.Json.JsonReader reader, System.Type objectType, System.Object existingValue, Newtonsoft.Json.JsonSerializer serializer) [0x00000] in /Users/xxx/Projects/xxx/shared/src/api/Notification.cs:23
at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateValueNonProperty (Newtonsoft.Json.JsonReader reader, System.Type objectType, Newtonsoft.Json.Serialization.JsonContract contract) [0x00000] in <filename unknown>:0
at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.PopulateList (IWrappedCollection wrappedList, Newtonsoft.Json.JsonReader reader, System.String reference, Newtonsoft.Json.Serialization.JsonArrayContract contract) [0x00000] in <filename unknown>:0
以下是我用于反序列化的几行:
using (WebResponse webResponse = requestWeb.GetResponse()) {
using (Stream responseStream = webResponse.GetResponseStream()) {
using (StreamReader streamReader = new StreamReader(responseStream)) {
using (JsonReader jsonReader = new JsonTextReader(streamReader)) {
JsonSerializer serializer = new JsonSerializer();
serializer.Converters.Add(creator);
rsp = serializer.Deserialize<SimpleServerResponse>(jsonReader);
}
}
}
}
它在JObject.Load(读者)崩溃:
public class NotificationConverter : JsonConverter {
public override bool CanConvert(Type objectType) {
return typeof(ANotification).IsAssignableFrom(objectType);
}
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) {
JObject jobject = JObject.Load(reader);
object target = null;
JToken token = jobject["type_id"];
if (token != null) {
int id = token.ToObject<int>();
switch (id) {
case ANotification.STORY_INVITE:
target = new Notification<Moment>();
break;
case ANotification.STORY_CONTENT_ADDED:
target = new Notification<Moment>();
break;
case ANotification.STORY_LIKE:
target = new Notification<Moment>();
break;
case ANotification.PICTURE_COMMENTED:
target = new Notification<PictureMomentPair>();
break;
case ANotification.PICTURE_LIKED:
target = new Notification<PictureMomentPair>();
break;
case ANotification.USER_FOLLOW:
target = new Notification<User>();
break;
default:
break;
}
serializer.Populate(jobject.CreateReader(), target);
}
return target;
}
}
关于我的JsonConverter的一些解释:服务器在JSON文件上发送的对象可能会根据“type_id”值而改变。 JsonConverter尝试读取type_id并根据它返回正确的对象。
我现在的解决方法是在字符串中读取整个流,然后将其传递给json反序列化程序,但在iOS上肯定会更慢(消耗更多的CPU)。
我不知道为什么有时会起作用,有时候不行。