title: JSON.NET: How deserialize specific Exception type
body: <p>Consider the following very simple code.</p>
  <pre><code>var ex_to_serialize = new Exception(&quot;something wrong&quot;, new NullReferenceException(&quot;set to null&quot;));
  var serialized_ex = JsonConvert.SerializeObject(ex_to_serialize);
  
  var deserialized_ex = JsonConvert.DeserializeObject&lt;Exception&gt;(serialized_ex);
  Console.WriteLine($&quot;Type of inner exception: {deserialized_ex.InnerException.GetType().Name}&quot;);
  </code></pre>
  <p>And voilá... the inner exception type is <code>Exception</code> instead of <code>NullReferenceException</code>.
  So I searched SO and find lot of example with custom <code>JsonConverter</code> used for desrialization (as the serialized JSON contains <code>ClassName</code> property, which could be used to create the specific type). So I wrote one (only <code>ReadJson</code> is present for simplicity, rest is as usual).</p>
  <pre><code>public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
  {
      if (reader.TokenType == JsonToken.Null)
          return null;
      var json_object = Newtonsoft.Json.Linq.JObject.Load(reader);
      var target_type_name = json_object.Value&lt;string&gt;(&quot;ClassName&quot;);
      var target_type = Type.GetType(target_type_name);
      var target = Activator.CreateInstance(target_type);
      serializer.Populate(json_object.CreateReader(), target);
      return target;
  }
  </code></pre>
  <p>And pass this converter when calling <code>DeserializeObject</code> function above.
  This time, I get exception:</p>
  <blockquote>
  <p>Newtonsoft.Json.JsonSerializationException: Cannot populate JSON object onto type 'System.Exception'. Path 'ClassName'</p>
  </blockquote>
  <p>As far as I dug into the Newtonsoft.Json code, it seems the &quot;problem&quot; is that <code>Exception</code> is <code>[Serializable]</code> and <code>ISerializable</code> so it uses SerializableContract which does not support populating object. But here I end in desperation :-(. Could someone help me?</p>
  <p>(The example above is just for simplicity, I need to serialize and deserialize my own exceptions, which are far more complex. Newtonsoft.Json version 11.0.0.2 used.)</p>