JSON for Modern C++ 3.7.3

accept
accept
accept
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
basic_json
basic_json
begin
begin
binary
binary
binary
binary
binary_t
boolean_t
cbegin
cend
clear
const_iterator
const_pointer
const_reference
const_reverse_iterator
contains
contains
count
crbegin
crend
detail::external_constructor
diff
difference_type
dump
emplace
emplace_back
empty
end
end
erase
erase
erase
erase
error_handler_t
exception
find
find
flatten
from_bson
from_bson
from_bson
from_cbor
from_cbor
from_cbor
from_msgpack
from_msgpack
from_msgpack
from_ubjson
from_ubjson
from_ubjson
front
front
get
get
get
get
get
get
get_allocator
get_binary
get_binary
get_ptr
get_ptr
get_ref
get_ref
get_to
get_to
initializer_list_t
input_format_t
insert
insert
insert
insert
insert
insert
insert_iterator
invalid_iterator
is_array
is_binary
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
items
items
iterator
iterator_wrapper
iterator_wrapper
json_pointer
json_sax_t
json_serializer
max_size
merge_patch
meta
number_float_t
number_integer_t
number_unsigned_t
object
object_comparator_t
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[]
other_error
out_of_range
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
sax_parse
sax_parse
sax_parse
size
size_type
string_t
swap
swap
swap
swap
swap
swap
to_bson
to_bson
to_bson
to_cbor
to_cbor
to_cbor
to_msgpack
to_msgpack
to_msgpack
to_ubjson
to_ubjson
to_ubjson
type
type_error
type_name
unflatten
update
update
value
value
value
value
value_t
value_type
~basic_json
| ## ◆ to_cbor() [1/3]
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. 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 representable by a float | Single-Precision Float | 0xFA
number_float | any value NOT representable by a float | 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
binary | size: 0..23 | byte string | 0x40..0x57
binary | size: 23..255 | byte string (1 byte follow) | 0x58
binary | size: 256..65535 | byte string (2 bytes follow) | 0x59
binary | size: 65536..4294967295 | byte string (4 bytes follow) | 0x5A
binary | size: 4294967296..18446744073709551615 | byte string (8 bytes follow) | 0x5B
  Note The mapping is complete in the sense that any JSON value type can be converted to a CBOR value. If NaN or Infinity are stored inside a JSON number, they are serialized properly. This behavior differs from the dump() function which serializes NaN or Infinity to `null`. The following CBOR types are not used in the conversion: UTF-8 strings terminated by "break" (0x7F) arrays terminated by "break" (0x9F) maps terminated by "break" (0xBF) byte strings terminated by "break" (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) half-precision floats (0xF9) break (0xFF) Parameters
  [in] | j | JSON value to serialize
  Returns CBOR 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 <iostream> 2 #include <iomanip> 3 #include <nlohmann/json.hpp> 4 5 using json = nlohmann::json; 6 7 int main() 8 { 9 // create a JSON value 10 json j = R"({"compact": true, "schema": 0})"_json; 11 12 // serialize it to CBOR 13 std::vector<uint8_t> v = json::to_cbor(j); 14 15 // print the vector content 16 for (auto& byte : v) 17 { 18 std::cout << "0x" << std::hex << std::setw(2) << std::setfill('0') << (int)byte << " "; 19 } 20 std::cout << std::endl; 21 } 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 -Isingle_include doc/examples/to_cbor.cpp -o to_cbor
See also http://cbor.io from_cbor(detail::input_adapter&&, const bool, const bool) for the analogous deserialization to_msgpack(const basic_json&) for the related MessagePack format to_ubjson(const basic_json&, const bool, const bool) for the related UBJSON format Since version 2.0.9; compact representation of floating-point numbers since version 3.8.0 Definition at line 22828 of file json.hpp.

nlohmann::json
basic_json<> json
default JSON class
Definition: json.hpp:2774
nlohmann::basic_json::to_cbor
static std::vector< uint8_t > to_cbor(const basic_json &j)
create a CBOR serialization of a given JSON value
Definition: json.hpp:22828