JSON for Modern C++ 3.10.4

◆ from_bson() [3/4]

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, class BinaryType = std::vector<std::uint8_t>>
template<typename InputType >
static basic_json nlohmann::basic_json< ObjectType, ArrayType, StringType, BooleanType, NumberIntegerType, NumberUnsignedType, NumberFloatType, AllocatorType, JSONSerializer, BinaryType >::from_bson ( InputType &&  i,
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 binary
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>
5using json = nlohmann::json;
7int main()
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;
static basic_json from_bson(InputType &&i, const bool strict=true, const bool allow_exceptions=true)
Create a JSON value from an input in BSON format.
Definition: json.hpp:25621
basic_json<> json
default JSON class
Definition: json.hpp:3472

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
see to_bson(const basic_json&) for the analogous serialization
see from_cbor(InputType&&, const bool, const bool, const cbor_tag_handler_t) for the related CBOR format
see from_msgpack(InputType&&, const bool, const bool) for the related MessagePack format
see from_ubjson(InputType&&, const bool, const bool) for the related UBJSON format

Definition at line 25621 of file json.hpp.