Closed AlexKenbo closed 3 years ago
Can you provide us examples where this is needed? What's the goal you want to achieve?
Can you provide us examples where this is needed? What's the goal you want to achieve?
In the date layer, we will receive json, for example: Next, we want to create an Entity there:
jsonOrder = getFromAPI('o-1256');
//{
// "id": "o-1256",
// "positions": [
// {
// "name": "iphone",
// "quantity": 2
// },
// {
// "name": "macbook",
// "quantity": 1
// }
// ]
// }
// class Order {
// String id;
// List<Position> positions;
// }
// class Position {
// String name;
// int quantity;
// }
objboxOrder =Order.fromJson(jsonOrder);
Additional question. Can I get from the find-method one of the order positions, where name = iphone and change its quantity to 4? (Didn't find how to edit the fields. Just pick up the object and search for the position field in loops? And re-write to the box?)
objboxOrder.find(Position.name, 'iphone').set(Position.quantity, 4); // mutate objboxOrder
And then I need to call the
datasource.sendOrderToAPI(objboxOrder.toJson())
OK, this seems to intersect with https://flutter.dev/docs/development/data-and-backend/json.
JSON serialization with code generation means having an external library generate the encoding boilerplate for you. After some initial setup, you run a file watcher that generates the code from your model classes. For example, json_serializable and built_value are these kinds of libraries.
Did you try the approaches from the link?
It's not entirely impossible to have a more direct way of supporting JSON in ObjectBox in the future, but we should first evaluate how existing tools may already be helpful.
Did you try the approaches from the link?
We tried json_serializable, built_value
But we stopped at freezed which includes json_serializable, but is richer in functionality.
If we annotate freezed with an annotation class objectbox. Then we will have freezed methods for objectbox? copyWith, fromJson
As have been discussed above, there are libs to serialize to/from JSON and it's not in scope of ObjectBox to do that as well - it should rather be used in parallel with those packages. I don't even think OP wanted it to be in scope, just making it clear...
This looks like a duplicate of #229 please follow that one or reopen this if you think otherwise.
FYI I've just tried and https://pub.dev/packages/json_serializable works out of the box and you can use it with ObjectBox-dart 1.0 already... https://pub.dev/packages/freezed is a WIP
How can we create an entity from json? fromJson, toJson methods