Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Make sure you specify TypeNameHandling when deserializing, as per the docs:</p> <pre><code>// for security TypeNameHandling is required when deserializing Stockholder newStockholder = JsonConvert.DeserializeObject&lt;Stockholder&gt;(jsonTypeNameAuto, new JsonSerializerSettings { TypeNameHandling = TypeNameHandling.Auto }); </code></pre> <p>It is worth noting that the documentation is deserializing a Concrete class that <strong>contains</strong> a collection of Abstract classes.</p> <p>As an experiment try creating a throw-away class (concrete) that has a single property with your list of abstract objects and see if you can serialize and deserialize that.</p> <p><strong>UPDATE:</strong></p> <p>I just tested the following code in LINQPad:</p> <pre><code>void Main() { var test = new List&lt;Business&gt;(); test.Add(new Hotel { Name = "Hilton", Stars = 5 }); test.Add(new Pool { Name = "Big Splash", Capacity = 500 }); test.Dump(); string json = JsonConvert.SerializeObject(test, Newtonsoft.Json.Formatting.Indented, new JsonSerializerSettings { TypeNameHandling = TypeNameHandling.All }); json.Dump(); var businesses = JsonConvert.DeserializeObject&lt;List&lt;Business&gt;&gt;(json, new JsonSerializerSettings { TypeNameHandling = TypeNameHandling.All }); businesses.Dump(); } // Define other methods and classes here public abstract class Business { public string Name { get;set; } } public class Hotel : Business { public int Stars { get;set; } } public class Pool : Business { public int Capacity { get;set;} } </code></pre> <p>It worked perfectly. Abstract collection serialized to:</p> <pre><code>{ "$type": "System.Collections.Generic.List`1[[UserQuery+Business, query_jvrdcu]], mscorlib", "$values": [ { "$type": "UserQuery+Hotel, query_jvrdcu", "Stars": 5, "Name": "Hilton" }, { "$type": "UserQuery+Pool, query_jvrdcu", "Capacity": 500, "Name": "Big Splash" } ] } </code></pre> <p>The original and the deserialized collections matched.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
 

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