|
◆ from_cbor() [2/2]
static basic_json nlohmann::basic_json::from_cbor |
( |
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 CBOR (Concise Binary Object Representation) serialization format.
The library maps CBOR types to JSON value types as follows:
CBOR type | JSON value type | first byte |
Integer | number_unsigned | 0x00..0x17 |
Unsigned integer | number_unsigned | 0x18 |
Unsigned integer | number_unsigned | 0x19 |
Unsigned integer | number_unsigned | 0x1A |
Unsigned integer | number_unsigned | 0x1B |
Negative integer | number_integer | 0x20..0x37 |
Negative integer | number_integer | 0x38 |
Negative integer | number_integer | 0x39 |
Negative integer | number_integer | 0x3A |
Negative integer | number_integer | 0x3B |
Negative integer | number_integer | 0x40..0x57 |
UTF-8 string | string | 0x60..0x77 |
UTF-8 string | string | 0x78 |
UTF-8 string | string | 0x79 |
UTF-8 string | string | 0x7A |
UTF-8 string | string | 0x7B |
UTF-8 string | string | 0x7F |
array | array | 0x80..0x97 |
array | array | 0x98 |
array | array | 0x99 |
array | array | 0x9A |
array | array | 0x9B |
array | array | 0x9F |
map | object | 0xA0..0xB7 |
map | object | 0xB8 |
map | object | 0xB9 |
map | object | 0xBA |
map | object | 0xBB |
map | object | 0xBF |
False | false | 0xF4 |
True | true | 0xF5 |
Null | null | 0xF6 |
Half-Precision Float | number_float | 0xF9 |
Single-Precision Float | number_float | 0xFA |
Double-Precision Float | number_float | 0xFB |
- Warning
- The mapping is incomplete in the sense that not all CBOR types can be converted to a JSON value. The following CBOR types are not supported and will yield parse errors (parse_error.112):
- byte strings (0x40..0x5F)
- date/time (0xC0..0xC1)
- bignum (0xC2..0xC3)
- decimal fraction (0xC4)
- bigfloat (0xC5)
- tagged items (0xC6..0xD4, 0xD8..0xDB)
- expected conversions (0xD5..0xD7)
- simple values (0xE0..0xF3, 0xF8)
- undefined (0xF7)
-
CBOR allows map keys of any type, whereas JSON only allows strings as keys in object values. Therefore, CBOR maps with keys other than UTF-8 strings are rejected (parse_error.113).
- Note
- Any CBOR output created to_cbor can be successfully parsed by from_cbor.
- Parameters
-
[in] | i | an input in CBOR 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.110 | if the given input ends prematurely or the end of file was not reached when strict was set to true |
parse_error.112 | if unsupported features from CBOR were used in the given input v or if the input is not valid CBOR |
parse_error.113 | if a string was expected as map key, but not found |
- Complexity
- Linear in the size of the input i.
- Example
- The example shows the deserialization of a byte vector in CBOR format to a JSON value.
3 #include <nlohmann/json.hpp>
10 std::vector<uint8_t> v = {0xa2, 0x67, 0x63, 0x6f, 0x6d, 0x70, 0x61, 0x63,
11 0x74, 0xf5, 0x66, 0x73, 0x63, 0x68, 0x65, 0x6d,
19 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_cbor.cpp -o from_cbor
- See also
- http://cbor.io
-
to_cbor(const basic_json&) for the analogous serialization
-
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
- Since
- version 2.0.9; parameter start_index since 2.1.1; changed to consume input adapters, removed start_index parameter, and added strict parameter since 3.0.0; added allow_exceptions parameter since 3.2.0
Definition at line 21527 of file json.hpp.
|