JSON for Modern C++  2.1.1
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 std::vector<uint8_t> nlohmann::basic_json::to_cbor ( const basic_json j)
inlinestatic

Serializes a given JSON value j to a byte vector using the CBOR (Concise Binary Object Representation) serialization format. CBOR is a binary serialization format which aims to be more compact than JSON itself, yet more efficient to parse.

Parameters
[in]jJSON value to serialize
Returns
MessagePack serialization as byte vector
Complexity
Linear in the size of the JSON value j.
Example
The example shows the serialization of a JSON value to a byte vector in CBOR format.
1 #include <json.hpp>
2 
3 using json = nlohmann::json;
4 
5 int main()
6 {
7  // create a JSON value
8  json j = R"({"compact": true, "schema": 0})"_json;
9 
10  // serialize it to CBOR
11  std::vector<uint8_t> v = json::to_cbor(j);
12 
13  // print the vector content
14  for (auto& byte : v)
15  {
16  std::cout << "0x" << std::hex << std::setw(2) << std::setfill('0') << (int)byte << " ";
17  }
18  std::cout << std::endl;
19 }
basic_json<> json
default JSON class
Definition: json.hpp:12369
static std::vector< uint8_t > to_cbor(const basic_json &j)
create a MessagePack serialization of a given JSON value
Definition: json.hpp:8004
Output (play with this example online):
0xa2 0x67 0x63 0x6f 0x6d 0x70 0x61 0x63 0x74 0xf5 0x66 0x73 0x63 0x68 0x65 0x6d 0x61 0x00 
The example code above can be translated with
g++ -std=c++11 -Isrc doc/examples/to_cbor.cpp -o to_cbor 
See also
http://cbor.io
from_cbor(const std::vector<uint8_t>&, const size_t) for the analogous deserialization
to_msgpack(const basic_json& for the related MessagePack format
Since
version 2.0.9

Definition at line 8004 of file json.hpp.