Writing rapidjson document to a file using PrettyWriter












0















I have been unable to find a direct answer to this question. After searching for some time, I've written the following code but I'm sure that there exists a simpler way of doing the same task.



int persistJSONChanges(rapidjson::Document& fa_cloneDoc, string jsonFilePath)
{
FILE* lp_file = fopen(jsonFilePath.c_str(), "w");
rapidjson::StringBuffer buffer;
rapidjson::PrettyWriter<rapidjson::StringBuffer> writer(buffer);
fa_cloneDoc.Accept(writer);

string temp=buffer.GetString();
unique_ptr<char>l_writeBuffer(new char[temp.size()]);
rapidjson::FileWriteStream l_writeStream(lp_file, l_writeBuffer.get(), temp.size());
rapidjson::PrettyWriter<rapidjson::FileWriteStream> l_writer(l_writeStream);
bool l_returnStatus=fa_cloneDoc.Accept(l_writer);
if(l_returnStatus==false)
{
cout<<endl<<"file update failed"<<endl;
return -1;
}
fclose(lp_file);
return 0;
}









share|improve this question



























    0















    I have been unable to find a direct answer to this question. After searching for some time, I've written the following code but I'm sure that there exists a simpler way of doing the same task.



    int persistJSONChanges(rapidjson::Document& fa_cloneDoc, string jsonFilePath)
    {
    FILE* lp_file = fopen(jsonFilePath.c_str(), "w");
    rapidjson::StringBuffer buffer;
    rapidjson::PrettyWriter<rapidjson::StringBuffer> writer(buffer);
    fa_cloneDoc.Accept(writer);

    string temp=buffer.GetString();
    unique_ptr<char>l_writeBuffer(new char[temp.size()]);
    rapidjson::FileWriteStream l_writeStream(lp_file, l_writeBuffer.get(), temp.size());
    rapidjson::PrettyWriter<rapidjson::FileWriteStream> l_writer(l_writeStream);
    bool l_returnStatus=fa_cloneDoc.Accept(l_writer);
    if(l_returnStatus==false)
    {
    cout<<endl<<"file update failed"<<endl;
    return -1;
    }
    fclose(lp_file);
    return 0;
    }









    share|improve this question

























      0












      0








      0








      I have been unable to find a direct answer to this question. After searching for some time, I've written the following code but I'm sure that there exists a simpler way of doing the same task.



      int persistJSONChanges(rapidjson::Document& fa_cloneDoc, string jsonFilePath)
      {
      FILE* lp_file = fopen(jsonFilePath.c_str(), "w");
      rapidjson::StringBuffer buffer;
      rapidjson::PrettyWriter<rapidjson::StringBuffer> writer(buffer);
      fa_cloneDoc.Accept(writer);

      string temp=buffer.GetString();
      unique_ptr<char>l_writeBuffer(new char[temp.size()]);
      rapidjson::FileWriteStream l_writeStream(lp_file, l_writeBuffer.get(), temp.size());
      rapidjson::PrettyWriter<rapidjson::FileWriteStream> l_writer(l_writeStream);
      bool l_returnStatus=fa_cloneDoc.Accept(l_writer);
      if(l_returnStatus==false)
      {
      cout<<endl<<"file update failed"<<endl;
      return -1;
      }
      fclose(lp_file);
      return 0;
      }









      share|improve this question














      I have been unable to find a direct answer to this question. After searching for some time, I've written the following code but I'm sure that there exists a simpler way of doing the same task.



      int persistJSONChanges(rapidjson::Document& fa_cloneDoc, string jsonFilePath)
      {
      FILE* lp_file = fopen(jsonFilePath.c_str(), "w");
      rapidjson::StringBuffer buffer;
      rapidjson::PrettyWriter<rapidjson::StringBuffer> writer(buffer);
      fa_cloneDoc.Accept(writer);

      string temp=buffer.GetString();
      unique_ptr<char>l_writeBuffer(new char[temp.size()]);
      rapidjson::FileWriteStream l_writeStream(lp_file, l_writeBuffer.get(), temp.size());
      rapidjson::PrettyWriter<rapidjson::FileWriteStream> l_writer(l_writeStream);
      bool l_returnStatus=fa_cloneDoc.Accept(l_writer);
      if(l_returnStatus==false)
      {
      cout<<endl<<"file update failed"<<endl;
      return -1;
      }
      fclose(lp_file);
      return 0;
      }






      rapidjson






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 20 '18 at 8:22









      Vishal SharmaVishal Sharma

      323322




      323322
























          2 Answers
          2






          active

          oldest

          votes


















          1














          I think you misused the FileWriteStream. It just needs a buffer of arbitrary size.



          You simply needs:



          FILE* fp = fopen(...);
          char buffer[1024];
          FileWriteStream fs(fp, buffer, sizeof(buffer));
          PrettyWriter<FileWriteStream> writer(fs);
          document.Accept(writer);
          fclose(fp);





          share|improve this answer
























          • Is the size of the buffer really arbitrary? Can I use a really small value, regardless of the size of the document?

            – Vishal Sharma
            Nov 30 '18 at 7:23






          • 1





            @VishalSharma Yes. This design is due to performance consideration. The size of buffer can be smaller than the JSON. You may check the source code of FileWriteStream and it is very simple.

            – Milo Yip
            Dec 1 '18 at 15:54



















          0














          I have tried the following which worked for me:



          int persistJSONChanges(rapidjson::Document& fa_cloneDoc, string jsonFilePath)
          {

          rapidjson::StringBuffer buffer;
          rapidjson::PrettyWriter<rapidjson::StringBuffer> writer(buffer);
          bool l_returnStatus=fa_cloneDoc.Accept(writer);
          if(l_returnStatus==false)
          {
          fprintf(stdout,"n[%s::%d] JSON File update failedn",__FILE__,__LINE__);

          return -1;
          }

          string temp=buffer.GetString();
          ofstream out(jsonFilePath.c_str(),std::ofstream::trunc);
          out<<temp;
          return 0;
          }





          share|improve this answer























            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%2f53388845%2fwriting-rapidjson-document-to-a-file-using-prettywriter%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














            I think you misused the FileWriteStream. It just needs a buffer of arbitrary size.



            You simply needs:



            FILE* fp = fopen(...);
            char buffer[1024];
            FileWriteStream fs(fp, buffer, sizeof(buffer));
            PrettyWriter<FileWriteStream> writer(fs);
            document.Accept(writer);
            fclose(fp);





            share|improve this answer
























            • Is the size of the buffer really arbitrary? Can I use a really small value, regardless of the size of the document?

              – Vishal Sharma
              Nov 30 '18 at 7:23






            • 1





              @VishalSharma Yes. This design is due to performance consideration. The size of buffer can be smaller than the JSON. You may check the source code of FileWriteStream and it is very simple.

              – Milo Yip
              Dec 1 '18 at 15:54
















            1














            I think you misused the FileWriteStream. It just needs a buffer of arbitrary size.



            You simply needs:



            FILE* fp = fopen(...);
            char buffer[1024];
            FileWriteStream fs(fp, buffer, sizeof(buffer));
            PrettyWriter<FileWriteStream> writer(fs);
            document.Accept(writer);
            fclose(fp);





            share|improve this answer
























            • Is the size of the buffer really arbitrary? Can I use a really small value, regardless of the size of the document?

              – Vishal Sharma
              Nov 30 '18 at 7:23






            • 1





              @VishalSharma Yes. This design is due to performance consideration. The size of buffer can be smaller than the JSON. You may check the source code of FileWriteStream and it is very simple.

              – Milo Yip
              Dec 1 '18 at 15:54














            1












            1








            1







            I think you misused the FileWriteStream. It just needs a buffer of arbitrary size.



            You simply needs:



            FILE* fp = fopen(...);
            char buffer[1024];
            FileWriteStream fs(fp, buffer, sizeof(buffer));
            PrettyWriter<FileWriteStream> writer(fs);
            document.Accept(writer);
            fclose(fp);





            share|improve this answer













            I think you misused the FileWriteStream. It just needs a buffer of arbitrary size.



            You simply needs:



            FILE* fp = fopen(...);
            char buffer[1024];
            FileWriteStream fs(fp, buffer, sizeof(buffer));
            PrettyWriter<FileWriteStream> writer(fs);
            document.Accept(writer);
            fclose(fp);






            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Nov 30 '18 at 5:12









            Milo YipMilo Yip

            3,4891620




            3,4891620













            • Is the size of the buffer really arbitrary? Can I use a really small value, regardless of the size of the document?

              – Vishal Sharma
              Nov 30 '18 at 7:23






            • 1





              @VishalSharma Yes. This design is due to performance consideration. The size of buffer can be smaller than the JSON. You may check the source code of FileWriteStream and it is very simple.

              – Milo Yip
              Dec 1 '18 at 15:54



















            • Is the size of the buffer really arbitrary? Can I use a really small value, regardless of the size of the document?

              – Vishal Sharma
              Nov 30 '18 at 7:23






            • 1





              @VishalSharma Yes. This design is due to performance consideration. The size of buffer can be smaller than the JSON. You may check the source code of FileWriteStream and it is very simple.

              – Milo Yip
              Dec 1 '18 at 15:54

















            Is the size of the buffer really arbitrary? Can I use a really small value, regardless of the size of the document?

            – Vishal Sharma
            Nov 30 '18 at 7:23





            Is the size of the buffer really arbitrary? Can I use a really small value, regardless of the size of the document?

            – Vishal Sharma
            Nov 30 '18 at 7:23




            1




            1





            @VishalSharma Yes. This design is due to performance consideration. The size of buffer can be smaller than the JSON. You may check the source code of FileWriteStream and it is very simple.

            – Milo Yip
            Dec 1 '18 at 15:54





            @VishalSharma Yes. This design is due to performance consideration. The size of buffer can be smaller than the JSON. You may check the source code of FileWriteStream and it is very simple.

            – Milo Yip
            Dec 1 '18 at 15:54













            0














            I have tried the following which worked for me:



            int persistJSONChanges(rapidjson::Document& fa_cloneDoc, string jsonFilePath)
            {

            rapidjson::StringBuffer buffer;
            rapidjson::PrettyWriter<rapidjson::StringBuffer> writer(buffer);
            bool l_returnStatus=fa_cloneDoc.Accept(writer);
            if(l_returnStatus==false)
            {
            fprintf(stdout,"n[%s::%d] JSON File update failedn",__FILE__,__LINE__);

            return -1;
            }

            string temp=buffer.GetString();
            ofstream out(jsonFilePath.c_str(),std::ofstream::trunc);
            out<<temp;
            return 0;
            }





            share|improve this answer




























              0














              I have tried the following which worked for me:



              int persistJSONChanges(rapidjson::Document& fa_cloneDoc, string jsonFilePath)
              {

              rapidjson::StringBuffer buffer;
              rapidjson::PrettyWriter<rapidjson::StringBuffer> writer(buffer);
              bool l_returnStatus=fa_cloneDoc.Accept(writer);
              if(l_returnStatus==false)
              {
              fprintf(stdout,"n[%s::%d] JSON File update failedn",__FILE__,__LINE__);

              return -1;
              }

              string temp=buffer.GetString();
              ofstream out(jsonFilePath.c_str(),std::ofstream::trunc);
              out<<temp;
              return 0;
              }





              share|improve this answer


























                0












                0








                0







                I have tried the following which worked for me:



                int persistJSONChanges(rapidjson::Document& fa_cloneDoc, string jsonFilePath)
                {

                rapidjson::StringBuffer buffer;
                rapidjson::PrettyWriter<rapidjson::StringBuffer> writer(buffer);
                bool l_returnStatus=fa_cloneDoc.Accept(writer);
                if(l_returnStatus==false)
                {
                fprintf(stdout,"n[%s::%d] JSON File update failedn",__FILE__,__LINE__);

                return -1;
                }

                string temp=buffer.GetString();
                ofstream out(jsonFilePath.c_str(),std::ofstream::trunc);
                out<<temp;
                return 0;
                }





                share|improve this answer













                I have tried the following which worked for me:



                int persistJSONChanges(rapidjson::Document& fa_cloneDoc, string jsonFilePath)
                {

                rapidjson::StringBuffer buffer;
                rapidjson::PrettyWriter<rapidjson::StringBuffer> writer(buffer);
                bool l_returnStatus=fa_cloneDoc.Accept(writer);
                if(l_returnStatus==false)
                {
                fprintf(stdout,"n[%s::%d] JSON File update failedn",__FILE__,__LINE__);

                return -1;
                }

                string temp=buffer.GetString();
                ofstream out(jsonFilePath.c_str(),std::ofstream::trunc);
                out<<temp;
                return 0;
                }






                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 30 '18 at 7:18









                Vishal SharmaVishal Sharma

                323322




                323322






























                    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%2f53388845%2fwriting-rapidjson-document-to-a-file-using-prettywriter%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







                    這個網誌中的熱門文章

                    Hercules Kyvelos

                    Tangent Lines Diagram Along Smooth Curve

                    Yusuf al-Mu'taman ibn Hud