I am having a problem trying to deserialize a pretty complex nested dictionary type with interface values using Json.net. The code is located here "https://dotnetfiddle.net/JSoAug", and the types in question are:
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>>
{
}
I have several nested levels of collections or dictionaries containing interface objects (with BlobItem
as the root), and at each level I use a subclass of CustomCreationConverter<T>
to deserialize the interfaces as known concrete types. However, in this case, when I attempt to do so as follows:
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);
I receive an exception:
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
Why is the CustomCreationConverter<T>
not working?
The problem is with your
BlobItem
type:For
TypeXDataDictionary
you specify anItemConverterType = typeof(TypeConverter<IEnumerable<IDataItem>, List<DataItem>>)
to indicate how to deserialize the values of theTypeXDataDictionary
. However, this dictionary is actually a dictionary of dictionaries:Thus its values are not of type
IEnumerable<IDataItem>
, they are of typeDictionary<string, IEnumerable<IDataItem>>
and the converter will not work. What you need is a converter for the items of the items of theTypeXDictionary
, which can be defined as follows:Then applied to
BlobItem
as follows:Sample fiddle.