mirror of
https://github.com/nlohmann/json.git
synced 2026-09-28 13:05:46 +00:00
* Throw instead of writing MessagePack lengths beyond UINT32_MAX MessagePack stores the length of a string, binary value, array, or object in at most 32 bits. For a larger value, to_msgpack wrote no length at all, so the output could not be read back. It now throws out_of_range.412, which BSON already uses for its 32-bit length fields. The check lives in one function, so each length is written by an if/else chain that ends in a plain else, without a condition that can never be false. It is tested with string and binary types that report a size beyond UINT32_MAX without allocating it, like the BSON tests do. Signed-off-by: Niels Lohmann <mail@nlohmann.me> * Fix the CI failures of the MessagePack length check - mark to_msgpack_length's value as used when exceptions are disabled (-Wunused-parameter, misc-unused-parameters) - put "Exception safety" before "Exceptions" in to_msgpack.md, as the documentation style check requires - create the test's string value from its type: constructing it from a beyond_uint32_string_t considers the std::filesystem::path conversion, which libstdc++ 10 reports as ambiguous for a class derived from std::string (clang 13) Signed-off-by: Niels Lohmann <mail@nlohmann.me> * Skip the MessagePack string length test for clang with libstdc++ 10 C++17 builds consider the std::filesystem::path conversion for the string type, and with clang and libstdc++ 10 that conversion is ambiguous for a class derived from std::string. Creating the value from its type did not avoid it, since any basic_json with that string type instantiates the check. The binary and ext cases are still tested there. Signed-off-by: Niels Lohmann <mail@nlohmann.me> * Keep the MessagePack string test type and its alias in one block astyle indented the alias oddly when it had an #ifdef of its own after the binary alias; declare it right after the string type, in the same block. Signed-off-by: Niels Lohmann <mail@nlohmann.me> --------- Signed-off-by: Niels Lohmann <mail@nlohmann.me>
2.4 KiB
2.4 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
Version history
- Added in version 2.0.9.
- Throws
out_of_range.412andout_of_range.415since version 3.13.0.