title: Randoms failures on json.net deserialization through WebRequest
body: <p>I'm using Json.NET for deserializing json files sent by the API server on an iOS client (with Monotouch). I have a really weird problem because sometimes the deserialization fails with randoms exceptions. I hadn't any problem before and I think it started since the json files went bigger. When it happens, I just ask the application to try again and after a random number of times (and randoms different exceptions) the deserialization finally works, for no reason.</p>
  
  <p>Here are the different exceptions I got:</p>
  
  <pre><code>  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 &lt;filename unknown&gt;:0 
          at Newtonsoft.Json.Linq.JObject.Load (Newtonsoft.Json.JsonReader reader) [0x00000] in &lt;filename unknown&gt;: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 &lt;filename unknown&gt;:0 
          at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.PopulateList (IWrappedCollection wrappedList, Newtonsoft.Json.JsonReader reader, System.String reference, Newtonsoft.Json.Serialization.JsonArrayContract contract) [0x00000] in &lt;filename unknown&gt;:0 
  
  Failed to deserialize: Unterminated string. Expected delimiter: ". Line 2530, position 69.  at Newtonsoft.Json.JsonTextReader.ReadStringIntoBuffer (Char quote) [0x00000] in &lt;filename unknown&gt;:0 
    at Newtonsoft.Json.JsonTextReader.ParseString (Char quote) [0x00000] in &lt;filename unknown&gt;:0 
    at Newtonsoft.Json.JsonTextReader.ParseValue (Char currentChar) [0x00000] in &lt;filename unknown&gt;:0 
    at Newtonsoft.Json.JsonTextReader.ReadInternal () [0x00000] in &lt;filename unknown&gt;:0 
    at Newtonsoft.Json.JsonTextReader.Read () [0x00000] in &lt;filename unknown&gt;:0 
    at Newtonsoft.Json.Linq.JContainer.ReadContentFrom (Newtonsoft.Json.JsonReader r) [0x00000] in &lt;filename unknown&gt;:0 
    at Newtonsoft.Json.Linq.JContainer.ReadTokenFrom (Newtonsoft.Json.JsonReader r) [0x00000] in &lt;filename unknown&gt;:0 
    at Newtonsoft.Json.Linq.JObject.Load (Newtonsoft.Json.JsonReader reader) [0x00000] in &lt;filename unknown&gt;: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 &lt;filename unknown&gt;:0 
    at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.PopulateList (IWrappedCollection wrappedList, Newtonsoft.Json.JsonReader reader, System.String reference,     Newtonsoft.Json.Serialization.JsonArrayContract contract) [0x00000] in &lt;filename unknown&gt;:0 
  </code></pre>
  
  <p>Here is the few lines I use for deserializing:</p>
  
  <pre><code>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&lt;SimpleServerResponse&gt;(jsonReader);
              }
          }
       }
  }
  </code></pre>
  
  <p>It crashes at the JObject.Load(reader):</p>
  
  <pre><code>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&lt;int&gt;();
  
              switch (id) {
                  case ANotification.STORY_INVITE:
                      target = new Notification&lt;Moment&gt;();
                      break;
                  case ANotification.STORY_CONTENT_ADDED:
                      target = new Notification&lt;Moment&gt;();
                      break;
                  case ANotification.STORY_LIKE:
                      target = new Notification&lt;Moment&gt;();
                      break;
                  case ANotification.PICTURE_COMMENTED:
                      target = new Notification&lt;PictureMomentPair&gt;();
                      break;
                  case ANotification.PICTURE_LIKED:
                      target = new Notification&lt;PictureMomentPair&gt;();
                      break;
                  case ANotification.USER_FOLLOW:
                      target = new Notification&lt;User&gt;();
                      break;
                  default:
                      break;
              }
  
              serializer.Populate(jobject.CreateReader(), target);
          }
  
          return target;
      }
  }
  </code></pre>
  
  <p>Some explanation about my JsonConverter: the objects sent by the server on the JSON file may change depending on the "type_id" value. The JsonConverter try to read the type_id and return the correct object depending on it.</p>
  
  <p>My workaround for now is to read the entire stream in a string and then pass it to the json deserializer, but it is definitely slower on iOS (more CPU consuming).</p>
  
  <p>I don't know why sometimes it works and sometimes not.</p>