Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

pkg:json - Support for enums #55760

Open
pattobrien opened this issue May 17, 2024 · 2 comments
Open

pkg:json - Support for enums #55760

pattobrien opened this issue May 17, 2024 · 2 comments
Labels
area-pkg Used for miscellaneous pkg/ packages not associated with specific area- teams. feature-macros Implementation of the macros feature pkg-json The json macro package

Comments

@pattobrien
Copy link

The following code snippet throws an error for enums.

@JsonCodable()
class Foo {
  final MyEnum myEnum;
  //    ^^^^^^ ERROR: Only classes are supported as field types for serializable classes dart(macro_error)
}

enum MyEnum { a }

I realize that EnumDeclarations are generally not yet supported by macros, but since this is basic functionality preventing any future migration, that this would be beneficial to document.

info

- Dart 3.5.0-159.0.dev (dev) (Wed May 15 13:03:12 2024 -0700) on "macos_arm64"
- on macos / Version 14.4.1 (Build 23E224)
- locale is en-US
@lrhn lrhn added feature-macros Implementation of the macros feature area-pkg Used for miscellaneous pkg/ packages not associated with specific area- teams. pkg-json The json macro package labels May 19, 2024
@FarisArmoush
Copy link

FarisArmoush commented May 29, 2024

@JsonCodable annotation already supports enums, but you have to make one extra step for it to work, which is adding @JsonValue annotation for each value in the enum.

enum Status {
  @JsonValue('active')
  active,
  @JsonValue('inactive')
  inactive,
  @JsonValue('pending')
  pending,
}

@1l0
Copy link

1l0 commented May 31, 2024

@JsonCodable annotation already supports enums, but you have to make one extra step for it to work, which is adding @JsonValue annotation for each value in the enum.

enum Status {
  @JsonValue('active')
  active,
  @JsonValue('inactive')
  inactive,
  @JsonValue('pending')
  pending,
}

It doesn't work for me. I need this too.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-pkg Used for miscellaneous pkg/ packages not associated with specific area- teams. feature-macros Implementation of the macros feature pkg-json The json macro package
Projects
None yet
Development

No branches or pull requests

4 participants