c# - Json.Net not saving $type information -


when serialize page object, json.net not adding $type property controls (which in ilist) when serializes them. have tried adding following code class constructor , webapi startup, json.net still not adding $type information control serializes.

        jsonconvert.defaultsettings = () => new jsonserializersettings         {             typenamehandling = typenamehandling.all,             metadatapropertyhandling = metadatapropertyhandling.readahead         }; 

for testing purposes, added $type property control myself in json code, , json.net able deserialize object correctly, still not serializing correctly. here's how classes setup.

public class page {     public guid id { get; set; }     public guid customerid { get; set; }     public ilist<control> controls { get; set; } } 

and here control class:

public class control : controlbase {     public override enums.cscontroltype cscontroltype { { return enums.cscontroltype.base; } } } 

and here controlbase abstract class:

public abstract class controlbase {     public guid id { get; set; }      public virtual enums.cscontroltype cscontroltype { get; }      public enums.controltype type { get; set; }      public string propertyname { get; set; }      public ilist<int> width { get; set; }      public string friendlyname { get; set; }      public string description { get; set; } } 

and here optionscontrol derived control:

public class optionscontrol : control {     public override enums.cscontroltype cscontroltype { { return enums.cscontroltype.optionscontrol; } }      public idictionary<string, string> options; } 

and how json comes out:

"pages": [     {       "id": "00000000-0000-0000-0000-000000000000",       "customerid": "00000000-0000-0000-0000-000000000000",       "controls": [         {           "options": {             "tn": "tn"           },           "cscontroltype": 4,           "id": "00000000-0000-0000-0000-000000000000",           "type": 4,           "propertyname": "addresses[0].state",           "width": [             2,             2,             6           ],           "friendlyname": "state",           "description": null         }       ]     } ] 

as can see, json.net did not add $type property json object. problem need json.net give me base control object need give me instance of optionscontrol object (which inherits control). why json.net not adding $type property controls?

rather modifying global settings used framework, can add [jsonproperty(itemtypenamehandling = typenamehandling.all)] public ilist<control> controls { get; set; } property force type information emitted each item in list:

public class page {     public guid id { get; set; }     public guid customerid { get; set; }     [jsonproperty(itemtypenamehandling = typenamehandling.all)]     public ilist<control> controls { get; set; } } 

sample fiddle.

you might consider sanitizing type information using custom serialization binder reasons explained here.


Comments

Popular posts from this blog

php - Permission denied. Laravel linux server -

google bigquery - Delta between query execution time and Java query call to finish -

python - Pandas two dataframes multiplication? -