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_msgpack() 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_msgpack | ( | const basic_json & | j | ) |
| inline static
Serializes a given JSON value j to a byte vector using the MessagePack serialization format. MessagePack 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 MessagePack types according to the MessagePack specification:
JSON value type | value/range | MessagePack type | first byte
--- | --- | --- | ---
null | `null` | nil | 0xc0
boolean | `true` | true | 0xc3
boolean | `false` | false | 0xc2
number_integer | -9223372036854775808..-2147483649 | int64 | 0xd3
number_integer | -2147483648..-32769 | int32 | 0xd2
number_integer | -32768..-129 | int16 | 0xd1
number_integer | -128..-33 | int8 | 0xd0
number_integer | -32..-1 | negative fixint | 0xe0..0xff
number_integer | 0..127 | positive fixint | 0x00..0x7f
number_integer | 128..255 | uint 8 | 0xcc
number_integer | 256..65535 | uint 16 | 0xcd
number_integer | 65536..4294967295 | uint 32 | 0xce
number_integer | 4294967296..18446744073709551615 | uint 64 | 0xcf
number_unsigned | 0..127 | positive fixint | 0x00..0x7f
number_unsigned | 128..255 | uint 8 | 0xcc
number_unsigned | 256..65535 | uint 16 | 0xcd
number_unsigned | 65536..4294967295 | uint 32 | 0xce
number_unsigned | 4294967296..18446744073709551615 | uint 64 | 0xcf
number_float | any value | float 64 | 0xcb
string | length: 0..31 | fixstr | 0xa0..0xbf
string | length: 32..255 | str 8 | 0xd9
string | length: 256..65535 | str 16 | 0xda
string | length: 65536..4294967295 | str 32 | 0xdb
array | size: 0..15 | fixarray | 0x90..0x9f
array | size: 16..65535 | array 16 | 0xdc
array | size: 65536..4294967295 | array 32 | 0xdd
object | size: 0..15 | fix map | 0x80..0x8f
object | size: 16..65535 | map 16 | 0xde
object | size: 65536..4294967295 | map 32 | 0xdf
  Note The mapping is complete in the sense that any JSON value type can be converted to a MessagePack value. The following values can not be converted to a MessagePack value: strings with more than 4294967295 bytes arrays with more than 4294967295 elements objects with more than 4294967295 elements The following MessagePack types are not used in the conversion: bin 8 - bin 32 (0xc4..0xc6) ext 8 - ext 32 (0xc7..0xc9) float 32 (0xca) fixext 1 - fixext 16 (0xd4..0xd8) Any MessagePack output created to_msgpack can be successfully parsed by from_msgpack. 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 MessagePack 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 MessagePack 12 std::vector<uint8_t> v = json::to_msgpack(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_msgpack static std::vector< uint8_t > to_msgpack(const basic_json &j) create a MessagePack serialization of a given JSON value Definition: json.hpp:9104 Output (play with this example online):
      0x82 0xa7 0x63 0x6f 0x6d 0x70 0x61 0x63 0x74 0xc3 0xa6 0x73 0x63 0x68 0x65 0x6d 0x61 0x00
  The example code above can be translated with
      g++ -std=c++11 -Isrc doc/examples/to_msgpack.cpp -o to_msgpack
See also http://msgpack.org from_msgpack(const std::vector<uint8_t>&, const size_t) for the analogous deserialization to_cbor(const basic_json& for the related CBOR format Since version 2.0.9 Definition at line 9104 of file json.hpp.