Skip to content

Commit 5bf1610

Browse files
committed
added option to FastWriter which omits the trailing new line character
1 parent 3515db1 commit 5bf1610

File tree

2 files changed

+8
-2
lines changed

2 files changed

+8
-2
lines changed

include/json/writer.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,8 @@ class JSON_API FastWriter : public Writer {
5454
*/
5555
void dropNullPlaceholders();
5656

57+
void omitEndingLineFeed();
58+
5759
public: // overridden from Writer
5860
virtual std::string write(const Value &root);
5961

@@ -63,6 +65,7 @@ class JSON_API FastWriter : public Writer {
6365
std::string document_;
6466
bool yamlCompatiblityEnabled_;
6567
bool dropNullPlaceholders_;
68+
bool omitEndingLineFeed_;
6669
};
6770

6871
/** \brief Writes a Value in <a HREF="http://www.json.org">JSON</a> format in a

src/lib_json/json_writer.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -180,16 +180,19 @@ Writer::~Writer() {}
180180
// //////////////////////////////////////////////////////////////////
181181

182182
FastWriter::FastWriter()
183-
: yamlCompatiblityEnabled_(false), dropNullPlaceholders_(false) {}
183+
: yamlCompatiblityEnabled_(false), dropNullPlaceholders_(false), omitEndingLineFeed_(false) {}
184184

185185
void FastWriter::enableYAMLCompatibility() { yamlCompatiblityEnabled_ = true; }
186186

187187
void FastWriter::dropNullPlaceholders() { dropNullPlaceholders_ = true; }
188188

189+
void FastWriter::omitEndingLineFeed() { omitEndingLineFeed_ = true; }
190+
189191
std::string FastWriter::write(const Value &root) {
190192
document_ = "";
191193
writeValue(root);
192-
document_ += "\n";
194+
if (!omitEndingLineFeed_)
195+
document_ += "\n";
193196
return document_;
194197
}
195198

0 commit comments

Comments
 (0)