Skip to content

Commit d454d21

Browse files
committed
Merge pull request #577 from comand/prettywriter-options
Add PrettyWriter format option for writing array in single line.
2 parents 1623ef2 + 7a79e91 commit d454d21

File tree

2 files changed

+48
-5
lines changed

2 files changed

+48
-5
lines changed

include/rapidjson/prettywriter.h

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,14 @@ RAPIDJSON_DIAG_OFF(effc++)
2424

2525
RAPIDJSON_NAMESPACE_BEGIN
2626

27+
//! Combination of PrettyWriter format flags.
28+
/*! \see PrettyWriter::SetFormatOptions
29+
*/
30+
enum PrettyFormatOptions {
31+
kFormatDefault = 0, //!< Default pretty formatting.
32+
kFormatSingleLineArray = 1 //!< Format arrays on a single line.
33+
};
34+
2735
//! Writer with indentation and spacing.
2836
/*!
2937
\tparam OutputStream Type of ouptut os.
@@ -43,7 +51,7 @@ class PrettyWriter : public Writer<OutputStream, SourceEncoding, TargetEncoding,
4351
\param levelDepth Initial capacity of stack.
4452
*/
4553
explicit PrettyWriter(OutputStream& os, StackAllocator* allocator = 0, size_t levelDepth = Base::kDefaultLevelDepth) :
46-
Base(os, allocator, levelDepth), indentChar_(' '), indentCharCount_(4) {}
54+
Base(os, allocator, levelDepth), indentChar_(' '), indentCharCount_(4), formatOptions_(kFormatDefault) {}
4755

4856

4957
explicit PrettyWriter(StackAllocator* allocator = 0, size_t levelDepth = Base::kDefaultLevelDepth) :
@@ -61,6 +69,14 @@ class PrettyWriter : public Writer<OutputStream, SourceEncoding, TargetEncoding,
6169
return *this;
6270
}
6371

72+
//! Set pretty writer formatting options.
73+
/*! \param options Formatting options.
74+
*/
75+
PrettyWriter& SetFormatOptions(PrettyFormatOptions options) {
76+
formatOptions_ = options;
77+
return *this;
78+
}
79+
6480
/*! @name Implementation of Handler
6581
\see Handler
6682
*/
@@ -130,7 +146,7 @@ class PrettyWriter : public Writer<OutputStream, SourceEncoding, TargetEncoding,
130146
RAPIDJSON_ASSERT(Base::level_stack_.template Top<typename Base::Level>()->inArray);
131147
bool empty = Base::level_stack_.template Pop<typename Base::Level>(1)->valueCount == 0;
132148

133-
if (!empty) {
149+
if (!empty && !(formatOptions_ & kFormatSingleLineArray)) {
134150
Base::os_->Put('\n');
135151
WriteIndent();
136152
}
@@ -173,11 +189,14 @@ class PrettyWriter : public Writer<OutputStream, SourceEncoding, TargetEncoding,
173189
if (level->inArray) {
174190
if (level->valueCount > 0) {
175191
Base::os_->Put(','); // add comma if it is not the first element in array
176-
Base::os_->Put('\n');
192+
if (formatOptions_ & kFormatSingleLineArray)
193+
Base::os_->Put(' ');
177194
}
178-
else
195+
196+
if (!(formatOptions_ & kFormatSingleLineArray)) {
179197
Base::os_->Put('\n');
180-
WriteIndent();
198+
WriteIndent();
199+
}
181200
}
182201
else { // in object
183202
if (level->valueCount > 0) {
@@ -213,6 +232,7 @@ class PrettyWriter : public Writer<OutputStream, SourceEncoding, TargetEncoding,
213232

214233
Ch indentChar_;
215234
unsigned indentCharCount_;
235+
PrettyFormatOptions formatOptions_;
216236

217237
private:
218238
// Prohibit copy constructor & assignment operator.

test/unittest/prettywritertest.cpp

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,19 @@ static const char kPrettyJson[] =
3939
" \"i64\": -1234567890123456789\n"
4040
"}";
4141

42+
static const char kPrettyJson_FormatOptions_SLA[] =
43+
"{\n"
44+
" \"hello\": \"world\",\n"
45+
" \"t\": true,\n"
46+
" \"f\": false,\n"
47+
" \"n\": null,\n"
48+
" \"i\": 123,\n"
49+
" \"pi\": 3.1416,\n"
50+
" \"a\": [1, 2, 3, -1],\n"
51+
" \"u64\": 1234567890123456789,\n"
52+
" \"i64\": -1234567890123456789\n"
53+
"}";
54+
4255
TEST(PrettyWriter, Basic) {
4356
StringBuffer buffer;
4457
PrettyWriter<StringBuffer> writer(buffer);
@@ -48,6 +61,16 @@ TEST(PrettyWriter, Basic) {
4861
EXPECT_STREQ(kPrettyJson, buffer.GetString());
4962
}
5063

64+
TEST(PrettyWriter, FormatOptions) {
65+
StringBuffer buffer;
66+
PrettyWriter<StringBuffer> writer(buffer);
67+
writer.SetFormatOptions(kFormatSingleLineArray);
68+
Reader reader;
69+
StringStream s(kJson);
70+
reader.Parse(s, writer);
71+
EXPECT_STREQ(kPrettyJson_FormatOptions_SLA, buffer.GetString());
72+
}
73+
5174
TEST(PrettyWriter, SetIndent) {
5275
StringBuffer buffer;
5376
PrettyWriter<StringBuffer> writer(buffer);

0 commit comments

Comments
 (0)