2

I am using Newtonsoft Json.NET library for serialization/deserialization.

I have problem deserializing the object which has property as Dictionary < int,object>. In dictionary - Object can be any type.

Here is what I have done for Test.

    [DataContract]
    public class MyClass
    {
        [DataMember(Order = 1)]
        public int ID { get; set; }

        [DataMember]
        public string Text { get; set; }

        [DataMember]
        public Dictionary<int,object> sub { get; set; }
    }
    [DataContract]
    public class MyClass2
    {
        [DataMember]
        public int ID2 { get; set; }
        [DataMember]
        public string Text2 { get; set; }
    }

On button click in simple WPF App.

        MyClass c = new MyClass() { ID = 1, Text = "Hello", sub = new Dictionary<int, object>() };

        MyClass2 c2 = new MyClass2() { ID2 = 2, Text2 = "sub1" };
        c.sub.Add(1, c2);

        MyClass2 c3 = new MyClass2() { ID2 = 3, Text2 = "sub2" };
        c.sub.Add(2, c3);

        string file = "c:\\newfile.txt";

        if (File.Exists(file))
            File.Delete(file);

        Newtonsoft.Json.JsonSerializer ser = new Newtonsoft.Json.JsonSerializer();

        File.WriteAllText(file, JsonConvert.SerializeObject(c));

        MyClass o = JsonConvert.DeserializeObject<MyClass>(File.ReadAllText(file));
    }

Serilaized Json String -

{"Text":"Hello","sub":{"1":{"ID2":2,"Text2":"sub1"},"2":{"ID2":3,"Text2":"sub2"}},"ID":1}

At Deserialization - MyClass instance's proerties are resolved but property Dictionary (sub) has still json string which is not resolved to MyClass2.

Can anyone please help me ?

Jeet
  • 245
  • 1
  • 3
  • 15
  • 1
    Try using `TypeNameHandling.All`. See [TypeNameHandling setting](http://www.newtonsoft.com/json/help/html/serializetypenamehandling.htm). – dbc Jun 23 '16 at 08:20
  • See here: [how to deserialize JSON into IEnumerable with Newtonsoft JSON.NET](https://stackoverflow.com/questions/6348215/how-to-deserialize-json-into-ienumerablebasetype-with-newtonsoft-json-net/6495299#6495299). – dbc Jun 23 '16 at 08:22
  • @dbc : I understand that it worked with TypeNameHandling.All. Thank you for the solution. Is that possible to send this json to Javascript object and return it in the same format? – Jeet Jun 23 '16 at 08:24
  • And also here: [Serializing/Deserializing Dictionary of objects with JSON.NET](https://stackoverflow.com/questions/3739094/serializing-deserializing-dictionary-of-objects-with-json-net/3744505#3744505). – dbc Jun 23 '16 at 08:24
  • That sounds like a second question. The preferred format on stackoverflow is [one question per post](https://meta.stackexchange.com/questions/222735/can-i-ask-only-one-question-per-post), where each question includes a reproducible example of the problem. That being said, your second question resembles [How to parse Json.NET polymorphic objects?](https://stackoverflow.com/questions/5953450). – dbc Jun 23 '16 at 08:27
  • @dbc : Thank you for the answer...: ) – Jeet Jun 23 '16 at 08:38

2 Answers2

1

To fix your problem you should change

   [DataMember]
   public Dictionary<int,object> sub { get; set; }

To :

   [DataMember]
   public Dictionary<int,MyClass2> sub { get; set; }

and

MyClass c = new MyClass() { ID = 1, Text = "Hello", sub = new Dictionary<int, object>() };

To

MyClass c = new MyClass() { ID = 1, Text = "Hello", sub = new Dictionary<int, MyClass2>() };

EDIT

To deserialize your MyClass2, MyClassN... you should use TypeNameHandling.All

MyClass o = JsonConvert.DeserializeObject<MyClass>(File.ReadAllText(file), new JsonSerializerSettings() { TypeNameHandling = TypeNameHandling.All });
Quentin Roger
  • 6,410
  • 2
  • 23
  • 36
  • I know that it is the possible way to do it..but in Dictionary there can be multiple type of objects added like MyClass2,MyClass3,MyClass4 and so on. That's why i have named it to Dictionary. – Jeet Jun 23 '16 at 07:59
1

It's not possible to deserialize an object. The Json.NET cannot determine the exact class.

siposz
  • 71
  • 1
  • 3