I have a data table in my database where I store various settings. Since they are of any type (even complex object graphs) I decided to store their values as serialized JSON strings.
Let's say that I serialized a List<ItemBase>
. Serialized string looks just fine. But the problem is that list items are of various types that are inherited from ItemBase
(which may as well be abstract for what I care).
Which (de)serialization class/library should I use so my JSON strings will be correctly deserialized to correct object instances.
A common way to deserialize JSON is to first create a class with properties and fields that represent one or more of the JSON properties. Then, to deserialize from a string or a file, call the JsonSerializer. Deserialize method.
There is no polymorphic deserialization (equivalent to Newtonsoft. Json's TypeNameHandling ) support built-in to System.
The JSON contains a list of objects which is abstract and it's not possible to deserialize it.
JSON is a format that encodes objects in a string. Serialization means to convert an object into that string, and deserialization is its inverse operation (convert string -> object).
json.net has the ability to preserve references
And it supports storing the type
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With