Skip to content

[Feature Request] Case insensitive parsing of enums #927

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
orestesgaolin opened this issue Jun 30, 2021 · 6 comments
Open

[Feature Request] Case insensitive parsing of enums #927

orestesgaolin opened this issue Jun 30, 2021 · 6 comments

Comments

@orestesgaolin
Copy link

Due to inconsistencies in the data sources my app needs to handle various variants of enum representations e.g.:

{
  "myEnum": "VALUE"
}
{
  "myEnum": "value"
}

I wish there was a way to tell json_serializable to ignore the case differences and parse these values to MyEnum.value.

The API I would love to see could be:

In Dart:

JsonKey(ignoreCase: true)
final MyEnum myEnum;

and the generated code would then call toString().toLowerCase() on the value being passed to enumDecodeNullable:

      $checkedConvert(
              json,
              'myEnum',
              (v) => _$enumDecodeNullable(_$MyEnumEnumMap, v?.toString().toLowerCase(),
                  unknownValue: MyEnum.unknown)) ??
          MyEnum.unknown,
@bantunes22
Copy link

Any news on this?

@kevmoo
Copy link
Collaborator

kevmoo commented Nov 22, 2021

Turns out this is more gnarly to do that you'd think. It's in my backlog, but not super high priority.

Friendly reminder: I'm the only maintainer of this package – and purely in my free time.

@romk1n
Copy link

romk1n commented Mar 10, 2023

it would be great to support this even for keys

@t-beckmann
Copy link

This could be realized by supporting the more general feature #1036.

@kevmoo
Copy link
Collaborator

kevmoo commented May 23, 2023

@t-beckmann – you can also do this now by just adding toJson/fromJson to an enum class.

@t-beckmann
Copy link

Should cover most cases, except for enums one can not modify for some reason, but that's rare. Could be closed...?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants