-
Notifications
You must be signed in to change notification settings - Fork 413
how to convert the enum to the value declared by @JsonValue? #653
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
Comments
Enums work fine. What's the specific problem? |
I create a dart file named |
Oh! You won't get a file generated here. Only for classes
annotated @JsonSerializable
…On Wed, Jun 10, 2020 at 8:04 PM Arthas ***@***.***> wrote:
import 'package:json_annotation/json_annotation.dart';
part 'types.g.dart';
enum ABC {
@JsonValue(0)
HAHA,
@JsonValue(1)
HEHE,
@JsonValue(2)
XIXI
}
I create a dart file named types, then run flutter pub run build_runner
build.
the types.g.dart not create automatic.
—
You are receiving this because you commented.
Reply to this email directly, view it on GitHub
<#653 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AAAEFCXDBD3L25SLCGBDFDTRWBCSZANCNFSM4N2OU7HA>
.
|
I needed this feature badly, I have inheritance and multiple different types. so I need to deserialize type first, then create an instance based on the type. For now, I've copied the generated code to my factory class. is there any other way to do this? |
Could you show a bit more code than just the enum? Could you share what you hope is generated? |
Hi @kevmoo , I just have a string and I want to convert it to enum. I have something like this: enum Type {
text,
image,
unknown,
} and I have a for now, I've copied these to be globally accessible: T _$enumDecode<T>(Map<T, dynamic> enumValues,
dynamic source, {
T unknownValue,
}) {
if (source == null) {
throw ArgumentError('A value must be provided. Supported values: '
'${enumValues.values.join(', ')}');
}
final value = enumValues.entries
.singleWhere((e) => e.value == source, orElse: () => null)
?.key;
if (value == null && unknownValue == null) {
throw ArgumentError('`$source` is not one of the supported values: '
'${enumValues.values.join(', ')}');
}
return value ?? unknownValue;
}
T _$enumDecodeNullable<T>(Map<T, dynamic> enumValues,
dynamic source, {
T unknownValue,
}) {
if (source == null) {
return null;
}
return _$enumDecode<T>(enumValues, source, unknownValue: unknownValue);
}
const _$TypeEnumMap = {
Type.text: 'text',
Type.image: 'image',
Type.unknown: 'unknown',
}; and I'm using it like this: final type = _$enumDecodeNullable(_$TypeEnumMap, json['type'], unknownValue: Type.unknown), I don't think this is too relevant, but it's somehow related: converting a value to enum. And I have used this |
I have forked this repository and support generate the You can use
The best way is to create a new annotation like Here is the pubspec usage:
|
@LichKing-2234 That is absolutely awesome. Great work! This fixes a major deficiency in Dart. |
how can I convert the enum to the value declared by @JsonValue, I don‘t want to create a class to wrap the enum. Pls, help me, thanks!
The text was updated successfully, but these errors were encountered: