@@ -37,6 +37,12 @@ enum PrettyFormatOptions {
3737 kFormatSingleLineArray = 1 // !< Format arrays on a single line.
3838};
3939
40+ enum LineEndingOption {
41+ kLf = 0 , // default line ending \n
42+ kCrLf = 1 , // \r\n for windows
43+ kCr = 2 // \r for Mac
44+ };
45+
4046// ! Writer with indentation and spacing.
4147/* !
4248 \tparam OutputStream Type of ouptut os.
@@ -56,7 +62,7 @@ class PrettyWriter : public Writer<OutputStream, SourceEncoding, TargetEncoding,
5662 \param levelDepth Initial capacity of stack.
5763 */
5864 explicit PrettyWriter (OutputStream& os, StackAllocator* allocator = 0 , size_t levelDepth = Base::kDefaultLevelDepth ) :
59- Base(os, allocator, levelDepth), indentChar_(' ' ), indentCharCount_(4 ), formatOptions_(kFormatDefault ) {}
65+ Base(os, allocator, levelDepth), indentChar_(' ' ), indentCharCount_(4 ), formatOptions_(kFormatDefault ), lineEndingOption_( kLf ) {}
6066
6167
6268 explicit PrettyWriter (StackAllocator* allocator = 0 , size_t levelDepth = Base::kDefaultLevelDepth ) :
@@ -79,6 +85,11 @@ class PrettyWriter : public Writer<OutputStream, SourceEncoding, TargetEncoding,
7985 return *this ;
8086 }
8187
88+ PrettyWriter& SetLineEnding (LineEndingOption lineEndingOption) {
89+ lineEndingOption_ = lineEndingOption;
90+ return *this ;
91+ }
92+
8293 // ! Set pretty writer formatting options.
8394 /* ! \param options Formatting options.
8495 */
@@ -143,7 +154,7 @@ class PrettyWriter : public Writer<OutputStream, SourceEncoding, TargetEncoding,
143154 bool empty = Base::level_stack_.template Pop <typename Base::Level>(1 )->valueCount == 0 ;
144155
145156 if (!empty) {
146- Base::os_-> Put ( ' \n ' );
157+ WriteLineEnding ( );
147158 WriteIndent ();
148159 }
149160 bool ret = Base::WriteEndObject ();
@@ -167,7 +178,7 @@ class PrettyWriter : public Writer<OutputStream, SourceEncoding, TargetEncoding,
167178 bool empty = Base::level_stack_.template Pop <typename Base::Level>(1 )->valueCount == 0 ;
168179
169180 if (!empty && !(formatOptions_ & kFormatSingleLineArray )) {
170- Base::os_-> Put ( ' \n ' );
181+ WriteLineEnding ( );
171182 WriteIndent ();
172183 }
173184 bool ret = Base::WriteEndArray ();
@@ -218,23 +229,23 @@ class PrettyWriter : public Writer<OutputStream, SourceEncoding, TargetEncoding,
218229 }
219230
220231 if (!(formatOptions_ & kFormatSingleLineArray )) {
221- Base::os_-> Put ( ' \n ' );
232+ WriteLineEnding ( );
222233 WriteIndent ();
223234 }
224235 }
225236 else { // in object
226237 if (level->valueCount > 0 ) {
227238 if (level->valueCount % 2 == 0 ) {
228239 Base::os_->Put (' ,' );
229- Base::os_-> Put ( ' \n ' );
240+ WriteLineEnding ( );
230241 }
231242 else {
232243 Base::os_->Put (' :' );
233244 Base::os_->Put (' ' );
234245 }
235246 }
236247 else
237- Base::os_-> Put ( ' \n ' );
248+ WriteLineEnding ( );
238249
239250 if (level->valueCount % 2 == 0 )
240251 WriteIndent ();
@@ -254,10 +265,25 @@ class PrettyWriter : public Writer<OutputStream, SourceEncoding, TargetEncoding,
254265 PutN (*Base::os_, static_cast <typename OutputStream::Ch>(indentChar_), count);
255266 }
256267
268+ void WriteLineEnding () {
269+ switch (lineEndingOption_) {
270+ case kCrLf :
271+ Base::os_->Put (' \r ' );
272+ Base::os_->Put (' \n ' );
273+ break ;
274+ case kCr :
275+ Base::os_->Put (' \r ' );
276+ break ;
277+ default :
278+ Base::os_->Put (' \n ' );
279+ break ;
280+ }
281+ }
282+
257283 Ch indentChar_;
258284 unsigned indentCharCount_;
259285 PrettyFormatOptions formatOptions_;
260-
286+ LineEndingOption lineEndingOption_;
261287private:
262288 // Prohibit copy constructor & assignment operator.
263289 PrettyWriter (const PrettyWriter&);
0 commit comments