c# - Json.Net serialization of IEnumerable with TypeNameHandling=auto -


according json.net documentation ienumerable types should serialized json array.

so expect following class:

public class myclass {     public ienumerable<string> values { get; set; } } 

to serialized as:

{     "values": [] } 

the problem when use typenamehandling=auto get:

{     "values": {         "$type": "system.string[], mscorlib",         "$values": []     } } 

i need typenamehandling=auto other properties expect ienumerable use default serialization. other types (ilist example) works expected.

it bug or missing something?

here full code reproduce problem:

    [test]     public void newtonsoft_serialize_list_and_enumerable()     {         var target = new newtonsoft.json.jsonserializer         {             typenamehandling = typenamehandling.auto         };          var myevent = new myclass         {             values = new string[0]         };          var builder = new stringwriter();         target.serialize(builder, myevent);         var json = jobject.parse(builder.tostring());          assert.areequal(jtokentype.array, json["values"].type);     }      public class myclass     {         public ienumerable<string> values { get; set; }     } 

i'm using newtonsoft 7.0.1.

update: here test using more types:

    [test]     public void newtonsoft_serialize_list_and_enumerable()     {         var target = new newtonsoft.json.jsonserializer         {             typenamehandling = typenamehandling.auto         };          var myevent = new myclass         {             values1 = new string[0],             values2 = new list<string>(),             values3 = new string[0],             values4 = new list<string>(),             values5 = new string[0]         };          var builder = new stringwriter();         target.serialize(builder, myevent);         var json = builder.tostring();     }      public class myclass     {         public ienumerable<string> values1 { get; set; }         public ienumerable<string> values2 { get; set; }         public ilist<string> values3 { get; set; }         public ilist<string> values4 { get; set; }         public string[] values5 { get; set; }     } 

and results:

{     "values1": {         "$type": "system.string[], mscorlib",         "$values": []     },     "values2": [],     "values3": {         "$type": "system.string[], mscorlib",         "$values": []     },     "values4": [],     "values5": [] } 

again don't understand why different results depending on combination.

the default automatic behaviour of json.net, when deserializing ienumerable or ilist field, create list instance. if assign array instance, way restore object original instance state json.net add $type meta data, seeing. i.e. there many ways deserialize ienumerable field.

by using list<string> instance:

var myevent = new myclass {     values = new list<string>(), }; 

with:

public class myclass {    public ienumerable<string> values { get; set; } }     

results in (when serialized):

{"values":["hello"]} 

also, if use explicit constructable type or use default list, json.net use , omit $type;

for instance:

string[] values { get; set; } = new string[0]; // not needed ienumerable<string> values { get; set; } = new string[0]; //$type needed ienumerable<string> values { get; set; } = new stack<string>(); //$type needed ienumerable<string> values { get; set; } = new list<string>; // not needed list<string> values { get; set; } = new list<string>; // not needed  observablecollection<string> values { get; set; } =      new observablecollection<string>(); // not needed 

Comments

Popular posts from this blog

ruby - Trying to change last to "x"s to 23 -

jquery - Clone last and append item to closest class -

c - Unrecognised emulation mode: elf_i386 on MinGW32 -