json_serializable.dart
json_serializable.dart copied to clipboard
[Feature Request] Ignore unknown enum values in list
Currently, it seems not to be possible to ignore unknown enum values when serializing.
Given the following example:
enum MyEnum {
a, b, c
}
@JsonSerializable()
class MyModel {
List<MyEnum> enums;
...
}
This will fail for the input {"enums": ["a", "b", "d"]}.
What would be great to see is making this possible:
@JsonSerializable()
class MyModel {
@JsonKey(ignoreUnknownEnums: true)
List<MyEnum> enums;
...
}
which won't fail with the given input but create an object MyModel(enums: [MyEnum.a, MyEnum.b]).
For the generated code something like this should work:
enums: (json['enums'] as List<dynamic>?)
?.map((e) => $enumDecodeNullable(_$DogSleepingPlaceEnumMap, e)).whereNotNull()