|
◆ to_cbor() [1/3]
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, class BinaryType = std::vector<std::uint8_t>>
static std::vector< std::uint8_t > nlohmann::basic_json< ObjectType, ArrayType, StringType, BooleanType, NumberIntegerType, NumberUnsignedType, NumberFloatType, AllocatorType, JSONSerializer, BinaryType >::to_cbor |
( |
const basic_json< ObjectType, ArrayType, StringType, BooleanType, NumberIntegerType, NumberUnsignedType, NumberFloatType, AllocatorType, JSONSerializer, BinaryType > & |
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 |
Binary values with subtype are mapped to tagged values (0xD8..0xDB) depending on the subtype, followed by a byte string, see "binary" cells in the table above.
- 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)
- 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.
3#include <nlohmann/json.hpp>
10 json j = R "({"compact": true, "schema": 0})"_json;
18 std::cout << "0x" << std::hex << std::setw(2) << std::setfill( '0') << (int) byte << " ";
20 std::cout << std::endl;
static std::vector< std::uint8_t > to_cbor(const basic_json &j) create a CBOR serialization of a given JSON value
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 -Isingle_include doc/examples/to_cbor.cpp -o to_cbor
- See also
- http://cbor.io
-
see from_cbor(InputType&&, const bool, const bool, const cbor_tag_handler_t) for the analogous deserialization
-
see to_msgpack(const basic_json&) for the related MessagePack format
-
see 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 24837 of file json.hpp.
|