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

[Feature Request] Enum value with enhanced Enums #1147

Closed
a7md0 opened this issue May 10, 2022 · 9 comments · Fixed by #1203
Closed

[Feature Request] Enum value with enhanced Enums #1147

a7md0 opened this issue May 10, 2022 · 9 comments · Fixed by #1203

Comments

@a7md0
Copy link

a7md0 commented May 10, 2022

Feature request related to Enhanced enums while using JsonValue

  1. Currently, we would have to declare the value twice per Enum value (to allow this package to serialize/deserialize the Enum and to have access to .value)
import 'package:json_annotation/json_annotation.dart';

part 'status_code.g.dart';

enum StatusCode {
  @JsonValue(200)
  success(200),

  @JsonValue(500)
  weird(500),

  const StatusCode(this.value);

  final int value;
}

It would be much better if we could have the generated map EnumMap to use the value specified in the constructor

@a7md0
Copy link
Author

a7md0 commented May 10, 2022

I have no idea how could generation work in the package, but one possibility will be offer some kind of interface EnumValue<T> to be implemented by the Enum, thus making the T value required to be implemented. Then a check for Enums implementing this interface and use the value passed in the constructor to create the EnumMap with the code generation.

@kevmoo
Copy link
Collaborator

kevmoo commented May 11, 2022

Or we could "steal" the JasonValue annotation and put it on the enum definition with the field name – in the case above "value".

The package is not setup to support adding language-specific features until we're ready to bump the min SDK, so it'll likely be a while until we get there.

@kevmoo
Copy link
Collaborator

kevmoo commented Jul 29, 2022

#1180 bumps the min version of the required SDK – we could entertain this now!

@kevmoo kevmoo self-assigned this Jul 29, 2022
@memoriasIT
Copy link

Hey, did this get any updates? I am running the latest version and it's still generating by name. Am I missing something?

@kevmoo
Copy link
Collaborator

kevmoo commented Sep 20, 2022

Looking now!

kevmoo added a commit that referenced this issue Sep 20, 2022
Fixes #1147

Prepare to release json_annotation v4.7.0
kevmoo added a commit that referenced this issue Sep 20, 2022
Fixes #1147

Prepare to release json_annotation v4.7.0
@kevmoo
Copy link
Collaborator

kevmoo commented Sep 21, 2022

This feature has been added as of

https://pub.dev/packages/json_annotation/versions/4.7.0
and
https://pub.dev/packages/json_serializable/versions/6.4.0

@YiannisBourkelis
Copy link

YiannisBourkelis commented Jan 30, 2023

@kevmoo Sorry for coming back on this. Just to make things clear, when using the new syntax of enhanced enums should we still use the @JsonValue annotation on top of each enum value or does the library takes care of it?

Looks that json_convert always generates a string representation of the enum name when there is no @JsonValue and not the declared enum values.

@kevmoo
Copy link
Collaborator

kevmoo commented Jan 30, 2023

Just use @JsonEnum on the enum itself. @JsonValue should no longer be needed.

@YiannisBourkelis
Copy link

@kevmoo Perfect! Thank you

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants