46

When deserializing an object to a Dictionary (JsonConvert.DeserializeObject<IDictionary<string,object>>(json)) nested objects are deserialized to JObjects. Is it possible to force nested objects to be deserialized to Dictionarys?

Daniel
  • 47,404
  • 11
  • 101
  • 179
  • This article provides an easy way to deserialize a nested JSON obejct into a Dictionary: https://buildplease.com/pages/json/ – zirkelc Apr 02 '18 at 12:23
  • May not be Dictionary, but I myself have been deserializing to ExpandoObject to get the same effect. – alans Sep 21 '18 at 18:39

7 Answers7

56

I found a way to convert all nested objects to Dictionary<string,object> by providing a CustomCreationConverter implementation:

class MyConverter : CustomCreationConverter<IDictionary<string, object>>
{
    public override IDictionary<string, object> Create(Type objectType)
    {
        return new Dictionary<string, object>();
    }

    public override bool CanConvert(Type objectType)
    {
        // in addition to handling IDictionary<string, object>
        // we want to handle the deserialization of dict value
        // which is of type object
        return objectType == typeof(object) || base.CanConvert(objectType);
    }

    public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
    {
        if (reader.TokenType == JsonToken.StartObject
            || reader.TokenType == JsonToken.Null)
            return base.ReadJson(reader, objectType, existingValue, serializer);

        // if the next token is not an object
        // then fall back on standard deserializer (strings, numbers etc.)
        return serializer.Deserialize(reader);
    }
}

class Program
{
    static void Main(string[] args)
    {
        var json = File.ReadAllText(@"c:\test.json");
        var obj = JsonConvert.DeserializeObject<IDictionary<string, object>>(
            json, new JsonConverter[] {new MyConverter()});
    }
}

Documentation: CustomCreationConverter with Json.NET

James Newton-King
  • 48,174
  • 24
  • 109
  • 130
AlexD
  • 5,011
  • 2
  • 23
  • 34
14

I had a very similar but slightly more complex need when I ran across this Q. At first I thought maybe I could adapt the accepted answer, but that seemed a bit complicated and I ended up taking a different approach. I was attempting to put a modern JSON layer on top of a legacy C++ API. I'll spare you the details of that, and just say the requirements boil down to:

  • JSON objects become Dictionary<string,object>.

  • JSON arrays become List<object>.

  • JSON values become the corresponding primitive CLR values.

  • The objects and arrays can be infinitely nested.

I first deserialize the request string into a Newtonsoft JSON object and then call my method to convert in accordance with the above requirements:

var jsonObject = JsonConvert.DeserializeObject(requestString);
var apiRequest = ToApiRequest(jsonObject);
// call the legacy C++ API ...

Here is my method that converts to the structure the API expects:

    private static object ToApiRequest(object requestObject)
    {
        switch (requestObject)
        {
            case JObject jObject: // objects become Dictionary<string,object>
                return ((IEnumerable<KeyValuePair<string, JToken>>) jObject).ToDictionary(j => j.Key, j => ToApiRequest(j.Value));
            case JArray jArray: // arrays become List<object>
                return jArray.Select(ToApiRequest).ToList();
            case JValue jValue: // values just become the value
                return jValue.Value;
            default: // don't know what to do here
                throw new Exception($"Unsupported type: {requestObject.GetType()}");
        }
    }

I hope that someone can find this approach useful.

James R.
  • 822
  • 8
  • 17
  • This was really useful. I was unable to decode this kind of response with the given solution. {"id":2,"name":"root","descendants":[{"id":4,"name":"node2","descendants":[{"id":7,"name":"node21"},{"id":8,"name":"node22"}]},{"id":3,"name":"node1","descendants":[{"id":5,"name":"node11"},{"id":6,"name":"node12"}]}]}. Thanks – Pushpa Kumara May 28 '21 at 11:56
2

Alternative/Update:

I needed to deserialize a dictionary of dictionaries of Strings and with current Json.NET (5.0) I did not had to create a CustomConverter, I just used (in VB.Net):

JsonConvert.DeserializeObject(Of IDictionary(Of String, IDictionary(Of String, String)))(jsonString)

Or, in C#:

JsonConvert.DeserializeObject<IDictionary<String, IDictionary<String, String>>(jsonString);
madth3
  • 7,275
  • 12
  • 50
  • 74
2

@AlexD's accepted solution does not work ideally if there is an array in the json. It returns a JArray of JObject instead a List<Dictionary<string, object>>

This can be solved by modifying the ReadJson() method:

public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
    if (reader.TokenType == JsonToken.StartObject || reader.TokenType == JsonToken.Null)
        return base.ReadJson(reader, objectType, existingValue, serializer);

    //if it's an array serialize it as a list of dictionaries
    if(reader.TokenType == JsonToken.ArrayStart)
        return serializer.Deserialize(reader, typeof(List<Dictionary<string, object>>));    


    // if the next token is not an object
    // then fall back on standard deserializer (strings, numbers etc.)
    return serializer.Deserialize(reader);
}
Phillip
  • 631
  • 6
  • 15
1

I have a nested/deep structure of "unknown" dictionaries that is serialized/deserialized to/from C# objects and JSON string. .NET 5.

If I use Newtonsoft it does not work automatically.

If I use System.Text.Json it works automatically.

//does NOT work (newtonDeserialized does not have the same data in the nested Dictionaries as in object):
var newtonSerialized = Newtonsoft.Json.JsonConvert.SerializeObject(object);
var newtonDeserialized = Newtonsoft.Json.JsonConvert.DeserializeObject<WaitlistResponse>(newtonSerialized);

//Works (netDeserialized have the same data in the nested Directories as in object):
var netSerialized = System.Text.Json.JsonSerializer.Serialize(object);
var netDeserialized = System.Text.Json.JsonSerializer.Deserialize<WaitlistResponse>(netSerialized);
HakonIngvaldsen
  • 354
  • 2
  • 12
1

In my case, not everything is nested dictionary. I also have an array that is key-value of primitive types and it throws exception when the object of the array is not a dictionary.

So, based on Phillip S' answer, I came up with

public override object? ReadJson(JsonReader reader, Type objectType, object? existingValue,
            JsonSerializer serializer)
        {
            if (reader.TokenType == JsonToken.StartObject || reader.TokenType == JsonToken.Null)
                return base.ReadJson(reader, objectType, existingValue, serializer);

            //if it's an array serialize it as a list of dictionaries
            if (reader.TokenType == JsonToken.StartArray)
            {
                return serializer.Deserialize(reader, typeof(List<object>)); // instead of List<Dictionary<string, object>>
            }

            // if the next token is not an object
            // then fall back on standard deserializer (strings, numbers etc.)
            return serializer.Deserialize(reader);
        }

Hope it helps those haven't got it working yet.

Seiko Santana
  • 153
  • 1
  • 8
0

First of all you need to serialize your dictionary values.

var settings = new JsonSerializerSettings { TypeNameHandling= TypeNameHandling.All };

var serializeValues = JsonConvert.SerializeObject(nestedDictionaryValues, settings);

//After serialize using deserialize using the same TypeNameHandling.

var deserializeValues = JsonConvert.DeserializeObject(serializeValues, settings);