Skip to content

A JsonConverter to obtain case insensitive conversion for any enum

Notifications You must be signed in to change notification settings

codibre/dotnet-case-insensitive-enum

Repository files navigation

Actions Status Actions Status Actions Status benchmark Test Coverage Maintainability

JsonConvert to treat any Enum as case insensitive

Why

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.

How to use it

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.

License

Licensed under MIT.

About

A JsonConverter to obtain case insensitive conversion for any enum

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages