Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Deserialize to IEnumerable class using Newtonsoft Json.Net

Tags:

I have a project that is currently using Json.Net for Json deserialization classes like these:

public class Foo {     public Guid FooGuid { get; set; }     public string Name { get; set; }     public List<Bar> Bars { get; set; } }  public class Bar {     public Guid BarGuid { get; set; }     public string Description { get; set; } } 

So far it works fine.

To make iteration simpler at one point I made Foo class implement IEnumerable<Bar> like this:

public class Foo : IEnumerable<Bar> {     public Guid FooGuid { get; set; }     public string Name { get; set; }     public List<Bar> Bars { get; set; }      public IEnumerator<Bar> GetEnumerator()     {         return Bars.GetEnumerator();     }      IEnumerator IEnumerable.GetEnumerator()     {         return GetEnumerator();     } }  public class Bar {     public Guid BarGuid { get; set; }     public string Description { get; set; } } 

But then it fails deserializing the object. The error is confusing, as it said that it cannot deserialize FooGuid field, but removing the IEnumerable interface works again.

The problem is the same in both MonoTouch and MonoDroid environments in simulator or device.

Any clue about how to make it work?


Added code to reproduce this issue:

public static class Tests {     public static void SerializeAndDeserializeFoo() {         // Serialize and deserialize Foo class         var element = new Foo         {             FooGuid = Guid.NewGuid(),             Name = "Foo name",             Bars = new List<Bar> {                 new Bar { BarGuid = Guid.NewGuid(), Description = "Bar description" },                 new Bar { BarGuid = Guid.NewGuid(), Description = "Bar description" },                 new Bar { BarGuid = Guid.NewGuid(), Description = "Bar description" }             }         };          var serializedObject = JsonConvert.SerializeObject(element);         Console.WriteLine("Serialized Foo element: {0}", serializedObject);          // Exception if Foo implements IEnumerable         var deserializedObject = JsonConvert.DeserializeObject<Foo>(serializedObject);         Console.WriteLine("Foo deserialization worked!");     }      public static void SerializeAndDeserializeEnumerableFoo() {         // Serialize and deserialize Foo class         var element = new EnumerableFoo         {             FooGuid = Guid.NewGuid(),             Name = "Foo name",             Bars = new List<Bar> {                 new Bar { BarGuid = Guid.NewGuid(), Description = "Bar description" },                 new Bar { BarGuid = Guid.NewGuid(), Description = "Bar description" },                 new Bar { BarGuid = Guid.NewGuid(), Description = "Bar description" }             }         };          var serializedObject = JsonConvert.SerializeObject(element);         Console.WriteLine("Serialized EnumerableFoo element: {0}", serializedObject);          try {             // Exception if Foo implements IEnumerable             var deserializedObject = JsonConvert.DeserializeObject<EnumerableFoo>(serializedObject);             Console.WriteLine("EnumerableFoo deserialization worked!");         }         catch (Exception e){             Console.WriteLine("EnumerableFoo deserialization failed!");             throw;         }     } }  public class EnumerableFoo : IEnumerable<Bar> {     public Guid FooGuid { get; set; }     public string Name { get; set; }     public List<Bar> Bars { get; set; }      public IEnumerator<Bar> GetEnumerator()     {         return Bars.GetEnumerator();     }      IEnumerator IEnumerable.GetEnumerator()     {         return GetEnumerator();     } }  public class Foo {     public Guid FooGuid { get; set; }     public string Name { get; set; }     public List<Bar> Bars { get; set; } }  public class Bar {     public Guid BarGuid { get; set; }     public string Description { get; set; } } 

Sample project: https://www.dropbox.com/s/27i58aiz71dylkw/IEnumerableJson.zip

like image 968
redent84 Avatar asked Sep 29 '13 16:09

redent84


1 Answers

From the Json.Net documentation:

IEnumerable, Lists and Arrays

.NET lists (types that inherit from IEnumerable) and .NET arrays are converted to JSON arrays. Because JSON arrays only support a range of values and not properties, any additional properties and fields declared on .NET collections are not serialized. In situations where a JSON array is not wanted the JsonObjectAttribute can be placed on a .NET type that implements IEnumerable to force the type to be serialized as a JSON object instead.

In other words, since your class implements IEnumerable<T>, Json.Net thinks it is a list. To get around this, simply decorate your class with the [JsonObject] attribute. This will force Json.Net to serialize and deserialize it as a normal class, which is what you want in this case.

[JsonObject] public class EnumerableFoo : IEnumerable<Bar> {     ... } 
like image 115
Brian Rogers Avatar answered Oct 20 '22 13:10

Brian Rogers