Note that there are some explanatory texts on larger screens.

plurals
  1. POSerializing/Deserializing Dictionary of objects with JSON.NET
    text
    copied!<p>I'm trying to serialize/deserialize a <code>Dictionary&lt;string, object&gt;</code> which seems to work fine if the object is a simple type but doesn't work when the object is more complex.</p> <p>I have this class:</p> <pre><code>public class UrlStatus { public int Status { get; set; } public string Url { get; set; } } </code></pre> <p>In my dictionary I add a <code>List&lt;UrlStatus&gt;</code> with a key of "Redirect Chain" and a few simple strings with keys "Status", "Url", "Parent Url". The string I'm getting back from JSON.Net looks like this: </p> <pre><code>{"$type":"System.Collections.Generic.Dictionary`2[[System.String, mscorlib],[System.Object, mscorlib]], mscorlib","Status":"OK","Url":"http://www.ehow.com/m/how_5615409_create-pdfs-using-bean.html","Parent Url":"http://www.ehow.com/mobilearticle35.xml","Redirect Chain":[{"$type":"Demand.TestFramework.Core.Entities.UrlStatus, Demand.TestFramework.Core","Status":301,"Url":"http://www.ehow.com/how_5615409_create-pdfs-using-bean.html"}]} </code></pre> <p>The code I'm using to serialize looks like:</p> <pre><code>JsonConvert.SerializeObject(collection, Formatting.None, new JsonSerializerSettings { TypeNameHandling = TypeNameHandling.Objects, TypeNameAssemblyFormat = System.Runtime.Serialization.Formatters.FormatterAssemblyStyle.Simple }); </code></pre> <p>to deserialize I'm doing:</p> <pre><code>JsonConvert.DeserializeObject&lt;T&gt;(collection, new JsonSerializerSettings { TypeNameHandling = TypeNameHandling.Objects, TypeNameAssemblyFormat = System.Runtime.Serialization.Formatters.FormatterAssemblyStyle.Simple, }); </code></pre> <p>The Dictionary comes back fine, all the strings come back fine, but the List doesn't get properly deserialized. It just comes back as </p> <pre><code>{[ { "$type": "XYZ.TestFramework.Core.Entities.UrlStatus, XYZ.TestFramework.Core", "Status": 301, "Url": "/how_5615409_create-pdfs-using-bean.html" } ]} </code></pre> <p>Of course I can deserailize this string again and I get the correct object, but it seems like JSON.Net should have done this for me. Clearly I'm doing something wrong, but I don't know what it is.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload