JSON for Modern C++  2.1.1

◆ 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>
static basic_json nlohmann::basic_json::from_cbor ( const std::vector< uint8_t > &  v,
const size_t  start_index = 0 
)
inlinestatic

Deserializes a given byte vector v to a JSON value using the CBOR (Concise Binary Object Representation) serialization format.

Parameters
[in]va byte vector in CBOR format
[in]start_indexthe index to start reading from v (0 by default)
Returns
deserialized JSON value
Exceptions
std::invalid_argumentif unsupported features from CBOR were used in the given vector v or if the input is not valid MessagePack
std::out_of_rangeif the given vector ends prematurely
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.
1 #include <json.hpp>
2 
3 using json = nlohmann::json;
4 
5 int main()
6 {
7  // create byte vector
8  std::vector<uint8_t> v = {0xa2, 0x67, 0x63, 0x6f, 0x6d, 0x70, 0x61, 0x63,
9  0x74, 0xf5, 0x66, 0x73, 0x63, 0x68, 0x65, 0x6d,
10  0x61, 0x00
11  };
12 
13  // deserialize it with CBOR
14  json j = json::from_cbor(v);
15 
16  // print the deserialized JSON value
17  std::cout << std::setw(2) << j << std::endl;
18 }
basic_json<> json
default JSON class
Definition: json.hpp:12369
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
Definition: json.hpp:8037
Output (play with this example online):
{
  "compact": true,
  "schema": 0
}
The example code above can be translated with
g++ -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 8037 of file json.hpp.