diff options
author | Henrik Edin <henrik.edin@mongodb.com> | 2019-12-18 15:57:45 +0000 |
---|---|---|
committer | evergreen <evergreen@mongodb.com> | 2019-12-18 15:57:45 +0000 |
commit | b14cfb55e25db5a7f05355054f687bcc73779ee2 (patch) | |
tree | cc4ba7c7432b18a90f2b73be074d3a7000a0152b /src/mongo/bson/bsonobj.cpp | |
parent | 58bae64c6c66c577328e4171dc96889bcc2157a5 (diff) | |
download | mongo-b14cfb55e25db5a7f05355054f687bcc73779ee2.tar.gz |
SERVER-44623 Rework JSON generation from BSONObj
- Extended Canonical/Relaxed 2.0.0 is supported
- Using libfmt instead of stringstream as internal buffer
- JSON parser can parse Extended Canonical/Relaxed and Strict formats
- Removed TenGen format
Diffstat (limited to 'src/mongo/bson/bsonobj.cpp')
-rw-r--r-- | src/mongo/bson/bsonobj.cpp | 82 |
1 files changed, 61 insertions, 21 deletions
diff --git a/src/mongo/bson/bsonobj.cpp b/src/mongo/bson/bsonobj.cpp index 15e08aefca6..6fab09e94ab 100644 --- a/src/mongo/bson/bsonobj.cpp +++ b/src/mongo/bson/bsonobj.cpp @@ -34,6 +34,9 @@ #include "mongo/base/data_range.h" #include "mongo/bson/bson_validate.h" #include "mongo/bson/bsonelement_comparator_interface.h" +#include "mongo/bson/generator_extended_canonical_2_0_0.h" +#include "mongo/bson/generator_extended_relaxed_2_0_0.h" +#include "mongo/bson/generator_legacy_strict.h" #include "mongo/db/json.h" #include "mongo/util/allocator.h" #include "mongo/util/hex.h" @@ -140,39 +143,76 @@ BSONObj BSONObj::getOwned(const BSONObj& obj) { return obj.getOwned(); } -std::string BSONObj::jsonString(JsonStringFormat format, int pretty, bool isArray) const { - std::stringstream s; - BSONObj::jsonStringStream(format, pretty, isArray, s); - return s.str(); -} - -void BSONObj::jsonStringStream(JsonStringFormat format, - int pretty, - bool isArray, - std::stringstream& s) const { +template <typename Generator> +void BSONObj::_jsonStringGenerator(const Generator& g, + int pretty, + bool isArray, + fmt::memory_buffer& buffer) const { if (isEmpty()) { - s << (isArray ? "[]" : "{}"); + fmt::format_to(buffer, "{}", isArray ? "[]" : "{}"); return; } - s << (isArray ? "[ " : "{ "); + buffer.push_back(isArray ? '[' : '{'); + BSONObjIterator i(*this); BSONElement e = i.next(); if (!e.eoo()) while (1) { - e.jsonStringStream(format, !isArray, pretty ? pretty + 1 : 0, s); + e.jsonStringGenerator(g, !isArray, pretty, buffer); e = i.next(); - if (e.eoo()) + if (e.eoo()) { + g.writePadding(buffer); break; - s << ","; + } + buffer.push_back(','); if (pretty) { - s << '\n'; - for (int x = 0; x < pretty; x++) - s << " "; - } else { - s << " "; + fmt::format_to(buffer, "{: <{}}", '\n', pretty * 2); } } - s << (isArray ? " ]" : " }"); + + buffer.push_back(isArray ? ']' : '}'); +} + +void BSONObj::jsonStringGenerator(ExtendedCanonicalV200Generator const& generator, + int pretty, + bool isArray, + fmt::memory_buffer& buffer) const { + _jsonStringGenerator(generator, pretty, isArray, buffer); +} +void BSONObj::jsonStringGenerator(ExtendedRelaxedV200Generator const& generator, + int pretty, + bool isArray, + fmt::memory_buffer& buffer) const { + _jsonStringGenerator(generator, pretty, isArray, buffer); +} +void BSONObj::jsonStringGenerator(LegacyStrictGenerator const& generator, + int pretty, + bool isArray, + fmt::memory_buffer& buffer) const { + _jsonStringGenerator(generator, pretty, isArray, buffer); +} + +std::string BSONObj::jsonString(JsonStringFormat format, int pretty, bool isArray) const { + fmt::memory_buffer buffer; + jsonStringBuffer(format, pretty, isArray, buffer); + return fmt::to_string(buffer); +} + +void BSONObj::jsonStringBuffer(JsonStringFormat format, + int pretty, + bool isArray, + fmt::memory_buffer& buffer) const { + auto withGenerator = [&](auto&& gen) { jsonStringGenerator(gen, pretty, isArray, buffer); }; + + if (format == ExtendedCanonicalV2_0_0) { + withGenerator(ExtendedCanonicalV200Generator()); + } else if (format == ExtendedRelaxedV2_0_0) { + withGenerator(ExtendedRelaxedV200Generator()); + } else if (format == LegacyStrict) { + withGenerator(LegacyStrictGenerator()); + } else { + MONGO_UNREACHABLE; + } } bool BSONObj::valid(BSONVersion version) const { |