Paul
Paul

Reputation: 137

Json.NET serializing/ deserializing nested dictionaries

I have been doing some research on this subject, and other than changing my Dictionary to a Collection of KeyValuePair objects, I have not been able to find any solution for the following case.

I have a very simple model called Client, which has a dictionary as one of it's properties and finally I am trying to serialize and deserialize a Dictionary model.

Here is my code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Newtonsoft.Json;

namespace Automation
{
public class Client
{
    public Client(IList<String> environments)
    {
        this.Environments = new Dictionary<String, Boolean>();
        if (environments != null)
            foreach (String environment in environments)
                this.Environments[environment] = false;
    }

    public Dictionary<String, Boolean> Environments { get; set; }
}

public static class TestClass
{
    public static void Serializer()
    {
        Dictionary<String, Client> newDict = new Dictionary<String, Client>();
        newDict[Guid.NewGuid().ToString()] = new Client(new List<String>() { "A", "B" });
        newDict[Guid.NewGuid().ToString()] = new Client(new List<String>() { "A", "B" });
        newDict[Guid.NewGuid().ToString()] = new Client(new List<String>() { "A", "B" });

        string json = JsonConvert.SerializeObject(newDict, Formatting.Indented);
        try
        {
            Dictionary<String, Client> obj = JsonConvert.DeserializeObject<Dictionary<String, Client>>(json);
        }
        catch
        {
        }
    }
}

}

this is the json that gets generated:

{
"246af598-89b3-4198-b2cb-9f2d8fe6e03e": {
"Environments": {
  "A": false,
  "B": false
  }
},
"fc43bb8d-65d3-4a97-86f3-cb67567845bd": {
"Environments": {
  "A": false,
  "B": false
   }
},
"90c0158a-69e2-42b0-9072-e695c94a2f7a": {
"Environments": {
  "A": false,
  "B": false
   }
 }
}

and here is the exception I am getting when trying to deserialize:

Cannot deserialize the current JSON object (e.g. {"name":"value"}) into type 'System.Collections.Generic.IList`1[System.String]' because the type requires a JSON array (e.g. [1,2,3]) to deserialize correctly. To fix this error either change the JSON to a JSON array (e.g. [1,2,3]) or change the deserialized type so that it is a normal .NET type (e.g. not a primitive type like integer, not a collection type like an array or List) that can be deserialized from a JSON object. JsonObjectAttribute can also be added to the type to force it to deserialize from a JSON object. Path '246af598-89b3-4198-b2cb-9f2d8fe6e03e.Environments.A', line 4, position 11.

Any thoughts, eventually I will be sending this object serialized over the wire via a SignalR request.

Thank you

Upvotes: 1

Views: 5902

Answers (1)

YK1
YK1

Reputation: 7612

Just introduce a default constructor for the de-serializer to work with:

public class Client
{
    public Client()
    { }

    // your other constructor ...
}

Upvotes: 1

Related Questions