json_serializable.dart icon indicating copy to clipboard operation
json_serializable.dart copied to clipboard

[Feature Request] Ignore unknown enum values in list

Open stefangaller opened this issue 3 years ago • 0 comments

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()

stefangaller avatar Aug 31 '22 05:08 stefangaller