JSON for Modern C++ 2.1.1

allocator_type
array
array_t
at
at
at
at
at
at
back
back
basic_json
basic_json
basic_json
basic_json
basic_json
basic_json
basic_json
basic_json
begin
begin
boolean_t
cbegin
cend
clear
const_iterator
const_pointer
const_reference
const_reverse_iterator
count
crbegin
crend
detail::external_constructor
diff
difference_type
dump
emplace
emplace_back
empty
end
end
erase
erase
erase
erase
exception
find
find
flatten
from_cbor
from_msgpack
front
front
get
get
get
get
get
get_allocator
get_ptr
get_ptr
get_ref
get_ref
insert
insert
insert
insert
insert
insert
invalid_iterator
is_array
is_boolean
is_discarded
is_null
is_number
is_number_float
is_number_integer
is_number_unsigned
is_object
is_primitive
is_string
is_structured
iterator
iterator_wrapper
iterator_wrapper
json_serializer
max_size
meta
number_float_t
number_integer_t
number_unsigned_t
object
object_t
operator value_t
operator ValueType
operator!=
operator!=
operator!=
operator+=
operator+=
operator+=
operator+=
operator<
operator<
operator<
operator<<
operator<<
operator<=
operator<=
operator<=
operator=
operator==
operator==
operator==
operator>
operator>
operator>
operator>=
operator>=
operator>=
operator>>
operator>>
operator[]
operator[]
operator[]
operator[]
operator[]
operator[]
operator[]
operator[]
operator[]
operator[]
other_error
out_of_range
parse
parse
parse
parse
parse
parse
parse_error
parse_event_t
parser_callback_t
patch
pointer
push_back
push_back
push_back
push_back
rbegin
rbegin
reference
rend
rend
reverse_iterator
size
size_type
string_t
swap
swap
swap
swap
to_cbor
to_msgpack
type
type_error
type_name
unflatten
value
value
value
value
value_t
value_type
~basic_json
| ## ◆ to_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 std::vector<uint8_t> nlohmann::basic_json::to_cbor | ( | const basic_json & | j | ) |
| inline static
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. The library uses the following mapping from JSON values types to CBOR types according to the CBOR specification (RFC 7049):
JSON value type | value/range | CBOR type | first byte
--- | --- | --- | ---
null | `null` | Null | 0xf6
boolean | `true` | True | 0xf5
boolean | `false` | False | 0xf4
number_integer | -9223372036854775808..-2147483649 | Negative integer (8 bytes follow) | 0x3b
number_integer | -2147483648..-32769 | Negative integer (4 bytes follow) | 0x3a
number_integer | -32768..-129 | Negative integer (2 bytes follow) | 0x39
number_integer | -128..-25 | Negative integer (1 byte follow) | 0x38
number_integer | -24..-1 | Negative integer | 0x20..0x37
number_integer | 0..23 | Integer | 0x00..0x17
number_integer | 24..255 | Unsigned integer (1 byte follow) | 0x18
number_integer | 256..65535 | Unsigned integer (2 bytes follow) | 0x19
number_integer | 65536..4294967295 | Unsigned integer (4 bytes follow) | 0x1a
number_integer | 4294967296..18446744073709551615 | Unsigned integer (8 bytes follow) | 0x1b
number_unsigned | 0..23 | Integer | 0x00..0x17
number_unsigned | 24..255 | Unsigned integer (1 byte follow) | 0x18
number_unsigned | 256..65535 | Unsigned integer (2 bytes follow) | 0x19
number_unsigned | 65536..4294967295 | Unsigned integer (4 bytes follow) | 0x1a
number_unsigned | 4294967296..18446744073709551615 | Unsigned integer (8 bytes follow) | 0x1b
number_float | any value | Double-Precision Float | 0xfb
string | length: 0..23 | UTF-8 string | 0x60..0x77
string | length: 23..255 | UTF-8 string (1 byte follow) | 0x78
string | length: 256..65535 | UTF-8 string (2 bytes follow) | 0x79
string | length: 65536..4294967295 | UTF-8 string (4 bytes follow) | 0x7a
string | length: 4294967296..18446744073709551615 | UTF-8 string (8 bytes follow) | 0x7b
array | size: 0..23 | array | 0x80..0x97
array | size: 23..255 | array (1 byte follow) | 0x98
array | size: 256..65535 | array (2 bytes follow) | 0x99
array | size: 65536..4294967295 | array (4 bytes follow) | 0x9a
array | size: 4294967296..18446744073709551615 | array (8 bytes follow) | 0x9b
object | size: 0..23 | map | 0xa0..0xb7
object | size: 23..255 | map (1 byte follow) | 0xb8
object | size: 256..65535 | map (2 bytes follow) | 0xb9
object | size: 65536..4294967295 | map (4 bytes follow) | 0xba
object | size: 4294967296..18446744073709551615 | map (8 bytes follow) | 0xbb
  Note The mapping is complete in the sense that any JSON value type can be converted to a CBOR value. The following CBOR types are not used in the conversion: byte strings (0x40..0x5f) UTF-8 strings terminated by "break" (0x7f) arrays terminated by "break" (0x9f) maps terminated by "break" (0xbf) 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) half and single-precision floats (0xf9-0xfa) break (0xff) Parameters
  [in] | j | JSON 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 #include <iomanip> // for std::setw 3 4 using json = nlohmann::json; 5 6 int main() 7 { 8 // create a JSON value 9 json j = R"({"compact": true, "schema": 0})"_json; 10 11 // serialize it to CBOR 12 std::vector<uint8_t> v = json::to_cbor(j); 13 14 // print the vector content 15 for (auto& byte : v) 16 { 17 std::cout << "0x" << std::hex << std::setw(2) << std::setfill('0') << (int)byte << " "; 18 } 19 std::cout << std::endl; 20 } nlohmann::json basic_json<> json default JSON class Definition: json.hpp:13933 nlohmann::basic_json::to_cbor static std::vector< uint8_t > to_cbor(const basic_json &j) create a MessagePack serialization of a given JSON value Definition: json.hpp:9267 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 9267 of file json.hpp.