Files
json/docs/mkdocs/docs/api/basic_json/to_bjdata.md
T
Niels Lohmann b3ab23e11c docs: correct and complete the BJData documentation
Verify the BJData chapter against the library and the Draft 2/3/4
specifications and fix what did not match:

- bjdata.md: correct the list of unused markers (N, not Z), the ND-array
  example (3x2, not 2x3), the conditions under which an object is packed
  as an ND-array (exactly three keys, row vectors, float-only elements for
  single/double, char 0..127, byte only in Draft 3), the binary table row
  and notes (which flag/draft combinations write [$B), the "$" type
  restriction (Draft 3, Z is excluded too), the H marker in the
  deserialization table, and the claim of a complete deserialization
  mapping. Document the supported drafts, that Draft 4 (SoA containers,
  extension type E) and column-major ND-arrays are rejected, ND-arrays of
  half floats, duplicate object keys, and link the Draft 3 specifications
  instead of master.
- Add an API page for basic_json::bjdata_version_t with an example, and
  link it from the nav, the API index and to_bjdata.
- Update binary_formats/index.md and binary_values.md, which still claimed
  that BJData has no binary type, and fix a wrong byte comment there.
- Document parse_error.115 and the out_of_range.408 cases for from_bjdata,
  the precise other_error.502 condition for to_bjdata, and reword the note
  on truncated unsigned integers in exceptions.md (only ND-array elements
  were truncated before 3.13.0).

Signed-off-by: Niels Lohmann <mail@nlohmann.me>
2026-10-26 06:53:52 +01:00

5.4 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.

  1. Returns a byte vector containing the BJData serialization.
  2. 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 false by default.
use_type (in)
whether to add type annotations to container types (must be combined with #!cpp use_size = true); optional, #!cpp false by default.
version (in)
which version of BJData to use, see bjdata_version_t and the note on "Binary values" on BJData; optional, #!cpp bjdata_version_t::draft2 by default.
error_handler (in)
how to treat a string or object key in j that is not valid UTF-8; see error_handler_t. The default, keep, writes the ill-formed bytes to the output as is, as every version of to_bjdata did before this parameter was added; strict throws; replace/ignore sanitize it the same way dump would. If JSON_STRICT_BINARY_UTF8 is enabled, the default is strict instead.

Return value

  1. BJData serialization as byte vector
  2. (none)

Exception safety

Strong guarantee: if an exception is thrown, there are no changes in the JSON value.

Exceptions

  • Throws other_error.502 if use_type is true and use_size is false, and j contains a non-empty array or object, or a non-empty binary value (with version = bjdata_version_t::draft3, any binary value, including an empty one). Scalars, empty arrays, and empty objects do not throw.
  • Throws type_error.316 if a string or object key in j is not valid UTF-8 and error_handler is strict (the default only if JSON_STRICT_BINARY_UTF8 is enabled)
  • Throws type_error.321 if j or 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_handler parameter 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 if JSON_STRICT_BINARY_UTF8 is enabled) throws type_error.316.
  • Throws type_error.321 for a discarded value since version 3.13.0; previously, a discarded value nested in an array or object was silently skipped, producing invalid BJData.