指定字典键的转换器

本文关键字:转换器 字典 | 更新日期: 2023-09-27 18:03:47

我有一个JSON:

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

我需要将数据反序列化到一个类中:

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

但是MyEnum的值是CodeACodeB,而不是简单的分别为AB

我有一个可以处理转换的自定义转换器。但是我如何指定JsonConverter与字典键一起使用?

指定字典键的转换器

我认为唯一的方法是为整个Dictionary<MyEnum, int>类型或Dictionary<MyEnum, T>制作JsonConverter。

字典键不被视为值,也不会通过json转换器运行。TypeConverters本来是一种解决方案,但是在查看TypeConverters之前,将进入默认的字符串到enum的转换。

所以…我认为没有别的办法了。

编辑:

没有完全测试过,但我在我的一个项目中使用了这样的东西:

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

你需要这个小帮手:

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

希望你能成功

这是一个通用的解决方案,使用json与字典与任何类型的键的问题:

[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;
    }
}

我不能得到任何TypeConverter解决方案工作,不想有一个JsonConverter构建一个字符串键字典,然后复制到一个新的字典,所以我去了这样的东西:

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
}