-
Notifications
You must be signed in to change notification settings - Fork 28.5k
Plugin: Object to JSON mapper #9318
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
Little bit off topic here, but when mentioning storing objects in the key-value database, what database do you have in mind? As far as I know, there is currently no designated solution, but I'm also looking for one. |
See also #9236 which is a documentation request around json handling. |
@hundeva I am using https://github.com/tekartik/sembast.dart but this is more like off topic @eseidelGoogle I saw those tickets but none of them solves the problem. There is no easy way to map object to JSON and back. And this is what other frameworks like React Native provide out of the box. |
@dbacinski Thanks, I decided to use sembast for now as well. On topic: I think what we would like is something like JSON to Object mapping: Pojo pojo = new Gson().fromJson(jsonString, Pojo.class); Object to JSON mapping: String jsonString = new Gson().toJson(pojo); By default, this wouldn't really work because of no I'm yet to dig in to the build process, so I have no idea how much annotation processing is supported at the moment. Alternatively, if annotation processing is not supported, I'd be fine with writing (de)serializers where I handle |
@hundeva this should work fine with a code generator similar to what built_value uses or other packages that use code generation. I'd suggest trying built_value first. I think it's immutability approach is a great fit for Flutter. |
Yeah, I've checked out built_value some time ago, my problem with that is, the developer needs to write some non trivial boilerplate by hand, which is fairly error prone, at least I had some trouble with it. :) Also, the code generation has to be invoked manually, and the generated code has to be pushed to the repos as well. I found it easier to fallback to manually parsing the objects from maps, which is still not the best solution. |
AFAIK think this can't be avoided except by using mirrors I'd expect code generation to be integrated better into the build process. Not sure how the Flutter team sees this but for normal Dart development (server, web) this the direction they are heading AFAIK.
I think it's rather trivial (just my opinion of course) and can be made even easier by using for example something like live templates of IntelliJ IDEs (most other IDEs have similar features) |
Yeah, and this is what I would skip, if possible. Again, I can bring an android example only (as that is what I have worked with for the last 5 years), android-apt does all this for you, and you don't need to check in any generated code.
That is the direction that I would head, fingers crossed.
I will surely give it another shot, I'm just exploring flutter in my free time for now. |
@hundeva android-apt is deprecated in the latest Android Gradle Plugin because it already provides one. |
I am going to close this. Flutter apps work with a variety of serialization formats, not just JSON, and there are a few packages available already:
If the goal is to add a language-level support, consider filing an issue for the Dart language: https://github.com/dart-lang/sdk/issues. |
This thread has been automatically locked since there has not been any recent activity after it was closed. If you are still experiencing a similar issue, please open a new bug, including the output of |
It would be nice to have Flutter support for mapping objects to JSON and JSON to object.
Currently to achieve that I have to transcode object to Map. There is no clean way to do that because Flutter do not support
dart:mirrors
. Only way to do that without code written by hand is to use https://github.com/google/built_value.dart but it is still far from perfect and it needs additional setup.When I have a Map then I can use JSON.encode to produce JSON string.
This is a code that I am currently using:
Would be nice to be able to call
JSON.encode
directly on object and get JSON string, and convert back JSON string to object usingJSON.decode
. Or at least to have similar functions to transcode objects to Maps.It would help a lot in the use cases:
The text was updated successfully, but these errors were encountered: