Skip to content

[Feature Request] Case insensitive parsing of enums #927

Open
@orestesgaolin

Description

@orestesgaolin

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,

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions