|
◆ 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< ObjectType, ArrayType, StringType, BooleanType, NumberIntegerType, NumberUnsignedType, NumberFloatType, AllocatorType, JSONSerializer >::to_cbor |
( |
const basic_json< ObjectType, ArrayType, StringType, BooleanType, NumberIntegerType, NumberUnsignedType, NumberFloatType, AllocatorType, JSONSerializer > & |
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] | j | JSON value to serialize |
- Returns
- MessagePack serialization as byte vector
- Complexity\n Linear in the size of the JSON value j.
- Example\n The example shows the serialization of a JSON value to a byte
- vector in CBOR format.
8 json j = R "({"compact": true, "schema": 0})"_json;
16 std::cout << "0x" << std::hex << std::setw(2) << std::setfill( '0') << ( int) byte << " ";
18 std::cout << std::endl;
static basic_json array(std::initializer_list< basic_json > init=std::initializer_list< basic_json >()) explicitly create an array from an initializer list
static std::vector< uint8_t > to_cbor(const basic_json &j) create a MessagePack serialization of a given JSON value
a class to store JSON values
basic_json<> json default JSON class
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.
|