From 31c6c50ac66e5728d086260fc4a5d0993faaf683 Mon Sep 17 00:00:00 2001 From: John Stiles Date: Tue, 14 Mar 2017 23:28:59 -0700 Subject: [PATCH] Provide a Flush() API within Writer MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This is helpful if you’re writing code that needs to control flush behavior and you don’t want to pass around your buffer object to each handler function alongside the writer. Seems like an easy convenience to add. --- include/rapidjson/prettywriter.h | 4 ++-- include/rapidjson/writer.h | 10 +++++++++- 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/include/rapidjson/prettywriter.h b/include/rapidjson/prettywriter.h index a9d0f02..2d6a04f 100644 --- a/include/rapidjson/prettywriter.h +++ b/include/rapidjson/prettywriter.h @@ -152,7 +152,7 @@ public: (void)ret; RAPIDJSON_ASSERT(ret == true); if (Base::level_stack_.Empty()) // end of json text - Base::os_->Flush(); + Base::Flush(); return true; } @@ -176,7 +176,7 @@ public: (void)ret; RAPIDJSON_ASSERT(ret == true); if (Base::level_stack_.Empty()) // end of json text - Base::os_->Flush(); + Base::Flush(); return true; } diff --git a/include/rapidjson/writer.h b/include/rapidjson/writer.h index b83b68e..cb7afd5 100644 --- a/include/rapidjson/writer.h +++ b/include/rapidjson/writer.h @@ -267,6 +267,14 @@ public: return EndValue(WriteRawValue(json, length)); } + //! Flush the output stream. + /*! + Allows the user to flush the output stream immediately. + */ + void Flush() { + os_->Flush(); + } + protected: //! Information for each nested level struct Level { @@ -473,7 +481,7 @@ protected: // Flush the value if it is the top level one. bool EndValue(bool ret) { if (RAPIDJSON_UNLIKELY(level_stack_.Empty())) // end of json text - os_->Flush(); + Flush(); return ret; }