c# - Deserialize to real underlying type with NewtonSoft.Json -


if serialise object json new jsonserializersettings { typenamehandling = typenamehandling.all } json includes type names. e.g.

{     "$type": "mydomain.customers.subscribedtonewsletter, mydomain",     "newslettername": "top", } 

therefore when use var evt = jsonconvert.deserialiseobject(json, settings); the same settings expect inspect $type encoded in json string , return object of type.

instead, jobject. have no idea actual type ought without inspecting json string myself hand in advance.

what correct route around problem?

this works me.

namespace typenamehandlingtest {     using system;     using newtonsoft.json;      public class foo     {         public string bar { get; set; }     }      class program     {         static void main(string[] args)         {             var foo = new foo {                  bar = "bar"              };             var settings = new jsonserializersettings {                  typenamehandling = newtonsoft.json.typenamehandling.all              };             var json = jsonconvert.serializeobject(foo, settings);              object deserialized = jsonconvert.deserializeobject(json, settings);             console.writeline(deserialized.gettype().assemblyqualifiedname);             console.readline();         }     } } 

output:

typenamehandlingtest.foo, typenamehandlingtest, version=1.0.0.0, culture=neutral, publickeytoken=null 

Comments