title: How to debug JSON Serialization error?
body: <p>I have the following exception generated inconsistently but frequently: </p>
  
  <pre><code>System.Exception: Serialization error - One or more errors occurred., Unexpected character encountered while parsing value: &lt;. Path '', line 0, position 0., System.AggregateException: One or more errors occurred. ---&gt; Newtonsoft.Json.JsonReaderException: Unexpected character encountered while parsing value: &lt;. Path '', line 0, position 0.
     at Newtonsoft.Json.JsonTextReader.ParseValue()
     at Newtonsoft.Json.JsonTextReader.Read()
     at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.ReadForType(JsonReader reader, JsonContract contract, Boolean hasConverter)
     at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.Deserialize(JsonReader reader, Type objectType, Boolean checkAdditionalContent)
     at Newtonsoft.Json.JsonSerializer.DeserializeInternal(JsonReader reader, Type objectType)
     at Newtonsoft.Json.JsonConvert.DeserializeObject(String value, Type type, JsonSerializerSettings settings)
     at Newtonsoft.Json.JsonConvert.DeserializeObject[T](String value, JsonSerializerSettings settings)
     at Microsoft.AspNet.SignalR.Client.Transports.TransportHelper.b__1(String raw)
     at Microsoft.AspNet.SignalR.TaskAsyncHelper.&lt;&gt;c__DisplayClass19`2.b__17(Task`1 t)
     at Microsoft.AspNet.SignalR.TaskAsyncHelper.TaskRunners`2.&lt;&gt;c__DisplayClass42.b__41(Task`1 t)
     --- End of inner exception stack trace ---
     at System.Threading.Tasks.Task.ThrowIfExceptional(Boolean includeTaskCanceledExceptions)
     at System.Threading.Tasks.Task.Wait(Int32 millisecondsTimeout, CancellationToken cancellationToken)
     **at Project.Services.NotificationService.SignalObjectToUser(Object data, String username, Boolean isTypingNotification)
  ---&gt; (Inner Exception #0) Newtonsoft.Json.JsonReaderException: Unexpected character encountered while parsing value: &lt;. Path '', line 0, position 0.**
     at Newtonsoft.Json.JsonTextReader.ParseValue()
     at Newtonsoft.Json.JsonTextReader.Read()
     at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.ReadForType(JsonReader reader, JsonContract contract, Boolean hasConverter)
     at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.Deserialize(JsonReader reader, Type objectType, Boolean checkAdditionalContent)
     at Newtonsoft.Json.JsonSerializer.DeserializeInternal(JsonReader reader, Type objectType)
     at Newtonsoft.Json.JsonConvert.DeserializeObject(String value, Type type, JsonSerializerSettings settings)
     at Newtonsoft.Json.JsonConvert.DeserializeObject[T](String value, JsonSerializerSettings settings)
     at Microsoft.AspNet.SignalR.Client.Transports.TransportHelper.b__1(String raw)
     at Microsoft.AspNet.SignalR.TaskAsyncHelper.&lt;&gt;c__DisplayClass19`2.b__17(Task`1 t)
  </code></pre>
  
  <p>The important bit: </p>
  
  <pre><code>**at Project.Services.NotificationService.SignalObjectToUser(Object data, String username, Boolean isTypingNotification)
  ---&gt; (Inner Exception #0) Newtonsoft.Json.JsonReaderException: Unexpected character encountered while parsing value: &lt;. Path '', line 0, position 0.**
  </code></pre>
  
  <p>My code there: </p>
  
  <pre><code>private void SignalObjectToUser(object data, string username, bool isTypingNotification = false) {
          try {
              _hubConnection = new HubConnection(_baseUrl);
              _hubConnection.CookieContainer = new CookieContainer();
              _hubConnection.CookieContainer.Add(_cookie);
              _hubProxy = _hubConnection.CreateHubProxy("appHub");
              _hubConnection.Start().Wait();
              string serialized = null;
              try {
                  List&lt;string&gt; errors = new List&lt;string&gt;();
                  serialized = JsonConvert.SerializeObject(data, new JsonSerializerSettings() {
                      Error = delegate(object sender, ErrorEventArgs args) {
                          errors.Add(args.ErrorContext.Error.Message);
                          args.ErrorContext.Handled = true;
                          Elmah.ErrorSignal.FromCurrentContext().Raise(new Exception("Delegated serialization error - " + args.ErrorContext.Error.Message));
                      }
                  });
                  _hubProxy.Invoke((isTypingNotification ? "SendTypingNotification" : "SendClientNotification"), serialized, username).Wait();
              }
              catch (Exception exception) {
                  Elmah.ErrorSignal.FromCurrentContext().Raise(new Exception("Serialization error - " + exception.Message + ", " + exception.InnerException.Message + ", " + exception.ToString()));
                  throw new Exception("Messaging Error");
              }
          }
          catch (Exception e) {
              Elmah.ErrorSignal.FromCurrentContext().Raise(new Exception("Serialization error - " + e.Message + ", " + e.InnerException.Message + ", " + e.ToString()));
              throw new Exception("Messaging Error");
          }
      }
  </code></pre>
  
  <p>I'm trying to figure out what <em>exactly</em> is causing this issue though.  I don't know what key/value pair is generating the exception.  How can I gather more instructive details here?</p>