Json.NET: Deserializing nested dictionaries

Viewed 31259

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?

6 Answers

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.

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);

@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);
}

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.

Related