A more precise enum converter for Newtonsoft json.
When serializing and deserializing an Enum using Newtonsoft.Json, you may get the problem of knowing what was the specific enum type.
This may happen if your class declares a property of type Enum, instead of the specific enum.
public class TestClass
{
public Enum Enum1 { get; set; }
}
The "Enum1" field is declared as Enum.
This allows it to be filled with any enum.
If you try to fill it with StringComparison enum, using the value OrdinalIgnoreCase, the serialized output, will be:
{"Enum1":5}
The result cannot be acceptable for common uses, because you actually don't know what enum was used.
The serialization lost the information.
This enum converter aims to solve this problem, by extending the out-of-the-box converter "StringEnumConverter", that writes the string value instead of its underlying type (int in this case).
The class we are extending only writes the string value of the enum:
{"Enum1":"OrdinalIgnoreCase"}
StringTypeEnumConverter extends this concept, writing the string value along with the originating enum type name, in this way:
{"Enum1":"StringComparison.OrdinalIgnoreCase"}
When deserializing, it looks for the StringComparison enum type in a given array of assemblies, and returns that type.
For code and more info: https://github.com/jonnidip/StringTypeEnumConverter