📝 added examples for BSON functions

parent d2e4f0b0
#include <iostream>
#include <iomanip>
#include <nlohmann/json.hpp>
using json = nlohmann::json;
int main()
{
// create byte vector
std::vector<uint8_t> v = {0x1b, 0x00, 0x00, 0x00, 0x08, 0x63, 0x6f, 0x6d,
0x70, 0x61, 0x63, 0x74, 0x00, 0x01, 0x10, 0x73,
0x63, 0x68, 0x65, 0x6d, 0x61, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00
};
// deserialize it with BSON
json j = json::from_bson(v);
// print the deserialized JSON value
std::cout << std::setw(2) << j << std::endl;
}
<a target="_blank" href="https://wandbox.org/permlink/qtfoxfHO7u4eKMcO"><b>online</b></a>
\ No newline at end of file
{
"compact": true,
"schema": 0
}
#include <iostream>
#include <iomanip>
#include <nlohmann/json.hpp>
using json = nlohmann::json;
int main()
{
// create a JSON value
json j = R"({"compact": true, "schema": 0})"_json;
// serialize it to BSON
std::vector<uint8_t> v = json::to_bson(j);
// print the vector content
for (auto& byte : v)
{
std::cout << "0x" << std::hex << std::setw(2) << std::setfill('0') << (int)byte << " ";
}
std::cout << std::endl;
}
<a target="_blank" href="https://wandbox.org/permlink/2roucrct3qDL6LCk"><b>online</b></a>
\ No newline at end of file
0x1b 0x00 0x00 0x00 0x08 0x63 0x6f 0x6d 0x70 0x61 0x63 0x74 0x00 0x01 0x10 0x73 0x63 0x68 0x65 0x6d 0x61 0x00 0x00 0x00 0x00 0x00 0x00
...@@ -6672,6 +6672,9 @@ class basic_json ...@@ -6672,6 +6672,9 @@ class basic_json
@complexity Linear in the size of the JSON value @a j. @complexity Linear in the size of the JSON value @a j.
@liveexample{The example shows the serialization of a JSON value to a byte
vector in BSON format.,to_bson}
@sa http://bsonspec.org/spec.html @sa http://bsonspec.org/spec.html
@sa @ref from_bson(detail::input_adapter&&, const bool strict) for the @sa @ref from_bson(detail::input_adapter&&, const bool strict) for the
analogous deserialization analogous deserialization
...@@ -7020,9 +7023,6 @@ class basic_json ...@@ -7020,9 +7023,6 @@ class basic_json
return res ? result : basic_json(value_t::discarded); return res ? result : basic_json(value_t::discarded);
} }
/*! /*!
@brief Create a JSON value from an input in BSON format @brief Create a JSON value from an input in BSON format
...@@ -7054,7 +7054,6 @@ class basic_json ...@@ -7054,7 +7054,6 @@ class basic_json
Max Key | 0x7F | still unsupported Max Key | 0x7F | still unsupported
Min Key | 0xFF | still unsupported Min Key | 0xFF | still unsupported
@warning The mapping is **incomplete**. The unsupported mappings @warning The mapping is **incomplete**. The unsupported mappings
are indicated in the table above. are indicated in the table above.
...@@ -7068,6 +7067,11 @@ class basic_json ...@@ -7068,6 +7067,11 @@ class basic_json
@throw parse_error.114 if an unsupported BSON record type is encountered @throw parse_error.114 if an unsupported BSON record type is encountered
@complexity Linear in the size of the input @a i.
@liveexample{The example shows the deserialization of a byte vector in
BSON format to a JSON value.,from_bson}
@sa http://bsonspec.org/spec.html @sa http://bsonspec.org/spec.html
@sa @ref to_bson(const basic_json&) for the analogous serialization @sa @ref to_bson(const basic_json&) for the analogous serialization
@sa @ref from_cbor(detail::input_adapter&&, const bool, const bool) for the @sa @ref from_cbor(detail::input_adapter&&, const bool, const bool) for the
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment