THX-1138
THX-1138

Reputation: 21750

json.net: specify converter for dictionary keys

I have a JSON:

{ 
    "data": { "A": 5, "B": 6 }, 
    "foo": "foo", 
    "bar": "bar" 
}

I need to deserialize data into a class:

public Dictionary<MyEnum, int> Data { get; set; }
public string Foo { get; set; }
public string Bar { get; set; }

But MyEnum values are CodeA, and CodeB instead of simply A and B respectively.

I have a custom Converter that can handle conversion. But how do I specify a JsonConverter to use with Dictionary keys?

Upvotes: 19

Views: 11695

Answers (3)

Tinister
Tinister

Reputation: 11487

I couldn't get any TypeConverter solution working and didn't want to have a JsonConverter that builds a string-key dictionary and then copies everything into a new dictionary, so I went with something like this:

public sealed class MyEnumKeyDictionary<TValue> : IReadOnlyDictionary<MyEnum, TValue>, IDictionary<string, TValue>
{
    private readonly Dictionary<MyEnum, TValue> actual = new Dictionary<MyEnum, TValue>();

    // implement IReadOnlyDictionary implicitly, passing everything from `actual`

    // implement IDictionary explicitly, passing everything into/from `actual` after doing Enum.Parse/Enum.ToString
}

Upvotes: 0

eldar yarden
eldar yarden

Reputation: 1

Here is a generic solution for the problem of using json with Dictionary with any type of key:

[JsonObject]
public class MyKeyValuePair<TKey, TValue>
{
    public TKey MyKey;

    public TValue MyValue;

    [JsonConstructor]
    public MyKeyValuePair()
    {

    }

    public MyKeyValuePair(TKey t1, TValue t2)
    {
        MyKey = t1;
        MyValue = t2;
    }
}



[JsonObject]
public class MyDictionaty<TKey, TValue>

{
    public ICollection<MyKeyValuePair<TKey, TValue>> Collection;

    [JsonConstructor]
    public MyDictionaty()
    {

    }
    public MyDictionaty(Dictionary<TKey, TValue> refund)
    {
        Collection = BuildMyKeyValuePairCollection(refund);
    }
    internal Dictionary<TKey, TValue> ToDictionary()
    {
        return Collection.ToDictionary(pair => pair.MyKey, pair => pair.MyValue);
    }

    private ICollection<MyKeyValuePair<TKey, TValue>> BuildMyKeyValuePairCollection(Dictionary<TKey, TValue> refund)
    {
        return refund.Select(o => new MyKeyValuePair<TKey, TValue>(o.Key, o.Value)).ToList();
    }
}




[JsonObject]
public class ClassWithDictionary
{
    [JsonProperty]
    private readonly MyDictionary<AnyKey, AnyValue> _myDictionary;

    private Dictionary<AnyKey, AnyValue> _dic;

    [JsonConstructor]
    public ClassWithDictionary()
    {

    }
    public ClassWithDictionary(Dictionary<AnyKey, AnyValue> dic)
    {
        _dic= dic;
        _myDictionary = new MyDictionaty<AnyKey, AnyValue>(dic);
    }

    public Dictionary<AnyKey, AnyValue> GetTheDictionary()
    {
        _dic = _dic??_myDictionary.ToDictionary();
        return _dic;
    }
}

Upvotes: 0

asgerhallas
asgerhallas

Reputation: 17724

I believe the only way is to make a JsonConverter for the whole Dictionary<MyEnum, int> type, or Dictionary<MyEnum, T>.

Dictionary keys are not regarded as values and will not be run through the JsonConverters. TypeConverters would have been a solution, but the default string to enum conversion will enter before it looks at the TypeConverters.

So... I don't think it can be done any other way.

EDIT:

Not fully tested, but I use something like this in a project of mine:

public class DictionaryWithSpecialEnumKeyConverter : JsonConverter
{
    public override bool CanWrite
    {
        get { return false; }
    }

    public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
    {
        throw new NotSupportedException();
    }

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

        var valueType = objectType.GetGenericArguments()[1];
        var intermediateDictionaryType = typeof(Dictionary<,>).MakeGenericType(typeof(string), valueType);
        var intermediateDictionary = (IDictionary)Activator.CreateInstance(intermediateDictionaryType);
        serializer.Populate(reader, intermediateDictionary);

        var finalDictionary = (IDictionary)Activator.CreateInstance(objectType);
        foreach (DictionaryEntry pair in intermediateDictionary)
            finalDictionary.Add(Enum.Parse(MyEnum, "Code" + pair.Key, false), pair.Value);

        return finalDictionary;
    }

    public override bool CanConvert(Type objectType)
    {
        return objectType.IsA(typeof(IDictionary<,>)) &&
               objectType.GetGenericArguments()[0].IsA<MyEnum>();
    }
}

You will need this little helper:

    public static bool IsA(this Type type, Type typeToBe)
    {
        if (!typeToBe.IsGenericTypeDefinition)
            return typeToBe.IsAssignableFrom(type);

        var toCheckTypes = new List<Type> { type };
        if (typeToBe.IsInterface)
            toCheckTypes.AddRange(type.GetInterfaces());

        var basedOn = type;
        while (basedOn.BaseType != null)
        {
            toCheckTypes.Add(basedOn.BaseType);
            basedOn = basedOn.BaseType;
        }

        return toCheckTypes.Any(x => x.IsGenericType && x.GetGenericTypeDefinition() == typeToBe);
    }

Hope it works out for you.

Upvotes: 17

Related Questions