Skip to content

Commit 31c6c50

Browse files
committed
Provide a Flush() API within Writer
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.
1 parent c7703f8 commit 31c6c50

File tree

2 files changed

+11
-3
lines changed

2 files changed

+11
-3
lines changed

include/rapidjson/prettywriter.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ class PrettyWriter : public Writer<OutputStream, SourceEncoding, TargetEncoding,
152152
(void)ret;
153153
RAPIDJSON_ASSERT(ret == true);
154154
if (Base::level_stack_.Empty()) // end of json text
155-
Base::os_->Flush();
155+
Base::Flush();
156156
return true;
157157
}
158158

@@ -176,7 +176,7 @@ class PrettyWriter : public Writer<OutputStream, SourceEncoding, TargetEncoding,
176176
(void)ret;
177177
RAPIDJSON_ASSERT(ret == true);
178178
if (Base::level_stack_.Empty()) // end of json text
179-
Base::os_->Flush();
179+
Base::Flush();
180180
return true;
181181
}
182182

include/rapidjson/writer.h

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,14 @@ class Writer {
267267
return EndValue(WriteRawValue(json, length));
268268
}
269269

270+
//! Flush the output stream.
271+
/*!
272+
Allows the user to flush the output stream immediately.
273+
*/
274+
void Flush() {
275+
os_->Flush();
276+
}
277+
270278
protected:
271279
//! Information for each nested level
272280
struct Level {
@@ -473,7 +481,7 @@ class Writer {
473481
// Flush the value if it is the top level one.
474482
bool EndValue(bool ret) {
475483
if (RAPIDJSON_UNLIKELY(level_stack_.Empty())) // end of json text
476-
os_->Flush();
484+
Flush();
477485
return ret;
478486
}
479487

0 commit comments

Comments
 (0)