1

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#.

  • `Enum` is a type in this case. Are you trying to add both ValEnum1 and ValEnum2 to this List? When you are doing inheritence I don't think Enum's are the answer. – Thor Jan 23 '17 at 18:05
  • Yeah, I should've mentioned that the value List is going to be either List or List. I'll edit the post to include that. Is there a better approach, or should I do make some baseclass for all 'sub-Enum' to inherit from? – thebenismightier Jan 23 '17 at 18:07
  • Looks like this is basically a duplicate of [Deserialize specific enum into system.enum in Json.Net](https://stackoverflow.com/questions/31351262). Or maybe this one is closer: [Deserialize Dictionary with enum values in C#](https://stackoverflow.com/questions/38336390). Do either of those answer your question or do you need something more specific? – dbc Jan 24 '17 at 04:42

1 Answers1

0

Newtonsoft serializes Enum default to their numeric value. And then it has no idea how to serialize that numeric value back to Enum. You can use an attribute on your Enum to serialize them to string value (so that your data transfer object is understandable) but to deserialize back to an object you have to implement your own JsonConverter.

It's easier to work with more easily serializable data types.

Thor
  • 498
  • 1
  • 6
  • 15
  • [Here](http://bytefish.de/blog/enums_json_net/) is an article on how if you wish to go down that road. – Thor Jan 23 '17 at 18:28
  • Thanks, I think this applies the best to my usecase since I'm not worried about memory constraints for storing the json with the Enums as strings, and I can still use standard Enums across our applications. – thebenismightier Jan 29 '17 at 22:23