2016-11-17 18 views
2

我遇到了一個問題,試圖使用Json.net反序列化具有接口值的相當複雜的嵌套字典類型。的代碼位於此處「https://dotnetfiddle.net/JSoAug」,並且所討論的類型有:使用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>> 
{ 
} 

我有集合或含有界面對象(具有BlobItem作爲根)的字典的幾個嵌套層,並且在每個級別我使用CustomCreationConverter<T>的子類將接口反序列化爲已知的具體類型。然而,在這種情況下,當我試圖這樣做如下:

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

我收到異常:

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<T>

回答

0

問題是與您的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; } 
} 

對於TypeXDataDictionary您指定ItemConverterType = typeof(TypeConverter<IEnumerable<IDataItem>, List<DataItem>>)指示如何反序列化TypeXDataDictionary的值。然而,這本詞典實際上是一個字典詞典的:

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

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

因此,它的值是IEnumerable<IDataItem>型不,他們是Dictionary<string, IEnumerable<IDataItem>>類型和轉換器將無法正常工作。你需要的是對TypeXDictionary的項目,它可以被定義如下的項目轉換器:

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

然後如下應用於BlobItem

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

} 

fiddle

相關問題