JSON for Modern C++  3.7.2

◆ from_bson() [1/2]

template<template< typename U, typename V, typename... Args > class ObjectType = std::map, template< typename U, typename... Args > class ArrayType = std::vector, class StringType = std::string, class BooleanType = bool, class NumberIntegerType = std::int64_t, class NumberUnsignedType = std::uint64_t, class NumberFloatType = double, template< typename U > class AllocatorType = std::allocator, template< typename T, typename SFINAE=void > class JSONSerializer = adl_serializer>
template<typename A1 , typename A2 , detail::enable_if_t< std::is_constructible< detail::input_adapter, A1, A2 >::value, int > = 0>
static basic_json nlohmann::basic_json::from_bson ( A1 &&  a1,
A2 &&  a2,
const bool  strict = true,
const bool  allow_exceptions = true 

Deserializes a given input i to a JSON value using the BSON (Binary JSON) serialization format.

The library maps BSON record types to JSON value types as follows:

BSON type BSON marker byte JSON value type
double 0x01 number_float
string 0x02 string
document 0x03 object
array 0x04 array
binary 0x05 still unsupported
undefined 0x06 still unsupported
ObjectId 0x07 still unsupported
boolean 0x08 boolean
UTC Date-Time 0x09 still unsupported
null 0x0A null
Regular Expr. 0x0B still unsupported
DB Pointer 0x0C still unsupported
JavaScript Code 0x0D still unsupported
Symbol 0x0E still unsupported
JavaScript Code 0x0F still unsupported
int32 0x10 number_integer
Timestamp 0x11 still unsupported
128-bit decimal float 0x13 still unsupported
Max Key 0x7F still unsupported
Min Key 0xFF still unsupported
The mapping is incomplete. The unsupported mappings are indicated in the table above.
[in]ian input in BSON format convertible to an input adapter
[in]strictwhether to expect the input to be consumed until EOF (true by default)
[in]allow_exceptionswhether to throw exceptions in case of a parse error (optional, true by default)
deserialized JSON value; in case of a parse error and allow_exceptions set to false, the return value will be value_t::discarded.
parse_error.114if an unsupported BSON record type is encountered
Linear in the size of the input i.
The example shows the deserialization of a byte vector in BSON format to a JSON value.
1 #include <iostream>
2 #include <iomanip>
3 #include <nlohmann/json.hpp>
5 using json = nlohmann::json;
7 int main()
8 {
9  // create byte vector
10  std::vector<uint8_t> v = {0x1b, 0x00, 0x00, 0x00, 0x08, 0x63, 0x6f, 0x6d,
11  0x70, 0x61, 0x63, 0x74, 0x00, 0x01, 0x10, 0x73,
12  0x63, 0x68, 0x65, 0x6d, 0x61, 0x00, 0x00, 0x00,
13  0x00, 0x00, 0x00
14  };
16  // deserialize it with BSON
17  json j = json::from_bson(v);
19  // print the deserialized JSON value
20  std::cout << std::setw(2) << j << std::endl;
21 }

Output (play with this example online):
  "compact": true,
  "schema": 0
The example code above can be translated with
g++ -std=c++11 -Isingle_include doc/examples/from_bson.cpp -o from_bson 
See also
to_bson(const basic_json&) for the analogous serialization
from_cbor(detail::input_adapter&&, const bool, const bool) for the related CBOR format
from_msgpack(detail::input_adapter&&, const bool, const bool) for the related MessagePack format
from_ubjson(detail::input_adapter&&, const bool, const bool) for the related UBJSON format

Definition at line 21829 of file json.hpp.

static basic_json from_bson(detail::input_adapter &&i, const bool strict=true, const bool allow_exceptions=true)
Create a JSON value from an input in BSON format.
Definition: json.hpp:21813
basic_json<> json
default JSON class
Definition: json.hpp:2445