mirror of
https://github.com/nlohmann/json.git
synced 2026-10-25 15:28:54 +00:00
- Make the iterator range illustrations legible in dark mode (prefers-color-scheme rules inside the SVGs). - Add a "Byte layout" section with a color-coded byte table to each binary format page (BJData, BON8, BSON, CBOR, MessagePack, UBJSON), and highlight the smallest size per file in the comparison table. - Add "See also" sections to the overview pages that lacked one. - Split the release notes into one page per release, grouped by minor version on the overview page, with an "API changes" section per release generated from the API pages' version histories (hooks/api_changes.py). - Fact-check the release notes and version histories against the release tags and correct wrong or misleading entries (e.g., json_serializer 2.1.0, merge_patch 3.1.0, items, count, operator[], to_bjdata, std::string_view key support). Signed-off-by: Niels Lohmann <mail@nlohmann.me>
5.2 KiB
5.2 KiB
nlohmann::basic_json::to_bjdata
// (1)
static std::vector<std::uint8_t> to_bjdata(const basic_json& j,
const bool use_size = false,
const bool use_type = false,
const bjdata_version_t version = bjdata_version_t::draft2,
const error_handler_t error_handler = error_handler_t::keep);
// (2)
static void to_bjdata(const basic_json& j, detail::output_adapter<std::uint8_t> o,
const bool use_size = false, const bool use_type = false,
const bjdata_version_t version = bjdata_version_t::draft2,
const error_handler_t error_handler = error_handler_t::keep);
static void to_bjdata(const basic_json& j, detail::output_adapter<char> o,
const bool use_size = false, const bool use_type = false,
const bjdata_version_t version = bjdata_version_t::draft2,
const error_handler_t error_handler = error_handler_t::keep);
Serializes a given JSON value j to a byte vector using the BJData (Binary JData) serialization format. BJData aims to
be more compact than JSON itself, yet more efficient to parse.
- Returns a byte vector containing the BJData serialization.
- Writes the BJData 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
use_size(in)- whether to add size annotations to container types; optional,
#!cpp falseby default. use_type(in)- whether to add type annotations to container types (must be combined with
#!cpp use_size = true); optional,#!cpp falseby default. version(in)- which version of BJData to use (see note on "Binary values" on BJData);
optional,
#!cpp bjdata_version_t::draft2by default. error_handler(in)- how to treat a string or object key in
jthat is not valid UTF-8; seeerror_handler_t. The default,keep, writes the ill-formed bytes to the output as is, as every version ofto_bjdatadid before this parameter was added;strictthrows;replace/ignoresanitize it the same waydumpwould. IfJSON_STRICT_BINARY_UTF8is enabled, the default isstrictinstead.
Return value
- BJData serialization as byte vector
- (none)
Exception safety
Strong guarantee: if an exception is thrown, there are no changes in the JSON value.
Exceptions
- Throws
other_error.502ifuse_typeis true anduse_sizeis false, andjcontains a non-empty array, object, or binary value. - Throws type_error.316 if a string or object key in
jis not valid UTF-8 anderror_handlerisstrict(the default only ifJSON_STRICT_BINARY_UTF8is enabled) - Throws type_error.321 if
jor a value nested in it is discarded; example:"cannot serialize discarded value to BJData"
Complexity
Linear in the size of the JSON value j.
Examples
??? example "Example: serialize a JSON value to BJData"
The example shows the serialization of a JSON value to a byte vector in BJData format.
```cpp
--8<-- "examples/to_bjdata.cpp"
```
Output:
```json
--8<-- "examples/to_bjdata.output"
```
??? example "Example: other_error.502 exception"
The example shows how requesting type annotations (`use_type`) without size annotations (`use_size`) throws an
exception, because type-optimized containers can only be read back with a preceding size.
```cpp
--8<-- "examples/to_bjdata__exception.cpp"
```
Output:
```json
--8<-- "examples/to_bjdata__exception.output"
```
See also
- from_bjdata create a JSON value from an input in BJData format
- to_cbor create a CBOR serialization of a JSON value
- to_msgpack create a MessagePack 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_bon8 create a BON8 serialization of a JSON value
Version history
- Added in version 3.11.0.
- BJData version parameter (for draft3 binary encoding) added in version 3.12.0.
- Added
error_handlerparameter in version 3.13.0. Its default,keep, writes the bytes of a string or object key that is not valid UTF-8 unchanged, as before;strict(the default ifJSON_STRICT_BINARY_UTF8is enabled) throwstype_error.316. - Throws
type_error.321for a discarded value since version 3.13.0; previously, a discarded value nested in an array or object was silently skipped, producing invalid BJData.