Skip to content

Commit 94fa3d2

Browse files
authored
CXX-1712 overload to_json to handle bson arrays (#976)
1 parent 418e219 commit 94fa3d2

File tree

5 files changed

+78
-3
lines changed

5 files changed

+78
-3
lines changed

.mci.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,12 @@
77
#######################################
88
variables:
99

10-
mongoc_version_default: &mongoc_version_default "98995b5d9e31768ae02be265e3debe43ec35cff8" # TODO: update to 1.24.0 once released.
10+
mongoc_version_default: &mongoc_version_default "a41683b76fce42f841318d5bb8d926154f9438c8" # TODO: update to 1.24.0 once released.
1111

1212
# If updating mongoc_version_minimum, also update:
1313
# - the default value of --c-driver-build-ref in etc/make_release.py
1414
# - LIBMONGOC_REQUIRED_VERSION in src/mongocxx/CMakeLists.txt
15-
mongoc_version_minimum: &mongoc_version_minimum "98995b5d9e31768ae02be265e3debe43ec35cff8" # TODO: update to 1.24.0 once released.
15+
mongoc_version_minimum: &mongoc_version_minimum "a41683b76fce42f841318d5bb8d926154f9438c8" # TODO: update to 1.24.0 once released.
1616

1717
mongodb_version:
1818
version_latest: &version_latest "latest"

etc/make_release.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@
8484
show_default=True,
8585
help='The remote reference which points to the mongodb/mongo-cxx-driver repo')
8686
@click.option('--c-driver-build-ref',
87-
default='98995b5d9e31768ae02be265e3debe43ec35cff8',
87+
default='a41683b76fce42f841318d5bb8d926154f9438c8',
8888
show_default=True,
8989
help='When building the C driver, build at this Git reference')
9090
@click.option('--with-c-driver',

src/bsoncxx/json.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,21 @@ std::string BSONCXX_CALL to_json(document::view view, ExtendedJsonMode mode) {
6969
BSONCXX_UNREACHABLE;
7070
}
7171

72+
std::string BSONCXX_CALL to_json(array::view view, ExtendedJsonMode mode) {
73+
switch (mode) {
74+
case ExtendedJsonMode::k_legacy:
75+
return to_json_helper(view, bson_array_as_json);
76+
77+
case ExtendedJsonMode::k_relaxed:
78+
return to_json_helper(view, bson_array_as_relaxed_extended_json);
79+
80+
case ExtendedJsonMode::k_canonical:
81+
return to_json_helper(view, bson_array_as_canonical_extended_json);
82+
}
83+
84+
BSONCXX_UNREACHABLE;
85+
}
86+
7287
document::value BSONCXX_CALL from_json(stdx::string_view json) {
7388
bson_error_t error;
7489
bson_t* result = bson_new_from_json(reinterpret_cast<const uint8_t*>(json.data()),

src/bsoncxx/json.hpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
#include <string>
1818

19+
#include <bsoncxx/array/view.hpp>
1920
#include <bsoncxx/document/value.hpp>
2021
#include <bsoncxx/document/view.hpp>
2122
#include <bsoncxx/stdx/optional.hpp>
@@ -56,6 +57,9 @@ enum class ExtendedJsonMode : std::uint8_t {
5657
BSONCXX_API std::string BSONCXX_CALL to_json(document::view view,
5758
ExtendedJsonMode mode = ExtendedJsonMode::k_legacy);
5859

60+
BSONCXX_API std::string BSONCXX_CALL to_json(array::view view,
61+
ExtendedJsonMode mode = ExtendedJsonMode::k_legacy);
62+
5963
///
6064
/// Constructs a new document::value from the provided JSON text.
6165
///

src/bsoncxx/test/json.cpp

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,62 @@ TEST_CASE("CXX-1246: Canonical Extended JSON") {
116116
R"({ "number" : { "$numberInt" : "42" }, "bin" : { "$binary" : { "base64" : "ZGVhZGJlZWY=", "subType" : "04" } } })");
117117
}
118118

119+
TEST_CASE("CXX-1712: Overloaded to_json Legacy (Implicit)") {
120+
using namespace bsoncxx;
121+
using namespace builder::basic;
122+
123+
types::b_binary bin_val{
124+
binary_sub_type::k_uuid, 8, reinterpret_cast<const uint8_t*>("deadbeef")};
125+
auto arr = make_array(make_document(kvp("foo", 42), kvp("bar", "A"), kvp("baz", bin_val)));
126+
auto output = bsoncxx::to_json(arr.view());
127+
128+
REQUIRE(
129+
output ==
130+
R"([ { "foo" : 42, "bar" : "A", "baz" : { "$binary" : "ZGVhZGJlZWY=", "$type" : "04" } } ])");
131+
}
132+
133+
TEST_CASE("CXX-1712: Overloaded to_json Legacy (Explicit)") {
134+
using namespace bsoncxx;
135+
using namespace builder::basic;
136+
137+
types::b_binary bin_val{
138+
binary_sub_type::k_uuid, 8, reinterpret_cast<const uint8_t*>("deadbeef")};
139+
auto arr = make_array(make_document(kvp("foo", 42), kvp("bar", "A"), kvp("baz", bin_val)));
140+
auto output = to_json(arr.view(), ExtendedJsonMode::k_legacy);
141+
142+
REQUIRE(
143+
output ==
144+
R"([ { "foo" : 42, "bar" : "A", "baz" : { "$binary" : "ZGVhZGJlZWY=", "$type" : "04" } } ])");
145+
}
146+
147+
TEST_CASE("CXX-1712: Overloaded to_json Relaxed") {
148+
using namespace bsoncxx;
149+
using namespace builder::basic;
150+
151+
types::b_binary bin_val{
152+
binary_sub_type::k_uuid, 8, reinterpret_cast<const uint8_t*>("deadbeef")};
153+
auto arr = make_array(make_document(kvp("foo", 42), kvp("bar", "A"), kvp("baz", bin_val)));
154+
auto output = to_json(arr.view(), ExtendedJsonMode::k_relaxed);
155+
156+
REQUIRE(
157+
output ==
158+
R"([ { "foo" : 42, "bar" : "A", "baz" : { "$binary" : { "base64" : "ZGVhZGJlZWY=", "subType" : "04" } } } ])");
159+
}
160+
161+
TEST_CASE("CXX-1712: Overloaded to_json Canonical") {
162+
using namespace bsoncxx;
163+
using namespace builder::basic;
164+
165+
types::b_binary bin_val{
166+
binary_sub_type::k_uuid, 8, reinterpret_cast<const uint8_t*>("deadbeef")};
167+
auto arr = make_array(make_document(kvp("foo", 42), kvp("bar", "A"), kvp("baz", bin_val)));
168+
auto output = to_json(arr.view(), ExtendedJsonMode::k_canonical);
169+
170+
REQUIRE(
171+
output ==
172+
R"([ { "foo" : { "$numberInt" : "42" }, "bar" : "A", "baz" : { "$binary" : { "base64" : "ZGVhZGJlZWY=", "subType" : "04" } } } ])");
173+
}
174+
119175
TEST_CASE("UDL _bson works like from_json()") {
120176
using namespace bsoncxx;
121177

0 commit comments

Comments
 (0)