JsonConvert to treat any Enum as case insensitive
Case insensitive enum conversion can lead to more compatibility throughOut a cross-platform as, for best practice, we never use two enum values equivalents case insensitive, and depending on language best practices, some service implemented in a different language may implement the enum differently, so, a case insensitive conversion can save you a lot of discussion time.
First, import the application namespace:
using Codibre.CaseInsensitiveEnum
Now, create a singleton option to use in very needed conversion. It is important to create it singleton to avoid performances issues (not related to this package, though):
readonly static JsonSerializerOptions options = new JsonSerializerOptions
{
Converters = { new CaseInsensitiveEnumConverter() },
PropertyNameCaseInsensitive = true,
PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
};
Finally, use the created option on your Serializer:
var converted = JsonSerializer.Deserialize<MyTargetClass>(serializedValue, options);
That's it! For best practice, is good to create a serializer class, or method extension, centralized to use through your project, guaranteeing, that way, a singleton option definition.
Also, notice that, if we're talking about case insensitive serialization for enum, it is implied that you're serializing it to the enum property name, not the value, so, be aware that using this package for your serialization will lead to that behavior.
Licensed under MIT.