📝 added examples for BSON functions

This commit is contained in:
Niels Lohmann 2018-10-28 15:09:26 +01:00
parent d2e4f0b0d9
commit 86b5ce953a
No known key found for this signature in database
GPG key ID: 7F3CEA63AE251B69
7 changed files with 57 additions and 4 deletions

View file

@ -0,0 +1,21 @@
#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;
}

View file

@ -0,0 +1 @@
<a target="_blank" href="https://wandbox.org/permlink/qtfoxfHO7u4eKMcO"><b>online</b></a>

View file

@ -0,0 +1,4 @@
{
"compact": true,
"schema": 0
}

21
doc/examples/to_bson.cpp Normal file
View file

@ -0,0 +1,21 @@
#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;
}

View file

@ -0,0 +1 @@
<a target="_blank" href="https://wandbox.org/permlink/2roucrct3qDL6LCk"><b>online</b></a>

View file

@ -0,0 +1 @@
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

View file

@ -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
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
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
@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