Open DamianSuess opened 4 years ago
As a workaround you might be able to add a formatter like this:
public class CustomDictionaryFormatter : IJsonFormatter<DictIntKey>
{
public DictIntKey Deserialize(ref JsonReader reader, IJsonFormatterResolver formatterResolver)
{
return new DictIntKey(formatterResolver.GetFormatterWithVerify<Dictionary<int, string>>().Deserialize(ref reader, formatterResolver));
}
public void Serialize(ref JsonWriter writer, DictIntKey value, IJsonFormatterResolver formatterResolver)
{
formatterResolver.GetFormatterWithVerify<Dictionary<int, string>>().Serialize(ref writer, value, formatterResolver);
}
}
Although this is sub optimal since it crates the dictionary twice. Better option would be to write it from scratch.
Unable to Deserialize a class that inherits a
dictionary
when usingint
as its key. i.e.Dictionary<int, T>
. The use case for this is pulling info from a database and storing the identity in the key and it's object data in the value. Below I'm using astring
as the value for a simple demonstration.Instances which do serialize and deserialize are:
Dictionary<int, string>
(demonstrated below)class Foo : Dictionary<string, string>
)Output
Exception thrown:
'System.ArgumentException' in System.Private.CoreLib.dll An exception of type 'System.ArgumentException' occurred in System.Private.CoreLib.dll but was not handled in user code The value "1" is not of type "System.Int32" and cannot be used in this generic collection.
Test Class
Unit Test - Fails
Output before exception
Unit Test - Passes
This snip below shows that you can use a
Dictionary<int, T>
.Output: