JSON for Modern C++  3.7.2

◆ from_cbor() [2/2]

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>
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]ian input in CBOR 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)
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.110if the given input ends prematurely or the end of file was not reached when strict was set to true
parse_error.112if unsupported features from CBOR were used in the given input v or if the input is not valid CBOR
parse_error.113if 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.
1 #include <iostream>
2 #include <iomanip>
3 #include <nlohmann/json.hpp>
4 
5 using json = nlohmann::json;
6 
7 int main()
8 {
9  // create byte vector
10  std::vector<uint8_t> v = {0xa2, 0x67, 0x63, 0x6f, 0x6d, 0x70, 0x61, 0x63,
11  0x74, 0xf5, 0x66, 0x73, 0x63, 0x68, 0x65, 0x6d,
12  0x61, 0x00
13  };
14 
15  // deserialize it with CBOR
16  json j = json::from_cbor(v);
17 
18  // print the deserialized JSON value
19  std::cout << std::setw(2) << j << std::endl;
20 }

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
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 21529 of file json.hpp.

nlohmann::basic_json::from_cbor
static basic_json from_cbor(detail::input_adapter &&i, const bool strict=true, const bool allow_exceptions=true)
create a JSON value from an input in CBOR format
Definition: json.hpp:21529
nlohmann::json
basic_json<> json
default JSON class
Definition: json.hpp:2445