|
◆ from_cbor() [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_cbor |
( |
InputType && |
i, |
|
|
const bool |
strict = true , |
|
|
const bool |
allow_exceptions = true , |
|
|
const cbor_tag_handler_t |
tag_handler = cbor_tag_handler_t::error |
|
) |
| |
|
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 |
Byte string | binary | 0x40..0x57 |
Byte string | binary | 0x58 |
Byte string | binary | 0x59 |
Byte string | binary | 0x5A |
Byte string | binary | 0x5B |
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):
- date/time (0xC0..0xC1)
- bignum (0xC2..0xC3)
- decimal fraction (0xC4)
- bigfloat (0xC5)
- 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) |
[in] | tag_handler | how to treat CBOR tags (optional, error 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;
static basic_json from_cbor(InputType &&i, const bool strict=true, const bool allow_exceptions=true, const cbor_tag_handler_t tag_handler=cbor_tag_handler_t::error) create a JSON value from an input in CBOR format
basic_json<> json default JSON class
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_cbor.cpp -o from_cbor
- See also
- http://cbor.io
-
see to_cbor(const basic_json&) for the analogous serialization
-
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
- 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; added tag_handler parameter since 3.9.0.
Definition at line 25246 of file json.hpp.
|