mirror of
https://github.com/nlohmann/json.git
synced 2026-10-04 05:30:31 +00:00
* Fix to_msgpack() reading the inactive number union member basic_json stores number_integer and number_unsigned in a union, and number_unsigned_t only has to be at least as wide as number_integer_t (with the default types, both are 64-bit and have the same representation). When number_integer_t is narrower, write_msgpack() read the wrong union member in two places: - The number_unsigned case wrote number_integer's bits instead of number_unsigned's, silently writing the wrong value whenever it did not fit in number_integer_t. - The number_integer case (non-negative branch) picked the encoded width by comparing number_unsigned's bits, which is undefined behavior, though the value written was still number_integer's, so at worst a too-wide encoding was chosen. Read the active member in both cases, like the other binary writers (CBOR, UBJSON, BJData, BSON, BON8) already do. Fixes #5644. Signed-off-by: Niels Lohmann <mail@nlohmann.me> * Cast number_integer to number_unsigned_t only once in to_msgpack() Addresses review comment by @gregmarr. Signed-off-by: Niels Lohmann <mail@nlohmann.me> --------- Signed-off-by: Niels Lohmann <mail@nlohmann.me>
2.7 KiB
2.7 KiB
nlohmann::basic_json::to_msgpack
// (1)
static std::vector<std::uint8_t> to_msgpack(const basic_json& j);
// (2)
static void to_msgpack(const basic_json& j, detail::output_adapter<std::uint8_t> o);
static void to_msgpack(const basic_json& j, detail::output_adapter<char> o);
Serializes a given JSON value j to a byte vector using the MessagePack serialization format. MessagePack is a binary
serialization format that aims to be more compact than JSON itself, yet more efficient to parse.
- Returns a byte vector containing the MessagePack serialization.
- Writes the MessagePack serialization to an output adapter.
The exact mapping and its limitations are described on a dedicated page.
Parameters
j(in)- JSON value to serialize
o(in)- output adapter to write serialization to
Return value
- MessagePack serialization as a byte vector
- (none)
Exception safety
Strong guarantee: if an exception is thrown, there are no changes in the JSON value.
Exceptions
- Throws
out_of_range.412if the length of a string, binary value, array, or object exceeds 4294967295, the maximum MessagePack can store; example:"MessagePack length 4294967296 exceeds maximum of 4294967295" - Throws
out_of_range.415if the subtype of a binary value exceeds 255, the maximum of the MessagePack ext type; example:"subtype 70000 is too large for the MessagePack ext type (max 255)"
Complexity
Linear in the size of the JSON value j.
Examples
??? example
The example shows the serialization of a JSON value to a byte vector in MessagePack format.
```cpp
--8<-- "examples/to_msgpack.cpp"
```
Output:
```json
--8<-- "examples/to_msgpack.output"
```
See also
- from_msgpack create a JSON value from an input in MessagePack format
- to_cbor create a CBOR serialization of a JSON value
- to_bson create a BSON serialization of a JSON value
- to_ubjson create a UBJSON serialization of a JSON value
- to_bjdata create a BJData serialization of a JSON value
- to_bon8 create a BON8 serialization of a JSON value
Version history
- Added in version 2.0.9.
- Throws
out_of_range.412andout_of_range.415since version 3.13.0. - Fixed in version 3.13.0 to serialize
number_integer_t/number_unsigned_tpairs of different width correctly; before, integers could be serialized with the wrong value ifnumber_integer_twas narrower thannumber_unsigned_t.