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

Function playing_around

examples/src/readme_examples.cpp:222–334  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

220}
221
222void playing_around()
223{
224 // Construct some CBOR using the streaming API
225 std::vector<uint8_t> bytes_in;
226 cbor::cbor_bytes_encoder encoder(bytes_in);
227 encoder.begin_array(); // indefinite length outer array
228 encoder.begin_array(3); // a fixed length array
229 encoder.string_value("foo");
230 encoder.byte_string_value(std::vector<uint8_t>{'P','u','s','s'}); // no suggested conversion
231 encoder.string_value("-18446744073709551617", semantic_tag::bigint);
232 encoder.end_array();
233 encoder.end_array();
234 encoder.flush();
235
236 // Print bytes
237 std::cout << "(1)\n" << byte_string_view(bytes_in) << "\n\n";
238/*
239 9f -- Start indefinte length array
240 83 -- Array of length 3
241 63 -- String value of length 3
242 666f6f -- "foo"
243 44 -- Byte string value of length 4
244 50757373 -- 'P''u''s''s'
245 c3 -- Tag 3 (negative bignum)
246 49 -- Byte string value of length 9
247 010000000000000000 -- Bytes content
248 ff -- "break"
249*/
250 // Unpack bytes into a json variant value, and add some more elements
251 json j = cbor::decode_cbor<json>(bytes_in);
252
253 // Loop over the rows
254 std::cout << "(2)\n";
255 for (const json& row : j.array_range())
256 {
257 std::cout << row << "\n";
258 }
259 std::cout << "\n";
260
261 // Get bignum value at position 0/2 using jsonpointer
262 json& v = jsonpointer::get(j, "/0/2");
263 std::cout << "(3) " << v.as<std::string>() << "\n\n";
264
265 // Print JSON representation with default options
266 std::cout << "(4)\n";
267 std::cout << pretty_print(j) << "\n\n";
268
269 // Print JSON representation with different options
270 auto options = json_options{}
271 .byte_string_format(byte_string_chars_format::base64)
272 .bignum_format(bignum_format_kind::base64url);
273 std::cout << "(5)\n";
274 std::cout << pretty_print(j, options) << "\n\n";
275
276 // Add some more elements
277
278 json another_array(json_array_arg);
279 another_array.emplace_back(byte_string_arg, std::vector<uint8_t>({'P','u','s','s'}),

Callers 1

mainFunction · 0.85

Calls 15

byte_string_viewClass · 0.85
pretty_printFunction · 0.85
encode_cborFunction · 0.85
encode_csvFunction · 0.85
bignum_formatMethod · 0.80
byte_string_formatMethod · 0.80
emplaceMethod · 0.80
getFunction · 0.50
begin_arrayMethod · 0.45
string_valueMethod · 0.45
byte_string_valueMethod · 0.45
end_arrayMethod · 0.45

Tested by

no test coverage detected