• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# <small>nlohmann::basic_json::</small>to_ubjson
2
3```cpp
4// (1)
5static std::vector<std::uint8_t> to_ubjson(const basic_json& j,
6                                           const bool use_size = false,
7                                           const bool use_type = false);
8
9// (2)
10static void to_ubjson(const basic_json& j, detail::output_adapter<std::uint8_t> o,
11                      const bool use_size = false, const bool use_type = false);
12static void to_ubjson(const basic_json& j, detail::output_adapter<char> o,
13                      const bool use_size = false, const bool use_type = false);
14```
15
16Serializes a given JSON value `j` to a byte vector using the UBJSON (Universal Binary JSON) serialization format. UBJSON
17aims to be more compact than JSON itself, yet more efficient to parse.
18
191. Returns a byte vector containing the UBJSON serialization.
202. Writes the UBJSON serialization to an output adapter.
21
22The exact mapping and its limitations is described on a [dedicated page](../../features/binary_formats/ubjson.md).
23
24## Parameters
25
26`j` (in)
27:   JSON value to serialize
28
29`o` (in)
30:   output adapter to write serialization to
31
32`use_size` (in)
33:   whether to add size annotations to container types; optional, `#!cpp false` by default.
34
35`use_type` (in)
36:   whether to add type annotations to container types (must be combined with `#!cpp use_size = true`); optional,
37    `#!cpp false` by default.
38
39## Return value
40
411. UBJSON serialization as byte vector
422. (none)
43
44## Exception safety
45
46Strong guarantee: if an exception is thrown, there are no changes in the JSON value.
47
48## Complexity
49
50Linear in the size of the JSON value `j`.
51
52## Examples
53
54??? example
55
56    The example shows the serialization of a JSON value to a byte vector in UBJSON format.
57
58    ```cpp
59    --8<-- "examples/to_ubjson.cpp"
60    ```
61
62    Output:
63
64    ```json
65    --8<-- "examples/to_ubjson.output"
66    ```
67
68## Version history
69
70- Added in version 3.1.0.
71