flutter json_serializable tojson does not work properly
Im looking into the Order class example and found that the Item class is not converted to Map.
class Order {
int count;
int itemNumber;
bool isRushed;
Item item;
Map<String, dynamic> toJson() => _$OrderToJson(this);
}
The generated .g file has this:
Map<String, dynamic> _$OrderToJson(Order instance) {
...
writeNotNull('item', instance.item);
...
return val;
}
The item in order map is still of Item type, but Im expecting it to be auto converted to Map as well. the generated .g file should has something like this
writeNotNull('item', instance.item.toJson());
I don't want to manually add this since it will be overwritten when .g file is regenerated. Why is the json_serializable lib not doing such a simple thing, or am I missing something? thanks.
flutter json-serialization
add a comment |
Im looking into the Order class example and found that the Item class is not converted to Map.
class Order {
int count;
int itemNumber;
bool isRushed;
Item item;
Map<String, dynamic> toJson() => _$OrderToJson(this);
}
The generated .g file has this:
Map<String, dynamic> _$OrderToJson(Order instance) {
...
writeNotNull('item', instance.item);
...
return val;
}
The item in order map is still of Item type, but Im expecting it to be auto converted to Map as well. the generated .g file should has something like this
writeNotNull('item', instance.item.toJson());
I don't want to manually add this since it will be overwritten when .g file is regenerated. Why is the json_serializable lib not doing such a simple thing, or am I missing something? thanks.
flutter json-serialization
add a comment |
Im looking into the Order class example and found that the Item class is not converted to Map.
class Order {
int count;
int itemNumber;
bool isRushed;
Item item;
Map<String, dynamic> toJson() => _$OrderToJson(this);
}
The generated .g file has this:
Map<String, dynamic> _$OrderToJson(Order instance) {
...
writeNotNull('item', instance.item);
...
return val;
}
The item in order map is still of Item type, but Im expecting it to be auto converted to Map as well. the generated .g file should has something like this
writeNotNull('item', instance.item.toJson());
I don't want to manually add this since it will be overwritten when .g file is regenerated. Why is the json_serializable lib not doing such a simple thing, or am I missing something? thanks.
flutter json-serialization
Im looking into the Order class example and found that the Item class is not converted to Map.
class Order {
int count;
int itemNumber;
bool isRushed;
Item item;
Map<String, dynamic> toJson() => _$OrderToJson(this);
}
The generated .g file has this:
Map<String, dynamic> _$OrderToJson(Order instance) {
...
writeNotNull('item', instance.item);
...
return val;
}
The item in order map is still of Item type, but Im expecting it to be auto converted to Map as well. the generated .g file should has something like this
writeNotNull('item', instance.item.toJson());
I don't want to manually add this since it will be overwritten when .g file is regenerated. Why is the json_serializable lib not doing such a simple thing, or am I missing something? thanks.
flutter json-serialization
flutter json-serialization
asked Nov 24 '18 at 13:17
Goodbye WorldGoodbye World
17029
17029
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
Now I found the solution, just set this in build.yaml
explicit_to_json = true.
and regenerate the .g file. It should convert it to Map for you now.
add a comment |
json.encode(...)
will attempt to find a toJson()
method on the Item
class, and serialize the item correctly. There is no need for the generated serialization logic to call toJson()
.
You just have to make sure that the Item
class is also annotated with @JsonSerializable()
and implements toJson()
.
Also, you would never call toJson()
manually, instead you pass your order object to json.encode(...)
thanks for your reply @boformer, i do understand encode will convert it correctly but what im looking for is a returned map whereas encode returns a json String. the firestore lib that Im using expect the datas to be of Map type, so I will have to use decode again to convert it back to Map type. I think if .g file should use instance.item.toJson() to save us the extra effort.
– Goodbye World
Nov 25 '18 at 0:33
In that case you probably have to write the serialization code yourself
– boformer
Nov 25 '18 at 9:42
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53458535%2fflutter-json-serializable-tojson-does-not-work-properly%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
Now I found the solution, just set this in build.yaml
explicit_to_json = true.
and regenerate the .g file. It should convert it to Map for you now.
add a comment |
Now I found the solution, just set this in build.yaml
explicit_to_json = true.
and regenerate the .g file. It should convert it to Map for you now.
add a comment |
Now I found the solution, just set this in build.yaml
explicit_to_json = true.
and regenerate the .g file. It should convert it to Map for you now.
Now I found the solution, just set this in build.yaml
explicit_to_json = true.
and regenerate the .g file. It should convert it to Map for you now.
answered Nov 27 '18 at 10:23
Goodbye WorldGoodbye World
17029
17029
add a comment |
add a comment |
json.encode(...)
will attempt to find a toJson()
method on the Item
class, and serialize the item correctly. There is no need for the generated serialization logic to call toJson()
.
You just have to make sure that the Item
class is also annotated with @JsonSerializable()
and implements toJson()
.
Also, you would never call toJson()
manually, instead you pass your order object to json.encode(...)
thanks for your reply @boformer, i do understand encode will convert it correctly but what im looking for is a returned map whereas encode returns a json String. the firestore lib that Im using expect the datas to be of Map type, so I will have to use decode again to convert it back to Map type. I think if .g file should use instance.item.toJson() to save us the extra effort.
– Goodbye World
Nov 25 '18 at 0:33
In that case you probably have to write the serialization code yourself
– boformer
Nov 25 '18 at 9:42
add a comment |
json.encode(...)
will attempt to find a toJson()
method on the Item
class, and serialize the item correctly. There is no need for the generated serialization logic to call toJson()
.
You just have to make sure that the Item
class is also annotated with @JsonSerializable()
and implements toJson()
.
Also, you would never call toJson()
manually, instead you pass your order object to json.encode(...)
thanks for your reply @boformer, i do understand encode will convert it correctly but what im looking for is a returned map whereas encode returns a json String. the firestore lib that Im using expect the datas to be of Map type, so I will have to use decode again to convert it back to Map type. I think if .g file should use instance.item.toJson() to save us the extra effort.
– Goodbye World
Nov 25 '18 at 0:33
In that case you probably have to write the serialization code yourself
– boformer
Nov 25 '18 at 9:42
add a comment |
json.encode(...)
will attempt to find a toJson()
method on the Item
class, and serialize the item correctly. There is no need for the generated serialization logic to call toJson()
.
You just have to make sure that the Item
class is also annotated with @JsonSerializable()
and implements toJson()
.
Also, you would never call toJson()
manually, instead you pass your order object to json.encode(...)
json.encode(...)
will attempt to find a toJson()
method on the Item
class, and serialize the item correctly. There is no need for the generated serialization logic to call toJson()
.
You just have to make sure that the Item
class is also annotated with @JsonSerializable()
and implements toJson()
.
Also, you would never call toJson()
manually, instead you pass your order object to json.encode(...)
answered Nov 24 '18 at 14:26
boformerboformer
4,25221124
4,25221124
thanks for your reply @boformer, i do understand encode will convert it correctly but what im looking for is a returned map whereas encode returns a json String. the firestore lib that Im using expect the datas to be of Map type, so I will have to use decode again to convert it back to Map type. I think if .g file should use instance.item.toJson() to save us the extra effort.
– Goodbye World
Nov 25 '18 at 0:33
In that case you probably have to write the serialization code yourself
– boformer
Nov 25 '18 at 9:42
add a comment |
thanks for your reply @boformer, i do understand encode will convert it correctly but what im looking for is a returned map whereas encode returns a json String. the firestore lib that Im using expect the datas to be of Map type, so I will have to use decode again to convert it back to Map type. I think if .g file should use instance.item.toJson() to save us the extra effort.
– Goodbye World
Nov 25 '18 at 0:33
In that case you probably have to write the serialization code yourself
– boformer
Nov 25 '18 at 9:42
thanks for your reply @boformer, i do understand encode will convert it correctly but what im looking for is a returned map whereas encode returns a json String. the firestore lib that Im using expect the datas to be of Map type, so I will have to use decode again to convert it back to Map type. I think if .g file should use instance.item.toJson() to save us the extra effort.
– Goodbye World
Nov 25 '18 at 0:33
thanks for your reply @boformer, i do understand encode will convert it correctly but what im looking for is a returned map whereas encode returns a json String. the firestore lib that Im using expect the datas to be of Map type, so I will have to use decode again to convert it back to Map type. I think if .g file should use instance.item.toJson() to save us the extra effort.
– Goodbye World
Nov 25 '18 at 0:33
In that case you probably have to write the serialization code yourself
– boformer
Nov 25 '18 at 9:42
In that case you probably have to write the serialization code yourself
– boformer
Nov 25 '18 at 9:42
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53458535%2fflutter-json-serializable-tojson-does-not-work-properly%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown