Json_serializable.dart: Support JsonKey(name) for enums

Created on 28 Feb 2020  路  1Comment  路  Source: google/json_serializable.dart

Currently the parser ignores JsonKey(name) on enums.
This
enum Permission {

@JsonKey(name: "readonly")
READ_ONLY,
@JsonKey(name: "owner")
OWNER,
@JsonKey(name: "member")
MEMBER,

}

should generate:
const _$PermissionEnumMap = {
Permission.READ_ONLY: 'readonly',
Permission.OWNER: 'owner',
Permission.MEMBER: 'member',
};

but it generates:
const _$PermissionEnumMap = {
Permission.READ_ONLY: 'READ_ONLY',
Permission.OWNER: 'OWNER',
Permission.MEMBER: 'MEMBER',
};

enhancement

Most helpful comment

Use @JsonValue for enums.
JsonValue supports strings and ints.

But yeah, this could print a warning that explains this at code generation time

>All comments

Use @JsonValue for enums.
JsonValue supports strings and ints.

But yeah, this could print a warning that explains this at code generation time

Was this page helpful?
0 / 5 - 0 ratings