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

Function cbor_reputon_example

examples/src/cbor_examples.cpp:82–128  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

80}
81
82void cbor_reputon_example()
83{
84 ojson j1 = ojson::parse(R"(
85 {
86 "application": "hiking",
87 "reputons": [
88 {
89 "rater": "HikingAsylum",
90 "assertion": "advanced",
91 "rated": "Marilyn C",
92 "rating": 0.90,
93 "generated": 1514862245
94 }
95 ]
96 }
97 )");
98
99 // Encode a basic_json value to a CBOR value
100 std::vector<uint8_t> data;
101 cbor::encode_cbor(j1, data);
102
103 // Decode a CBOR value to a basic_json value
104 ojson j2 = cbor::decode_cbor<ojson>(data);
105 std::cout << "(1)\n" << pretty_print(j2) << "\n\n";
106
107 // Accessing the data items
108
109 const ojson& reputons = j2["reputons"];
110
111 std::cout << "(2)\n";
112 for (const auto& element : reputons.array_range())
113 {
114 std::cout << element.at("rated").as<std::string>() << ", ";
115 std::cout << element.at("rating").as<double>() << "\n";
116 }
117 std::cout << '\n';
118
119 // Get a CBOR value for a nested data item with jsonpointer
120 std::error_code ec;
121 auto const& rated = jsonpointer::get(j2, "/reputons/0/rated", ec);
122 if (!ec)
123 {
124 std::cout << "(3) " << rated.as_string() << "\n";
125 }
126
127 std::cout << '\n';
128}
129
130void decode_cbor_byte_string()
131{

Callers 1

mainFunction · 0.85

Calls 5

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

Tested by

no test coverage detected