]>
git.proxmox.com Git - ceph.git/blob - ceph/src/jaegertracing/opentelemetry-cpp/third_party/nlohmann-json/doc/examples/to_ubjson.cpp
3 #include <nlohmann/json.hpp>
5 using json
= nlohmann::json
;
7 // function to print UBJSON's diagnostic format
8 void print_byte(uint8_t byte
)
10 if (32 < byte
and byte
< 128)
12 std::cout
<< (char)byte
;
16 std::cout
<< (int)byte
;
22 // create a JSON value
23 json j
= R
"({"compact
": true, "schema
": false})"_json
;
25 // serialize it to UBJSON
26 std::vector
<std::uint8_t> v
= json::to_ubjson(j
);
28 // print the vector content
33 std::cout
<< std::endl
;
35 // create an array of numbers
36 json array
= {1, 2, 3, 4, 5, 6, 7, 8};
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);
45 // print the vector contents
46 for (auto& byte
: v_array
)
50 std::cout
<< std::endl
;
52 for (auto& byte
: v_array_size
)
56 std::cout
<< std::endl
;
58 for (auto& byte
: v_array_size_and_type
)
62 std::cout
<< std::endl
;