|
◆ from_bson() [2/2]
static basic_json nlohmann::basic_json::from_bson |
( |
detail::input_adapter && |
i, |
|
|
const bool |
strict = true , |
|
|
const bool |
allow_exceptions = true |
|
) |
| |
|
inlinestatic |
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 |
- Warning
- The mapping is incomplete. The unsupported mappings are indicated in the table above.
- Parameters
-
[in] | i | an input in BSON format convertible to an input adapter |
[in] | strict | whether to expect the input to be consumed until EOF (true by default) |
[in] | allow_exceptions | whether to throw exceptions in case of a parse error (optional, true by default) |
- Returns
- deserialized JSON value; in case of a parse error and allow_exceptions set to
false , the return value will be value_t::discarded.
- Exceptions
-
parse_error.114 | if an unsupported BSON record type is encountered |
- Complexity
- Linear in the size of the input i.
- Example
- The example shows the deserialization of a byte vector in BSON format to a JSON value.
3 #include <nlohmann/json.hpp>
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,
20 std::cout << std::setw(2) << j << std::endl;
Output (play with this example online): {
"compact": true,
"schema": 0
}
The example code above can be translated withg++ -std=c++11 -Isingle_include doc/examples/from_bson.cpp -o from_bson
- See also
- http://bsonspec.org/spec.html
-
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 21811 of file json.hpp.
|