summaryrefslogtreecommitdiff
path: root/src/mongo/bson/bsonobj.cpp
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/bsonobj.cpp
parent7a21994a8155f3b237f7c239798b55c7e9f67a7b (diff)
downloadmongo-f10f21467c2c0ae7586c5c9d327f2328f09d655c.tar.gz
SERVER-36475 Reduce use of stringstream in BSONObj.jsonString
Diffstat (limited to 'src/mongo/bson/bsonobj.cpp')
-rw-r--r--src/mongo/bson/bsonobj.cpp18
1 files changed, 13 insertions, 5 deletions
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 {