]> git.proxmox.com Git - ceph.git/blob - ceph/src/jaegertracing/opentelemetry-cpp/third_party/nlohmann-json/doc/examples/to_ubjson.cpp
update ceph source to reef 18.1.2
[ceph.git] / ceph / src / jaegertracing / opentelemetry-cpp / third_party / nlohmann-json / doc / examples / to_ubjson.cpp
1 #include <iostream>
2 #include <iomanip>
3 #include <nlohmann/json.hpp>
4
5 using json = nlohmann::json;
6
7 // function to print UBJSON's diagnostic format
8 void print_byte(uint8_t byte)
9 {
10 if (32 < byte and byte < 128)
11 {
12 std::cout << (char)byte;
13 }
14 else
15 {
16 std::cout << (int)byte;
17 }
18 }
19
20 int main()
21 {
22 // create a JSON value
23 json j = R"({"compact": true, "schema": false})"_json;
24
25 // serialize it to UBJSON
26 std::vector<std::uint8_t> v = json::to_ubjson(j);
27
28 // print the vector content
29 for (auto& byte : v)
30 {
31 print_byte(byte);
32 }
33 std::cout << std::endl;
34
35 // create an array of numbers
36 json array = {1, 2, 3, 4, 5, 6, 7, 8};
37
38 // serialize it to UBJSON using default representation
39 std::vector<std::uint8_t> v_array = json::to_ubjson(array);
40 // serialize it to UBJSON using size optimization
41 std::vector<std::uint8_t> v_array_size = json::to_ubjson(array, true);
42 // serialize it to UBJSON using type optimization
43 std::vector<std::uint8_t> v_array_size_and_type = json::to_ubjson(array, true, true);
44
45 // print the vector contents
46 for (auto& byte : v_array)
47 {
48 print_byte(byte);
49 }
50 std::cout << std::endl;
51
52 for (auto& byte : v_array_size)
53 {
54 print_byte(byte);
55 }
56 std::cout << std::endl;
57
58 for (auto& byte : v_array_size_and_type)
59 {
60 print_byte(byte);
61 }
62 std::cout << std::endl;
63 }