C# 使用Json.net反序列化具有接口值的复杂嵌套字典类型

C# 使用Json.net反序列化具有接口值的复杂嵌套字典类型,c#,dictionary,nested,json.net,deserialization,C#,Dictionary,Nested,Json.net,Deserialization,我在尝试使用Json.net反序列化具有接口值的非常复杂的嵌套字典类型时遇到问题。代码位于“”处,所述类型为: public class TypeConverter<T, TSerialized> : CustomCreationConverter<T> where TSerialized : T, new() { public override T Create(Type objectType) { return new TSer

我在尝试使用Json.net反序列化具有接口值的非常复杂的嵌套字典类型时遇到问题。代码位于“”处,所述类型为:

public class TypeConverter<T, TSerialized> : CustomCreationConverter<T>
    where TSerialized : T, new()
{
    public override T Create(Type objectType)
    {
        return new TSerialized();
    }
}

public interface IValue
{
    Dictionary<string, IValue> SomeValues { get; set; }
}

public class Value : IValue
{
    [JsonProperty(ItemConverterType = typeof(TypeConverter<IValue, Value>))]
    public Dictionary<string, IValue> SomeValues { get; set; }
}

public interface ISomeAtrributes
{
    Dictionary<string, object> Attributes { get; set; }
}

public interface IDataItem : ISomeAtrributes
{
    IValue Value { get; set; }
}

public class DataItem : IDataItem
{
    [JsonProperty(ItemConverterType = typeof(TypeConverter<IValue, Value>))]
    public IValue Value { get; set; }

    public Dictionary<string, object> Attributes { get; set; }
}

public interface IBlobItem
{
    TypeXDictionary<IEnumerable<IDataItem>> TypeXDataDictionary { get; set; }
}

public class BlobItem : IBlobItem
{
    public BlobItem()
    {
        TypeXDataDictionary = new TypeXDictionary<IEnumerable<IDataItem>>();
    }

    [JsonProperty(ItemConverterType = typeof(TypeConverter<IEnumerable<IDataItem>, List<DataItem>>))]
    public TypeXDictionary<IEnumerable<IDataItem>> TypeXDataDictionary { get; set; }

}

public class TypeYDictionary<T> : Dictionary<string, T>
{
}

public class TypeXDictionary<T> : Dictionary<string, TypeYDictionary<T>>
{
}
我收到一个例外:

Run-time exception (line 19): Cannot populate JSON object onto type 'System.Collections.Generic.List`1[JsonSerialization.DataItem]'. Path 'TypeXDataDictionary.typeXKey.dataItemKey', line 1, position 50.

Stack Trace:

[Newtonsoft.Json.JsonSerializationException: Cannot populate JSON object onto type 'System.Collections.Generic.List`1[JsonSerialization.DataItem]'. Path 'TypeXDataDictionary.typeXKey.dataItemKey', line 1, position 50.]
  at JsonSerialization.Program.Main(String[] args): line 19

为什么
CustomCreationConverter
不工作?

问题在于您的
BlobItem
类型:

public class BlobItem : IBlobItem
{
    public BlobItem()
    {
        TypeXDataDictionary = new TypeXDictionary<IEnumerable<IDataItem>>();
    }

    [JsonProperty(ItemConverterType = typeof(TypeConverter<IEnumerable<IDataItem>, List<DataItem>>))]
    public TypeXDictionary<IEnumerable<IDataItem>> TypeXDataDictionary { get; set; }
}
因此,其值不是类型
IEnumerable
,而是类型
Dictionary
,转换器将无法工作。您需要的是一个转换器,用于
TypeXDictionary
的项中的项,其定义如下:

var blobItem = new BlobItem();
var dataItemDic = new TypeYDictionary<IEnumerable<IDataItem>>();
var objDic = new Dictionary<string, object> {{"key", "object"}};
dataItemDic.Add("dataItemKey", new List<DataItem>() { new DataItem() { Attributes = objDic } });
blobItem.TypeXDataDictionary.Add("typeXKey", dataItemDic );
var ser = JsonConvert.SerializeObject(blobItem);

var deSerialized = JsonConvert.DeserializeObject<BlobItem>(ser);
public class DictionaryValueTypeConverter<TDictionary, TKey, TValue, TValueSerialized> : JsonConverter
    where TDictionary : class, IDictionary<TKey, TValue>, new()
    where TValueSerialized : TValue
{
    public override bool CanConvert(Type objectType)
    {
        throw new NotImplementedException();
    }

    public override bool CanWrite { get { return false; } }

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

    public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
    {
        if (reader.TokenType == JsonToken.Null)
            return null;
        var surrogate = serializer.Deserialize<Dictionary<TKey, TValueSerialized>>(reader);
        if (surrogate == null)
            return null;
        var dictionary = existingValue as TDictionary ?? new TDictionary();
        foreach (var pair in surrogate)
            dictionary[pair.Key] = pair.Value;
        return dictionary;
    }
}
public class BlobItem : IBlobItem
{
    public BlobItem()
    {
        TypeXDataDictionary = new TypeXDictionary<IEnumerable<IDataItem>>();
    }

    [JsonProperty(ItemConverterType = typeof(DictionaryValueTypeConverter<TypeYDictionary<IEnumerable<IDataItem>>, string, IEnumerable<IDataItem>, List<DataItem>>))]
    public TypeXDictionary<IEnumerable<IDataItem>> TypeXDataDictionary { get; set; }

}
样品

public class BlobItem : IBlobItem
{
    public BlobItem()
    {
        TypeXDataDictionary = new TypeXDictionary<IEnumerable<IDataItem>>();
    }

    [JsonProperty(ItemConverterType = typeof(DictionaryValueTypeConverter<TypeYDictionary<IEnumerable<IDataItem>>, string, IEnumerable<IDataItem>, List<DataItem>>))]
    public TypeXDictionary<IEnumerable<IDataItem>> TypeXDataDictionary { get; set; }

}