-
Notifications
You must be signed in to change notification settings - Fork 412
feature: allow customizing enum value serialization via annotations #251
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
Conversation
Question for reviewers (@natebosch , @jakemac53 ) Should I create another annotation class –
|
Just to give some reasoning: Integers are the default representation of enums in many other languages (e.g. C#, Kotlin, TypeScript), and it's also common to serialize enum values as integers, as it is a very efficient format. |
* Require the latest Dart SDK Annotated enums fail without the CFE * Require the latest pkg:analyzer Enum metadata parsing was just recently added * Require the latest pkg:source_gen Uses multiple return values to allow generating shared helpers * Removed several helpers in json_annotation They can now be generated as part of the source, which minimizes versioning issues with helpers. * Small, but breaking changes to Generator API to return multiple values * Added `addMember` API to Context classes to allow helpers to be generated. * Updated integration test code to include an annotated enum * Updated yaml test code to remove now superfluous convert logic and use annotated enums instead Fixes #38
21b6749
to
dd8f50b
Compare
@sir-boformer – added @jakemac53 – ready for review |
/// | ||
/// Can be a [String] or an [int]. | ||
final dynamic value; | ||
const JsonValue(this.value); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: newline above here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ack
return null; | ||
} | ||
|
||
context.addMember(_enumDecodeHelper); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So we will actually be adding one of these each time this helper is called right? Is that once per field using the enum or more than that?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
once per field using the enum – but then they are collapsed.
The storage is a set, so it's only recorded once.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
kk
json_serializable/lib/src/utils.dart
Outdated
/// If [targetType] is not an enum, `null` is returned. | ||
Map<FieldElement, dynamic> enumFieldsMap(DartType targetType) { | ||
if (targetType is InterfaceType && targetType.element.isEnum) { | ||
var value = _enumMapExpando[targetType]; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: return _enumMapExpando[targetType] ??= ...
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
also, I would consider taking moving big anonymous closure out to a private method or even just a local variable so its easier to reason about what is happening here
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ack
@jakemac53 – PTAL |
feature: allow customizing enum value serialization via annotations
Require the latest Dart SDK
Annotated enums fail without the CFE
Require the latest pkg:analyzer
Enum metadata parsing was just recently added
Require the latest pkg:source_gen
Uses multiple return values to allow generating shared helpers
Removed several helpers in json_annotation
They can now be generated as part of the source, which minimizes
versioning issues with helpers.
Small, but breaking changes to Generator API to return multiple
values
Added
addMember
API to Context classes to allow helpers to begenerated.
Updated integration test code to include an annotated enum
Updated yaml test code to remove now superfluous convert logic and use
annotated enums instead
Fixes #38