google / google/json_serializable.dart
[Feature Request] Case insensitive parsing of enums
- Dominant language
- Dart
- Stars
- 1.6k
- Forks
- 461
- Avg merge
- 45m
- Merged PRs (30d)
- 1
Description
Due to inconsistencies in the data sources my app needs to handle various variants of enum representations e.g.:
```json
{
"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:
```dart
JsonKey(ignoreCase: true)
final MyEnum myEnum;
```
and the generated code would then call `toString().toLowerCase()` on the value being passed to `enumDecodeNullable`:
```dart
$checkedConvert(
json,
'myEnum',
(v) => _$enumDecodeNullable(_$MyEnumEnumMap, v?.toString().toLowerCase(),
unknownValue: MyEnum.unknown)) ??
MyEnum.unknown,
```
Contributor guide
Assessment
This issue has not been assessed yet.