Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JSON deserialization to inherited types

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).

Question

Which (de)serialization class/library should I use so my JSON strings will be correctly deserialized to correct object instances.

like image 733
Robert Koritnik Avatar asked Nov 25 '10 01:11

Robert Koritnik


People also ask

How do I deserialize a JSON file?

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.

Is polymorphic deserialization possible in System text JSON?

There is no polymorphic deserialization (equivalent to Newtonsoft. Json's TypeNameHandling ) support built-in to System.

Can we deserialize an abstract class?

The JSON contains a list of objects which is abstract and it's not possible to deserialize it.

What is JSON serialization deserialization?

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).


1 Answers

json.net has the ability to preserve references

And it supports storing the type

like image 123
CodesInChaos Avatar answered Sep 28 '22 17:09

CodesInChaos