Skip to content

Commit 73063f5

Browse files
authored
Merge pull request Tencent#1340 from lelit/issue1336
Wrap all WriteXxx() calls within EndValue()
2 parents 6a905f9 + c9eabf9 commit 73063f5

File tree

2 files changed

+41
-12
lines changed

2 files changed

+41
-12
lines changed

include/rapidjson/prettywriter.h

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -92,26 +92,26 @@ class PrettyWriter : public Writer<OutputStream, SourceEncoding, TargetEncoding,
9292
*/
9393
//@{
9494

95-
bool Null() { PrettyPrefix(kNullType); return Base::WriteNull(); }
96-
bool Bool(bool b) { PrettyPrefix(b ? kTrueType : kFalseType); return Base::WriteBool(b); }
97-
bool Int(int i) { PrettyPrefix(kNumberType); return Base::WriteInt(i); }
98-
bool Uint(unsigned u) { PrettyPrefix(kNumberType); return Base::WriteUint(u); }
99-
bool Int64(int64_t i64) { PrettyPrefix(kNumberType); return Base::WriteInt64(i64); }
100-
bool Uint64(uint64_t u64) { PrettyPrefix(kNumberType); return Base::WriteUint64(u64); }
101-
bool Double(double d) { PrettyPrefix(kNumberType); return Base::WriteDouble(d); }
95+
bool Null() { PrettyPrefix(kNullType); return Base::EndValue(Base::WriteNull()); }
96+
bool Bool(bool b) { PrettyPrefix(b ? kTrueType : kFalseType); return Base::EndValue(Base::WriteBool(b)); }
97+
bool Int(int i) { PrettyPrefix(kNumberType); return Base::EndValue(Base::WriteInt(i)); }
98+
bool Uint(unsigned u) { PrettyPrefix(kNumberType); return Base::EndValue(Base::WriteUint(u)); }
99+
bool Int64(int64_t i64) { PrettyPrefix(kNumberType); return Base::EndValue(Base::WriteInt64(i64)); }
100+
bool Uint64(uint64_t u64) { PrettyPrefix(kNumberType); return Base::EndValue(Base::WriteUint64(u64)); }
101+
bool Double(double d) { PrettyPrefix(kNumberType); return Base::EndValue(Base::WriteDouble(d)); }
102102

103103
bool RawNumber(const Ch* str, SizeType length, bool copy = false) {
104104
RAPIDJSON_ASSERT(str != 0);
105105
(void)copy;
106106
PrettyPrefix(kNumberType);
107-
return Base::WriteString(str, length);
107+
return Base::EndValue(Base::WriteString(str, length));
108108
}
109109

110110
bool String(const Ch* str, SizeType length, bool copy = false) {
111111
RAPIDJSON_ASSERT(str != 0);
112112
(void)copy;
113113
PrettyPrefix(kStringType);
114-
return Base::WriteString(str, length);
114+
return Base::EndValue(Base::WriteString(str, length));
115115
}
116116

117117
#if RAPIDJSON_HAS_STDSTRING
@@ -146,7 +146,7 @@ class PrettyWriter : public Writer<OutputStream, SourceEncoding, TargetEncoding,
146146
Base::os_->Put('\n');
147147
WriteIndent();
148148
}
149-
bool ret = Base::WriteEndObject();
149+
bool ret = Base::EndValue(Base::WriteEndObject());
150150
(void)ret;
151151
RAPIDJSON_ASSERT(ret == true);
152152
if (Base::level_stack_.Empty()) // end of json text
@@ -170,7 +170,7 @@ class PrettyWriter : public Writer<OutputStream, SourceEncoding, TargetEncoding,
170170
Base::os_->Put('\n');
171171
WriteIndent();
172172
}
173-
bool ret = Base::WriteEndArray();
173+
bool ret = Base::EndValue(Base::WriteEndArray());
174174
(void)ret;
175175
RAPIDJSON_ASSERT(ret == true);
176176
if (Base::level_stack_.Empty()) // end of json text
@@ -201,7 +201,7 @@ class PrettyWriter : public Writer<OutputStream, SourceEncoding, TargetEncoding,
201201
bool RawValue(const Ch* json, size_t length, Type type) {
202202
RAPIDJSON_ASSERT(json != 0);
203203
PrettyPrefix(type);
204-
return Base::WriteRawValue(json, length);
204+
return Base::EndValue(Base::WriteRawValue(json, length));
205205
}
206206

207207
protected:

test/unittest/prettywritertest.cpp

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -339,6 +339,35 @@ TEST(PrettyWriter, MoveCtor) {
339339
}
340340
#endif
341341

342+
TEST(PrettyWriter, Issue_1336) {
343+
#define T(meth, val, expected) \
344+
{ \
345+
StringBuffer buffer; \
346+
PrettyWriter<StringBuffer> writer(buffer); \
347+
writer.meth(val); \
348+
\
349+
EXPECT_STREQ(expected, buffer.GetString()); \
350+
EXPECT_TRUE(writer.IsComplete()); \
351+
}
352+
353+
T(Bool, false, "false");
354+
T(Bool, true, "true");
355+
T(Int, 0, "0");
356+
T(Uint, 0, "0");
357+
T(Int64, 0, "0");
358+
T(Uint64, 0, "0");
359+
T(Double, 0, "0.0");
360+
T(String, "Hello", "\"Hello\"");
361+
#undef T
362+
363+
StringBuffer buffer;
364+
PrettyWriter<StringBuffer> writer(buffer);
365+
writer.Null();
366+
367+
EXPECT_STREQ("null", buffer.GetString());
368+
EXPECT_TRUE(writer.IsComplete());
369+
}
370+
342371
#ifdef __clang__
343372
RAPIDJSON_DIAG_POP
344373
#endif

0 commit comments

Comments
 (0)