summaryrefslogtreecommitdiff
path: root/src/mongo/bson
diff options
context:
space:
mode:
authorKashish Garg <kashish.garg@mongodb.com>2018-08-03 14:40:18 -0400
committerKashish Garg <kashish.garg@mongodb.com>2018-08-08 11:27:32 -0400
commitf10f21467c2c0ae7586c5c9d327f2328f09d655c (patch)
tree99421ecd99a59487f2ece30c78b59b644a01639a /src/mongo/bson
parent7a21994a8155f3b237f7c239798b55c7e9f67a7b (diff)
downloadmongo-f10f21467c2c0ae7586c5c9d327f2328f09d655c.tar.gz
SERVER-36475 Reduce use of stringstream in BSONObj.jsonString
Diffstat (limited to 'src/mongo/bson')
-rw-r--r--src/mongo/bson/bsonelement.cpp13
-rw-r--r--src/mongo/bson/bsonelement.h7
-rw-r--r--src/mongo/bson/bsonobj.cpp18
-rw-r--r--src/mongo/bson/bsonobj.h5
4 files changed, 35 insertions, 8 deletions
diff --git a/src/mongo/bson/bsonelement.cpp b/src/mongo/bson/bsonelement.cpp
index f0ea59acddc..ba44d546370 100644
--- a/src/mongo/bson/bsonelement.cpp
+++ b/src/mongo/bson/bsonelement.cpp
@@ -57,6 +57,14 @@ using std::string;
string BSONElement::jsonString(JsonStringFormat format, bool includeFieldNames, int pretty) const {
std::stringstream s;
+ BSONElement::jsonStringStream(format, includeFieldNames, pretty, s);
+ return s.str();
+}
+
+void BSONElement::jsonStringStream(JsonStringFormat format,
+ bool includeFieldNames,
+ int pretty,
+ std::stringstream& s) const {
if (includeFieldNames)
s << '"' << escape(fieldName()) << "\" : ";
switch (type()) {
@@ -129,7 +137,7 @@ string BSONElement::jsonString(JsonStringFormat format, bool includeFieldNames,
}
break;
case Object:
- s << embeddedObject().jsonString(format, pretty);
+ embeddedObject().jsonStringStream(format, pretty, false, s);
break;
case mongo::Array: {
if (embeddedObject().isEmpty()) {
@@ -151,7 +159,7 @@ string BSONElement::jsonString(JsonStringFormat format, bool includeFieldNames,
if (strtol(e.fieldName(), 0, 10) > count) {
s << "undefined";
} else {
- s << e.jsonString(format, false, pretty ? pretty + 1 : 0);
+ e.jsonStringStream(format, false, pretty ? pretty + 1 : 0, s);
e = i.next();
}
count++;
@@ -303,7 +311,6 @@ string BSONElement::jsonString(JsonStringFormat format, bool includeFieldNames,
string message = ss.str();
massert(10312, message.c_str(), false);
}
- return s.str();
}
namespace {
diff --git a/src/mongo/bson/bsonelement.h b/src/mongo/bson/bsonelement.h
index 43cb8aa206b..09d15a6140d 100644
--- a/src/mongo/bson/bsonelement.h
+++ b/src/mongo/bson/bsonelement.h
@@ -207,9 +207,16 @@ public:
bool full = false,
bool redactValues = false,
int depth = 0) const;
+
std::string jsonString(JsonStringFormat format,
bool includeFieldNames = true,
int pretty = 0) const;
+
+ void jsonStringStream(JsonStringFormat format,
+ bool includeFieldNames,
+ int pretty,
+ std::stringstream& s) const;
+
operator std::string() const {
return toString();
}
diff --git a/src/mongo/bson/bsonobj.cpp b/src/mongo/bson/bsonobj.cpp
index ff03f9aac79..2fd01c2d8e2 100644
--- a/src/mongo/bson/bsonobj.cpp
+++ b/src/mongo/bson/bsonobj.cpp
@@ -114,16 +114,25 @@ BSONObj BSONObj::getOwned() const {
}
string BSONObj::jsonString(JsonStringFormat format, int pretty, bool isArray) const {
- if (isEmpty())
- return isArray ? "[]" : "{}";
+ std::stringstream s;
+ BSONObj::jsonStringStream(format, pretty, isArray, s);
+ return s.str();
+}
- StringBuilder s;
+void BSONObj::jsonStringStream(JsonStringFormat format,
+ int pretty,
+ bool isArray,
+ std::stringstream& s) const {
+ if (isEmpty()) {
+ s << (isArray ? "[]" : "{}");
+ return;
+ }
s << (isArray ? "[ " : "{ ");
BSONObjIterator i(*this);
BSONElement e = i.next();
if (!e.eoo())
while (1) {
- s << e.jsonString(format, !isArray, pretty ? pretty + 1 : 0);
+ e.jsonStringStream(format, !isArray, pretty ? pretty + 1 : 0, s);
e = i.next();
if (e.eoo())
break;
@@ -137,7 +146,6 @@ string BSONObj::jsonString(JsonStringFormat format, int pretty, bool isArray) co
}
}
s << (isArray ? " ]" : " }");
- return s.str();
}
bool BSONObj::valid(BSONVersion version) const {
diff --git a/src/mongo/bson/bsonobj.h b/src/mongo/bson/bsonobj.h
index ee441046c25..2c9f41ceb60 100644
--- a/src/mongo/bson/bsonobj.h
+++ b/src/mongo/bson/bsonobj.h
@@ -254,6 +254,11 @@ public:
int pretty = 0,
bool isArray = false) const;
+ void jsonStringStream(JsonStringFormat format,
+ int pretty,
+ bool isArray,
+ std::stringstream& s) const;
+
/** note: addFields always adds _id even if not specified */
int addFields(BSONObj& from, std::set<std::string>& fields); /* returns n added */