|
|
◆ from_cbor()
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>
Deserializes a given byte vector v 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 |
| Nill | 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] | v | a byte vector in CBOR format |
| [in] | start_index | the index to start reading from v (0 by default) |
- Returns
- deserialized JSON value
- Exceptions
-
| parse_error.110 | if the given vector ends prematurely |
| parse_error.112 | if unsupported features from CBOR were used in the given vector 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 byte vector v.
- Example
- The example shows the deserialization of a byte vector in CBOR format to a JSON value.
9 std::vector<uint8_t> v = {0xa2, 0x67, 0x63, 0x6f, 0x6d, 0x70, 0x61, 0x63, 10 0x74, 0xf5, 0x66, 0x73, 0x63, 0x68, 0x65, 0x6d, 18 std::cout << std::setw(2) << j << std::endl; basic_json<> json default JSON class
static basic_json from_cbor(const std::vector< uint8_t > &v, const size_t start_index=0) create a JSON value from a byte vector in CBOR format
Output (play with this example online): {
"compact": true,
"schema": 0
}
The example code above can be translated withg++ -std=c++11 -Isrc doc/examples/from_cbor.cpp -o from_cbor
- See also
- http://cbor.io
-
to_cbor(const basic_json&) for the analogous serialization
-
from_msgpack(const std::vector<uint8_t>&, const size_t) for the related MessagePack format
- Since
- version 2.0.9, parameter start_index since 2.1.1
Definition at line 9361 of file json.hpp.
|