Json.Net deserialize into C# derived class -


json.net not deserializing object receives proper derivatives of control class. (please see following explanation of problem. please note, feel minimum amount of code required explain issue. ahead of time reviewing problem.)

i trying serialize/deserialize following class(es) into/from json.

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; } 

when optionscontrol serialized, json looks following. note json.net adds $type property, (supposedly) can deserialize control optionscontrol.

              {                    "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,                  "$type":"myapp.infrastructure.core.models.ui.controls.optionscontrol, myapp.infrastructure.core"               } 

however, when try deserialize page (which contains base controls , optionscontrols), controls deserialized base control , options property (which includes list of states, above) ignored.

this how try deserialize (and update) page object receive via webapi service:

    [httpput]     [actionname("updatepage")]     public async task<commandresult> updatepageasync([frombody]object page)     {         try         {             // deserialize page correct object             // when debug.writeline page.tostring(), page still has $type property @ point (before deserialized).             var dspage = return jsonconvert.deserializeobject<page>(page.tostring(), new jsonserializersettings             {                 typenamehandling = typenamehandling.all             });              // dspage passed in here not have optionscontrols.             // @ point, have been deserialized base control, unfortunately.             return await _uicommandfacade.updatepageasync(dspage, msg);         }         catch (exception ex)         {             return new commandresult()             {                 errortype = errortype.controllererror,                 developermessage = $"unable update page",                 exception = ex             };         }     } 

as can see, using typenamehandling.all (as described in docs , mentioned many times on so) deserialization (just during serialization, generates $type property in json). however, when deserialize page object, optionscontrols in page object deserialized regular base controls, options property ignored (and list of states not updated / thrown away).

how json.net deserialize controls , derivatives?

it looks problem might due fact $type metadata property not first property in json control. normally, json.net needs property first in order recognize it. try adding

metadatapropertyhandling = metadatapropertyhandling.readahead 

to settings when deserializing. should allow json.net find $type property later in json.


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? -