I have the following enum which I am using as keys in a Dictionary:
public enum SomeEnum
{
One, Two, Three
}
And the following code throws an exception:
Dictionary<Enum, string> map = new Dictionary<Enum, string>()
{
{SomeEnum.One,"1"},
{SomeEnum.Two,"2"},
{SomeEnum.Three,"3"}
};
string mapStr = JsonConvert.SerializeObject(map, Formatting.None,
new JsonSerializerSettings()
{
TypeNameAssemblyFormatHandling = TypeNameAssemblyFormatHandling.Full
});
var newMap = JsonConvert.DeserializeObject<Dictionary<Enum, string>>(mapStr);
Newtonsoft.Json.JsonSerializationException: ‘Could not convert string ‘One’ to dictionary key type ‘System.Enum’. Create a TypeConverter to convert from the string to the key type object. Path ‘One’, line 1, position 7.’
Probably because the serialized JSON does not contain any type definition that hints about the original run time enum type, and seems like using TypeNameAssemblyFormatHandling
doesn’t help either.
1