Skip to content

Option to generate encoder/decoder for enum? #778

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

Closed
PiN73 opened this issue Dec 21, 2020 · 7 comments · Fixed by #989
Closed

Option to generate encoder/decoder for enum? #778

PiN73 opened this issue Dec 21, 2020 · 7 comments · Fixed by #989

Comments

@PiN73
Copy link

PiN73 commented Dec 21, 2020

Currently one can't decode/encode enum values directly from/to string.

For example, I have

enum State {
  @JsonValue('in_review')
  inReview,

  ...
}

and want to do this

String s = 'in_review';
State state = stateFromString(s);

I need either implement stateFromString manually (which will duplicate strings in @JsonValue) or make a wrapper class.

It would be great to have an option to generate from/to string converters by some annotation, e.g.

@JsonSerializable()
enum State {
  ...
@PiN73
Copy link
Author

PiN73 commented Dec 21, 2020

Maybe the generator can also reuse this generated function (like stateFromString and stateFromString) in fromJson/toJson for classes, e.g. if there is

class Article {
  State state;
  ...
}

inside another file

@PiN73
Copy link
Author

PiN73 commented Dec 21, 2020

Also it would be great to have one place for setting unknownEnumValue, e.g.

@JsonEnumSerializable(unknownEnumValue: State.unknown)
enum State {
  ...

which will be used as default in stateFromString/stateFromString functions with possibility to override it

@LichKing-2234
Copy link

You can follow this.
#653 (comment)

@kevmoo
Copy link
Collaborator

kevmoo commented Sep 4, 2021

I have an idea for this, but we'll need to wait for Dart 2.14

kevmoo added a commit that referenced this issue Sep 27, 2021
Allows generating the associated helpers without requiring usage as a field
in a @JsonSerializable class

Fixes #778
@kevmoo
Copy link
Collaborator

kevmoo commented Sep 27, 2021

WIP: #989

@kevmoo kevmoo self-assigned this Sep 27, 2021
kevmoo added a commit that referenced this issue Sep 28, 2021
Allows generating the associated helpers without requiring usage as a field
in a @JsonSerializable class

Fixes #778
@kevmoo
Copy link
Collaborator

kevmoo commented Oct 15, 2021

Landed in https://pub.dev/packages/json_serializable/versions/6.0.0

@HJ29
Copy link

HJ29 commented Apr 4, 2022

Is it possible to convert enum to json value?
eg,

enum StatusCode {
  @JsonValue(200)
  success,
  @JsonValue('500')
  weird,
}

Sometime, I want to display the 200 value, but I cant find a way to decode StatusCode.success to json value 200.
json_serializable use _$EnumNameEnumMap and _$enumDecode to decode, but these aren't exposed to public,
so I cant use same way to decode.

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