JSON for Modern C++ 3.8.0

accept
accept
accept
allocator_type
array
array_t
at
at
at
at
at
at
back
back
basic_json
basic_json
basic_json
basic_json
basic_json
basic_json
basic_json
basic_json
basic_json
basic_json
begin
begin
binary
binary
binary
binary
binary_t
boolean_t
cbegin
cend
clear
const_iterator
const_pointer
const_reference
const_reverse_iterator
contains
contains
count
crbegin
crend
detail::external_constructor
diff
difference_type
dump
emplace
emplace_back
empty
end
end
erase
erase
erase
erase
error_handler_t
exception
find
find
flatten
from_bson
from_bson
from_bson
from_bson
from_cbor
from_cbor
from_cbor
from_cbor
from_msgpack
from_msgpack
from_msgpack
from_msgpack
from_ubjson
from_ubjson
from_ubjson
from_ubjson
front
front
get
get
get
get
get
get
get_allocator
get_binary
get_binary
get_ptr
get_ptr
get_ref
get_ref
get_to
get_to
initializer_list_t
input_format_t
insert
insert
insert
insert
insert
insert
insert_iterator
invalid_iterator
is_array
is_binary
is_boolean
is_discarded
is_null
is_number
is_number_float
is_number_integer
is_number_unsigned
is_object
is_primitive
is_string
is_structured
items
items
iterator
iterator_wrapper
iterator_wrapper
json_pointer
json_sax_t
json_serializer
max_size
merge_patch
meta
number_float_t
number_integer_t
number_unsigned_t
object
object_comparator_t
object_t
operator value_t
operator ValueType
operator!=
operator!=
operator!=
operator+=
operator+=
operator+=
operator+=
operator<
operator<
operator<
operator<<
operator<<
operator<=
operator<=
operator<=
operator=
operator==
operator==
operator==
operator>
operator>
operator>
operator>=
operator>=
operator>=
operator>>
operator>>
operator[]
operator[]
operator[]
operator[]
operator[]
operator[]
operator[]
operator[]
other_error
out_of_range
parse
parse
parse
parse_error
parse_event_t
parser_callback_t
patch
pointer
push_back
push_back
push_back
push_back
rbegin
rbegin
reference
rend
rend
reverse_iterator
sax_parse
sax_parse
sax_parse
size
size_type
string_t
swap
swap
swap
swap
swap
swap
to_bson
to_bson
to_bson
to_cbor
to_cbor
to_cbor
to_msgpack
to_msgpack
to_msgpack
to_ubjson
to_ubjson
to_ubjson
type
type_error
type_name
unflatten
update
update
value
value
value
value
value_t
value_type
~basic_json
| ## ◆ to_bson() [1/3]
static std::vector<uint8_t> nlohmann::basic_json::to_bson | ( | const basic_json & | j | ) |
| inline static
BSON (Binary JSON) is a binary format in which zero or more ordered key/value pairs are stored as a single entity (a so-called document). The library uses the following mapping from JSON values types to BSON types:
JSON value type | value/range | BSON type | marker
--- | --- | --- | ---
null | `null` | null | 0x0A
boolean | `true`, `false` | boolean | 0x08
number_integer | -9223372036854775808..-2147483649 | int64 | 0x12
number_integer | -2147483648..2147483647 | int32 | 0x10
number_integer | 2147483648..9223372036854775807 | int64 | 0x12
number_unsigned | 0..2147483647 | int32 | 0x10
number_unsigned | 2147483648..9223372036854775807 | int64 | 0x12
number_unsigned | 9223372036854775808..18446744073709551615 | – | –
number_float | any value | double | 0x01
string | any value | string | 0x02
array | any value | document | 0x04
object | any value | document | 0x03
binary | any value | binary | 0x05
  Warning The mapping is incomplete, since only JSON-objects (and things contained therein) can be serialized to BSON. Also, integers larger than 9223372036854775807 cannot be serialized to BSON, and the keys may not contain U+0000, since they are serialized a zero-terminated c-strings. Exceptions
  out_of_range.407 | if `j.is_number_unsigned() && j.get<std::uint64_t>() > 9223372036854775807`
  out_of_range.409 | if a key in `j` contains a NULL (U+0000)
  type_error.317 | if `!j.is_object()`
  Precondition The input `j` is required to be an object: `j.is_object() == true`. Note Any BSON output created via to_bson can be successfully parsed by from_bson. Parameters
  [in] | j | JSON value to serialize
  Returns BSON serialization as byte vector Complexity Linear in the size of the JSON value j. Example The example shows the serialization of a JSON value to a byte vector in BSON format. 1 #include <iostream> 2 #include <iomanip> 3 #include <nlohmann/json.hpp> 4 5 using json = nlohmann::json; 6 7 int main() 8 { 9 // create a JSON value 10 json j = R"({"compact": true, "schema": 0})"_json; 11 12 // serialize it to BSON 13 std::vector<uint8_t> v = json::to_bson(j); 14 15 // print the vector content 16 for (auto& byte : v) 17 { 18 std::cout << "0x" << std::hex << std::setw(2) << std::setfill('0') << (int)byte << " "; 19 } 20 std::cout << std::endl; 21 } Output (play with this example online):
      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
  The example code above can be translated with
      g++ -std=c++11 -Isingle_include doc/examples/to_bson.cpp -o to_bson
See also http://bsonspec.org/spec.html from_bson(detail::input_adapter&&, const bool strict) for the analogous deserialization to_ubjson(const basic_json&, const bool, const bool) for the related UBJSON format to_cbor(const basic_json&) for the related CBOR format to_msgpack(const basic_json&) for the related MessagePack format Definition at line 23065 of file json.hpp.

nlohmann::basic_json::to_bson
static std::vector< uint8_t > to_bson(const basic_json &j)
Serializes the given JSON object j to BSON and returns a vector containing the corresponding BSON-rep...
Definition: json.hpp:23065
nlohmann::json
basic_json<> json
default JSON class
Definition: json.hpp:2774