MCPcopy Create free account
hub / github.com/danielaparker/jsoncons / to_from_ubjson_using_basic_json

Function to_from_ubjson_using_basic_json

examples/src/ubjson_examples.cpp:18–64  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

16using namespace jsoncons;
17
18void to_from_ubjson_using_basic_json()
19{
20 ojson j1 = ojson::parse(R"(
21 {
22 "application": "hiking",
23 "reputons": [
24 {
25 "rater": "HikingAsylum",
26 "assertion": "advanced",
27 "rated": "Marilyn C",
28 "rating": 0.90,
29 "generated": 1514862245
30 }
31 ]
32 }
33 )");
34
35 // Encode a basic_json value to UBJSON
36 std::vector<uint8_t> data;
37 ubjson::encode_ubjson(j1, data);
38
39 // Decode UBJSON to a basic_json value
40 ojson j2 = ubjson::decode_ubjson<ojson>(data);
41 std::cout << "(1)\n" << pretty_print(j2) << "\n\n";
42
43 // Accessing the data items
44
45 const ojson& reputons = j2["reputons"];
46
47 std::cout << "(2)\n";
48 for (const auto& element : reputons.array_range())
49 {
50 std::cout << element.at("rated").as<std::string>() << ", ";
51 std::cout << element.at("rating").as<double>() << "\n";
52 }
53 std::cout << '\n';
54
55 // Get a UBJSON value for a nested data item with jsonpointer
56 std::error_code ec;
57 auto const& rated = jsonpointer::get(j2, "/reputons/0/rated", ec);
58 if (!ec)
59 {
60 std::cout << "(3) " << rated.as_string() << "\n";
61 }
62
63 std::cout << '\n';
64}
65
66void to_from_ubjson_using_example_type()
67{

Callers 1

mainFunction · 0.85

Calls 5

parseFunction · 0.85
encode_ubjsonFunction · 0.85
pretty_printFunction · 0.85
atMethod · 0.80
getFunction · 0.50

Tested by

no test coverage detected