[CXX-1455] Add JSON-like syntax to construct BSON object Created: 24/Oct/17  Updated: 28/Oct/23  Resolved: 12/Nov/20

Status: Closed
Project: C++ Driver
Component/s: BSON
Affects Version/s: None
Fix Version/s: 3.7.0, 3.7.0-beta1

Type: Improvement Priority: Minor - P4
Reporter: Yoann Couillec Assignee: Clyde Bazile III (Inactive)
Resolution: Fixed Votes: 1
Labels: internal-woes
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Issue Links:
Depends
depends on CXX-2067 Expose bson_value::value type constru... Closed
Duplicate
is duplicated by CXX-1137 Alternative interface via nlohmann/json Closed
is duplicated by CXX-1294 Add new free functions to assist in e... Closed
Related
is related to CXX-1137 Alternative interface via nlohmann/json Closed
Epic Link: CXX-2082

 Description   

Today in mongo-cxx-driver, there are several ways of constructing a BSON object
http://mongodb.github.io/mongo-cxx-driver/mongocxx-v3/working-with-bson/

None of them provides a JSON-like way despite the fact it exists generally to construct JSON documents
https://github.com/nlohmann/json

I propose to use a similar construction to build BSON documents. Then, we will be able to write:

document friends {{"friend",{{"_id", json::id{}},
                                             {"name","jean"},
                                             {"dob",1970}}}};

instead of

bsoncxx::document::value = document{} << "friends" << open_document 
                                                                                    << "_id" << bsoncxx::oid() 
                                                                                    << "name" << "jean"
                                                                                    << " dob" << 1970
                                                                                    << close_document
                                                                                    << finalize;

I started the development of it



 Comments   
Comment by Githook User [ 12/Nov/20 ]

Author:

{'name': 'Clyde Bazile', 'email': '34226620+bazile-clyde@users.noreply.github.com', 'username': 'bazile-clyde'}

Message: CXX-1455 JSON-like BSON constructor (#739)
Branch: master
https://github.com/mongodb/mongo-cxx-driver/commit/8339c19b0a4020f592d6d210c94c399e9e9030d5

Comment by Clyde Bazile III (Inactive) [ 02/Nov/20 ]

https://github.com/mongodb/mongo-cxx-driver/pull/739

Comment by Mathias Stearn [ 08/Oct/19 ]

You can use macros to get close to that syntax. We should probably either include something like these in the driver, or modify the code to support it with non-macro syntax.

#define BSON_each(r, unused, elem) BSON_builder.append(bsoncxx::builder::basic::kvp elem);
#define BSON_ARRAY_each(r, unused, elem) BSON_builder.append(elem);
 
/**
 * Produces a bsoncxx::document::value out of a sequence of parenthesized key-value pairs.
 * Key-value pairs can be anything that can be passed to bsoncxx's append() inside a kvp().
 *
 * Example: BSON(("_id", 1), ("otherField", "value"))
 */
#define BSON(...)                                                                                                    \
    [&] {                                                                                                            \
        bsoncxx::builder::basic::document BSON_builder;                                                              \
        BOOST_PP_SEQ_FOR_EACH(BSON_each, unused, BOOST_PP_VARIADIC_TO_SEQ(__VA_ARGS__));                             \
        return BSON_builder.extract();                                                                               \
    }()
 
/**
 * A helper to go along with the BSON() macro for constructing nested subobjects.
 */
#define BSON_SUBDOC(...)                                                                                             \
    ([&](bsoncxx::builder::basic::sub_document BSON_builder) {                                                       \
        BOOST_PP_SEQ_FOR_EACH(BSON_each, unused, BOOST_PP_VARIADIC_TO_SEQ(__VA_ARGS__));                             \
    })
 
/**
 * A helper to go along with the BSON() macro for constructing nested subarrays.
 *
 * Example: BSON("array", BSON_ARRAY(1, "two", 3))
 */
#define BSON_ARRAY(...)                                                                                              \
    ([&](bsoncxx::builder::basic::sub_array BSON_builder) {                                                          \
        BOOST_PP_SEQ_FOR_EACH(BSON_ARRAY_each, unused, BOOST_PP_VARIADIC_TO_SEQ(__VA_ARGS__));                       \
    })

Comment by Andrew Morrow (Inactive) [ 09/Aug/18 ]

I believe this ticket is a dup of CXX-1137

Comment by Derick Rethans [ 26/Oct/17 ]

Hi,

We are interested in having something like this in the bsoncxx part of the driver. Right now, we are working on MongoDB 3.6 support, and can't really dedicate much time on the implementation of this ourselves. We are however, more then happy to have a look at the syntax proposals, and, if you want, have an initial look at your implementation as well. I do however need to mention, that we don't expect to make it into our next 3.3 release due to time pressure.

cheers,
Derick

Generated at Wed Feb 07 22:02:42 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.