I am using Json.NET and I am trying to store a Dictionary mapping from an enum to a list of other enums. The enum Keys are meant to indicate which product, and the value mapped to by that product Key enum is a list of enums that indicate which features are available under it.
I was thinking of having the type of the enum in each value list depend on which product they belong to.
I have the following classes:
public enum KeyEnum {
Key1,
Key2
}
public enum ValEnum1 {
Type1,
Type2
}
public enum ValEnum2 {
Type3,
Type4,
Type5,
Type6
}
class MyClass {
public Dictionary<KeyEnum, List<Enum>> Data { get; set; }
}
I am trying to do a
var tmp = new MyClass()
{
Data = new Dictionary<KeyEnum, List<Enum>>()
{
new List<Enum>()
{
ValEnum2.Type6
}
}
}
string newdata = JsonConvert.SerializeObject(tmp);
var myClass = JsonConvert.DeserializeObject<MyClass>(newdata);
I am getting the followering error:
Newtonsoft.Json.JsonSerializationException: Error converting value 3 to type 'System.Enum'. Path 'Licenses.Audit[0]', line 1, position 160. ---> System.InvalidCastException: Unable to cast object of type 'System.Int64' to type 'System.Enum'.
I'm thinking that I could just convert all of the enum to int instead, but using enums might be better. However, I'm not sure how to properly do this in C#.