@@ -24,6 +24,14 @@ RAPIDJSON_DIAG_OFF(effc++)
2424
2525RAPIDJSON_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
217237private:
218238 // Prohibit copy constructor & assignment operator.
0 commit comments