I am creating classes for the Stack Exchange API. The filter_object type contains a member filter_type which will be either safe, unsafe, or invalid. So I created an enum like this:
[JsonConverter(typeof(StringEnumConverter))] public enum FilterType { safe, @unsafe, // Here lies the problem. invalid } Since unsafe is a keyword, I had to add some prefix to it. But how can I make the value "unsafe" to automatically map to @unsafe? Example JSON:
{ "filter": "....", "filter_type": "unsafe", "included_fields": [ "...", "....", "....." ] } How can I deserialize it, such that the filter_type is automatically converted to FilterType.@unsafe?
Update - Solved:
Using the @ symbol before an identifier makes it possible to be the same as keywords. It works fine even though the @ appears in intellisense.
unsafeis a keyword. I already came across prefixing @ so that a keyword can be used as an identifier. It appeared in intellisense and so I thought it was not working. But on testing I found it was correct. Seeing that there were no other answers, I accepted yours. However, that's not what is done. So I unaccepted it and edited my question to include the solution. I don't see any wrong in it._unsafe. Your method will throw an exception.