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

Does the Mappable interface reallly need to exist? #2

Open
KalilDev opened this issue Dec 3, 2020 · 2 comments
Open

Does the Mappable interface reallly need to exist? #2

KalilDev opened this issue Dec 3, 2020 · 2 comments

Comments

@KalilDev
Copy link

KalilDev commented Dec 3, 2020

In dart:convert, in the json to string encoder, the dart objects are passed to the toEncodable function or are treated as dynamic and the toJson method is called https://github.com/dart-lang/sdk/blob/d86f0e2836d0941776f911dfc86ff444121fd29d/sdk/lib/convert/json.dart#L521 , and if an NoSuchMethod is thrown, the encoder catches the error and throws an encoding error.

This library could do something similar. If the object is not an Map<String, dynamic>, then the toJson function gets called and in case it throws a NoSuchMethod, you can add no info, otherwise add the resulting json. This allows using standard codegen libraries, instead of having to wire everything up or making an MappableFromJson mixin which uses the toJson method (what im gonna use rn).

@andrea689
Copy link
Owner

Interesting approach, thanks!
If you want you can do a PR, otherwise I'll try to work on it in my free time.

@KalilDev
Copy link
Author

I created an wip pr with the initial implementation of the proposal

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

No branches or pull requests

2 participants