flutter json_serializable tojson does not work properly












1















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.










share|improve this question



























    1















    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.










    share|improve this question

























      1












      1








      1








      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.










      share|improve this question














      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






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 24 '18 at 13:17









      Goodbye WorldGoodbye World

      17029




      17029
























          2 Answers
          2






          active

          oldest

          votes


















          1














          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.






          share|improve this answer































            0














            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(...)






            share|improve this answer
























            • 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











            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
            });


            }
            });














            draft saved

            draft discarded


















            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









            1














            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.






            share|improve this answer




























              1














              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.






              share|improve this answer


























                1












                1








                1







                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.






                share|improve this answer













                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.







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 27 '18 at 10:23









                Goodbye WorldGoodbye World

                17029




                17029

























                    0














                    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(...)






                    share|improve this answer
























                    • 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
















                    0














                    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(...)






                    share|improve this answer
























                    • 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














                    0












                    0








                    0







                    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(...)






                    share|improve this answer













                    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(...)







                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    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



















                    • 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


















                    draft saved

                    draft discarded




















































                    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.




                    draft saved


                    draft discarded














                    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





















































                    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







                    Popular posts from this blog

                    Wiesbaden

                    To store a contact into the json file from server.js file using a class in NodeJS

                    Marschland