Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I think that is a bug in an older version of Json.NET. If you aren't already using the latest version, upgrade and try again.</p> <pre><code> public class UrlStatus { public int Status { get; set; } public string Url { get; set; } } [TestMethod] public void GenericDictionaryObject() { Dictionary&lt;string, object&gt; collection = new Dictionary&lt;string, object&gt;() { {"First", new UrlStatus{ Status = 404, Url = @"http://www.bing.com"}}, {"Second", new UrlStatus{Status = 400, Url = @"http://www.google.com"}}, {"List", new List&lt;UrlStatus&gt; { new UrlStatus {Status = 300, Url = @"http://www.yahoo.com"}, new UrlStatus {Status = 200, Url = @"http://www.askjeeves.com"} } } }; string json = JsonConvert.SerializeObject(collection, Formatting.Indented, new JsonSerializerSettings { TypeNameHandling = TypeNameHandling.All, TypeNameAssemblyFormat = FormatterAssemblyStyle.Simple }); Assert.AreEqual(@"{ ""$type"": ""System.Collections.Generic.Dictionary`2[[System.String, mscorlib],[System.Object, mscorlib]], mscorlib"", ""First"": { ""$type"": ""Newtonsoft.Json.Tests.Serialization.TypeNameHandlingTests+UrlStatus, Newtonsoft.Json.Tests"", ""Status"": 404, ""Url"": ""http://www.bing.com"" }, ""Second"": { ""$type"": ""Newtonsoft.Json.Tests.Serialization.TypeNameHandlingTests+UrlStatus, Newtonsoft.Json.Tests"", ""Status"": 400, ""Url"": ""http://www.google.com"" }, ""List"": { ""$type"": ""System.Collections.Generic.List`1[[Newtonsoft.Json.Tests.Serialization.TypeNameHandlingTests+UrlStatus, Newtonsoft.Json.Tests]], mscorlib"", ""$values"": [ { ""$type"": ""Newtonsoft.Json.Tests.Serialization.TypeNameHandlingTests+UrlStatus, Newtonsoft.Json.Tests"", ""Status"": 300, ""Url"": ""http://www.yahoo.com"" }, { ""$type"": ""Newtonsoft.Json.Tests.Serialization.TypeNameHandlingTests+UrlStatus, Newtonsoft.Json.Tests"", ""Status"": 200, ""Url"": ""http://www.askjeeves.com"" } ] } }", json); object c = JsonConvert.DeserializeObject(json, new JsonSerializerSettings { TypeNameHandling = TypeNameHandling.All, TypeNameAssemblyFormat = FormatterAssemblyStyle.Simple }); Assert.IsInstanceOfType(c, typeof(Dictionary&lt;string, object&gt;)); Dictionary&lt;string, object&gt; newCollection = (Dictionary&lt;string, object&gt;)c; Assert.AreEqual(3, newCollection.Count); Assert.AreEqual(@"http://www.bing.com", ((UrlStatus)newCollection["First"]).Url); List&lt;UrlStatus&gt; statues = (List&lt;UrlStatus&gt;) newCollection["List"]; Assert.AreEqual(2, statues.Count); } } </code></pre> <p>Edit, I just noticed you mentioned a list. TypeNameHandling should be set to All.</p> <p>Documentation: <a href="http://www.newtonsoft.com/json/help/html/SerializeTypeNameHandling.htm" rel="nofollow noreferrer"><strong>TypeNameHandling setting</strong></a></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