@@ -169,30 +169,30 @@ class Writer {
169169 */
170170 // @{
171171
172- bool Null () { Prefix (kNullType ); return WriteNull (); }
173- bool Bool (bool b) { Prefix (b ? kTrueType : kFalseType ); return WriteBool (b); }
174- bool Int (int i) { Prefix (kNumberType ); return WriteInt (i); }
175- bool Uint (unsigned u) { Prefix (kNumberType ); return WriteUint (u); }
176- bool Int64 (int64_t i64 ) { Prefix (kNumberType ); return WriteInt64 (i64 ); }
177- bool Uint64 (uint64_t u64 ) { Prefix (kNumberType ); return WriteUint64 (u64 ); }
172+ bool Null () { Prefix (kNullType ); return EndValue ( WriteNull () ); }
173+ bool Bool (bool b) { Prefix (b ? kTrueType : kFalseType ); return EndValue ( WriteBool (b) ); }
174+ bool Int (int i) { Prefix (kNumberType ); return EndValue ( WriteInt (i) ); }
175+ bool Uint (unsigned u) { Prefix (kNumberType ); return EndValue ( WriteUint (u) ); }
176+ bool Int64 (int64_t i64 ) { Prefix (kNumberType ); return EndValue ( WriteInt64 (i64 ) ); }
177+ bool Uint64 (uint64_t u64 ) { Prefix (kNumberType ); return EndValue ( WriteUint64 (u64 ) ); }
178178
179179 // ! Writes the given \c double value to the stream
180180 /* !
181181 \param d The value to be written.
182182 \return Whether it is succeed.
183183 */
184- bool Double (double d) { Prefix (kNumberType ); return WriteDouble (d); }
184+ bool Double (double d) { Prefix (kNumberType ); return EndValue ( WriteDouble (d) ); }
185185
186186 bool RawNumber (const Ch* str, SizeType length, bool copy = false ) {
187187 (void )copy;
188188 Prefix (kNumberType );
189- return WriteString (str, length);
189+ return EndValue ( WriteString (str, length) );
190190 }
191191
192192 bool String (const Ch* str, SizeType length, bool copy = false ) {
193193 (void )copy;
194194 Prefix (kStringType );
195- return WriteString (str, length);
195+ return EndValue ( WriteString (str, length) );
196196 }
197197
198198#if RAPIDJSON_HAS_STDSTRING
@@ -214,10 +214,7 @@ class Writer {
214214 RAPIDJSON_ASSERT (level_stack_.GetSize () >= sizeof (Level));
215215 RAPIDJSON_ASSERT (!level_stack_.template Top <Level>()->inArray );
216216 level_stack_.template Pop <Level>(1 );
217- bool ret = WriteEndObject ();
218- if (RAPIDJSON_UNLIKELY (level_stack_.Empty ())) // end of json text
219- os_->Flush ();
220- return ret;
217+ return EndValue (WriteEndObject ());
221218 }
222219
223220 bool StartArray () {
@@ -231,10 +228,7 @@ class Writer {
231228 RAPIDJSON_ASSERT (level_stack_.GetSize () >= sizeof (Level));
232229 RAPIDJSON_ASSERT (level_stack_.template Top <Level>()->inArray );
233230 level_stack_.template Pop <Level>(1 );
234- bool ret = WriteEndArray ();
235- if (RAPIDJSON_UNLIKELY (level_stack_.Empty ())) // end of json text
236- os_->Flush ();
237- return ret;
231+ return EndValue (WriteEndArray ());
238232 }
239233 // @}
240234
@@ -255,7 +249,7 @@ class Writer {
255249 \param length Length of the json.
256250 \param type Type of the root of json.
257251 */
258- bool RawValue (const Ch* json, size_t length, Type type) { Prefix (type); return WriteRawValue (json, length); }
252+ bool RawValue (const Ch* json, size_t length, Type type) { Prefix (type); return EndValue ( WriteRawValue (json, length) ); }
259253
260254protected:
261255 // ! Information for each nested level
@@ -460,6 +454,13 @@ class Writer {
460454 }
461455 }
462456
457+ // Flush the value if it is the top level one.
458+ bool EndValue (bool ret) {
459+ if (RAPIDJSON_UNLIKELY (level_stack_.Empty ())) // end of json text
460+ os_->Flush ();
461+ return ret;
462+ }
463+
463464 OutputStream* os_;
464465 internal::Stack<StackAllocator> level_stack_;
465466 int maxDecimalPlaces_;
0 commit comments