-
Notifications
You must be signed in to change notification settings - Fork 401
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
Use the extension methods feature to remove need of most boilerplate #558
Comments
The extension would need to be named, and the invocation of |
Also the auto-magic support for jsonDecode(object) won't work because the
`toJson` call that's wired into dart:convert won't pick up to toJson
function on the extension
…On Mon, Nov 4, 2019 at 1:08 PM Nate Bosch ***@***.***> wrote:
The extension would need to be named, and the invocation of fromJson
would need to reference that name. This would be a breaking change. Instead
of Person.fromJson the usage would look like PersonExtension.fromJson.
—
You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub
<#558>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AAAEFCW5RJADZHNZ4OKHGULQSCFNZANCNFSM4JIYECLA>
.
|
I like the general idea as the current method of having to create the methods in the source class is messy. What about using a It doesn't remove the need to update the source class but does make the update more elegant. class Team with _TeamJson {
Customer owner;
} In team.g.dart class _TeamJson {
factory _TeamJson.fromJson(Map<String, dynamic> json) => _$TeamFromJson(json);
Map<String, dynamic> toJson() => _$TeamToJson(this as Team);
}
Team _$TeamFromJson(Map<String, dynamic> json) {
...
The 'this as Team' expression is a little dodgy but given its generated code it will always be correct. So now the implementation is two words Elegant and non-breaking. |
A |
Why are we concerned about breaking changes? This simplifies everything by a bunch. Don't make the solution extra complicated to keep from breaking. Next we will just need native struct support and get rid of this all together. 😍 |
At the moment, there is only one full-blown solution with zero boilerplate in the model class, based on Almost two years I've looked for a clean serialization/deserialization solution, ended up creating it on my own 😄 |
Awesome, @k-paxian This package is meant to be straight-forward and meet the needs of the Dart team. Glad you're running with something that handles more cases in a different way! |
@natebosch extensions allow to write exactly |
Hrm...not sure... |
Sad that this is closed. |
No, They'd allow us to write
It is closed because it is not actionable. If the language adds new features like the ability to add static interface extensions we can certainly revisit.
That is not the only concern. The |
We should consider re-opening this issue given a recent idea that @DaveShuckerow implemented for a hackathon, see the cereal package. This adds extension methods to the We could similarly just add top level methods Both of these approaches remove all the boilerplate except for the generated part file reference, and result in something very similar to my previous |
Another alternative would be to create a separate package which also depends on |
Thanks for the shout-out, @jakemac53! All, if you're interested in seeing what JSON serialization in Dart looks like with no-boilerplate extension methods, please do try package:cereal. It's not production-ready, but it is a proof-of-concept. If we find that this approach works well, that can help us justify the work to port the functionality back to the full library. |
@jakemac53 – let's talk about this when I'm back in the office. |
Any update on this? I did take a look at |
Lets do this! |
I'm willing to entertain a PR here. It'd be a lot of work! Also: what's the suggestion for handling factories? |
Couldn't you just use a static method? That way you can |
Excited to see this soon! |
Just to set expectations, NO ONE in the Dart org is working on this. If we get a very clean PR, we'll consider it. ...we're very slammed working on null safety – while many of us are dealing with our wonderful children while we work from home. 😄 |
It really felt like you meant to say ""wonderful children"". |
I tried to implement this feature, but I encountered the below issue. Here's the generated code. extension PersonExt on Person {
static Person fromJson(Map<String, dynamic> json) => _$PersonFromJson(json);
Map<String, dynamic> toJson() => _$PersonToJson(this);
} When NoSuchMethodError (NoSuchMethodError: Class 'Person' has no instance method 'toJson'.
Receiver: Instance of 'Person'
Tried calling: toJson()) I did some investigation, it seems that I posted test code #658 (comment) to demo this issue. Any suggestion here? |
See #558 (comment) – I commented this in November. We need dart-lang/language#252 |
Thanks for the reply. Clear get it. |
Closing this out. We'll likely get metaprogramming before we get partial classes. Happy to re-open if language support comes along. |
Starting with Dart 2.6,
json_serializable
has the option to create thetoJson()
method for the user. Although extension methods don't support constructors,json_serializable
could create a static generative method instead. This means that user code would look like this:The generated extension might look like this:
Rest of the code can keep the same.
The text was updated successfully, but these errors were encountered: