Custom converter for Dictionary<enum, T> collection

Jul 26, 2011 at 11:36 PM

I have a JSON: {...data: { "A": 5, "B": 6 }...}

I need to deserialize data into a

Dictionary<MyEnum, int>data { get; set; }

But MyEnum values are "CodeA", and "CodeB" instead of simply "A" and "B" respectively.

I have a Converter that can handle conversion. How do I specify a JsonConverter for Dictionary keys?

Aug 10, 2011 at 7:46 AM

I believe the only way is to make a JsonConverter for the whole Dictionary<MyEnum, int> type, or Dictionary<MyEnum, T>. 

Dictionary keys are not regarded as values and will not be run through the JsonConverters. TypeConverters would have been a solution, but the default string to enum conversion will enter before it looks at the TypeConverters. 

So... I don't think it can be done any other way.

 

Regards, Asger