This project has moved. For the latest updates, please go here.

Deserializing of object when type is not known in compile time

May 29, 2013 at 2:05 PM
JsonConvert.DeserializeObject() - note the method is used without <Type> - returns always object of type Newtonsoft.Json.Linq.JObject regardless of $type value.

Let's say I have classes:
class DataA
{ ... }
class DataB : DataA
{ ... }

DataA GetOp(..)
// returns DataA or DataB
The object returned by GetOp() is serialized into JSON with type identification, like this:
  "$type": "JamesonTester.DataB, JamesonTester",
  "Count": 26,
Now I would expect that following call deserializes json data to the type specified in $type property.
DataA x = JsonConvert.DeserializeObject(json);
I'm obviiously wrong but how could I achieve such behavior?