• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# basic_json::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
22## Parameters
23
24`j` (in)
25:   JSON value to serialize
26
27`o` (in)
28:   output adapter to write serialization to
29
30`use_size` (in)
31:   whether to add size annotations to container types; optional, `#!cpp false` by default.
32
33`use_type` (in)
34:   whether to add type annotations to container types (must be combined with `#!cpp use_size = true`); optional,
35    `#!cpp false` by default.
36
37## Return value
38
391. UBJSON serialization as byte vector
402. /
41
42## Exception safety
43
44Strong guarantee: if an exception is thrown, there are no changes in the JSON value.
45
46## Complexity
47
48Linear in the size of the JSON value `j`.
49
50## Example
51
52??? example
53
54    The example shows the serialization of a JSON value to a byte vector in UBJSON format.
55
56    ```cpp
57    --8<-- "examples/to_ubjson.cpp"
58    ```
59
60    Output:
61
62    ```json
63    --8<-- "examples/to_ubjson.output"
64    ```
65
66## Version history
67
68- Added in version 3.1.0.
69