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

Annotate JsonKey for correct usage

Open splangi opened this issue 5 years ago • 6 comments

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', };

splangi avatar Feb 28 '20 10:02 splangi

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

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

knaeckeKami avatar Mar 03 '20 13:03 knaeckeKami

Requires a feature in pkg:meta - https://github.com/dart-lang/sdk/issues/47312

kevmoo avatar Sep 28 '21 20:09 kevmoo

Hi, I have the same problem. Any updates on this situation?

provokateurin avatar Sep 23 '22 13:09 provokateurin

No update @jld3103 – waiting on the linked SDK issue 🤷

kevmoo avatar Sep 23 '22 19:09 kevmoo

Thanks, I figured in my case I can work around it using JsonValue, but it would still be nice to have it supported properly.

provokateurin avatar Sep 23 '22 22:09 provokateurin

Unblocked in https://github.com/dart-lang/sdk/commit/9680d387ec7b683c14d580409abb61f72819cd99

Just need to wait until the min SDK is bumped!

kevmoo avatar Apr 03 '24 18:04 kevmoo