Skip to content

Commit dc34cbf

Browse files
committed
Reduce string copying and reallocating in Writer::write
1 parent 8aec8d8 commit dc34cbf

File tree

2 files changed

+7
-7
lines changed

2 files changed

+7
-7
lines changed

include/json/writer.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ class JSON_API Writer {
2929
public:
3030
virtual ~Writer();
3131

32-
virtual std::string write(const Value& root) = 0;
32+
virtual const std::string& write(const Value& root) = 0;
3333
};
3434

3535
/** \brief Outputs a Value in <a HREF="http://www.json.org">JSON</a> format
@@ -57,7 +57,7 @@ class JSON_API FastWriter : public Writer {
5757
void omitEndingLineFeed();
5858

5959
public: // overridden from Writer
60-
virtual std::string write(const Value& root);
60+
virtual const std::string& write(const Value& root);
6161

6262
private:
6363
void writeValue(const Value& value);
@@ -101,7 +101,7 @@ class JSON_API StyledWriter : public Writer {
101101
* \param root Value to serialize.
102102
* \return String containing the JSON document that represents the root value.
103103
*/
104-
virtual std::string write(const Value& root);
104+
virtual const std::string& write(const Value& root);
105105

106106
private:
107107
void writeValue(const Value& value);

src/lib_json/json_writer.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -187,8 +187,8 @@ void FastWriter::dropNullPlaceholders() { dropNullPlaceholders_ = true; }
187187

188188
void FastWriter::omitEndingLineFeed() { omitEndingLineFeed_ = true; }
189189

190-
std::string FastWriter::write(const Value& root) {
191-
document_ = "";
190+
const std::string& FastWriter::write(const Value& root) {
191+
document_.clear();
192192
writeValue(root);
193193
if (!omitEndingLineFeed_)
194194
document_ += "\n";
@@ -249,8 +249,8 @@ void FastWriter::writeValue(const Value& value) {
249249
StyledWriter::StyledWriter()
250250
: rightMargin_(74), indentSize_(3), addChildValues_() {}
251251

252-
std::string StyledWriter::write(const Value& root) {
253-
document_ = "";
252+
const std::string& StyledWriter::write(const Value& root) {
253+
document_.clear();
254254
addChildValues_ = false;
255255
indentString_ = "";
256256
writeCommentBeforeValue(root);

0 commit comments

Comments
 (0)