1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501
|
### jsoncons::cbor::basic_cbor_cursor
```cpp
#include <jsoncons_ext/cbor/cbor_cursor.hpp>
template<
typename Source=jsoncons::binary_stream_source,
typename Allocator=std::allocator<char>>
class basic_cbor_cursor;
```
A pull parser for reporting CBOR parse events. A typical application will
repeatedly process the `current()` event and call the `next()`
function to advance to the next event, until `done()` returns `true`.
In addition, when positioned on a `begin_object` event,
the `read_to` function can pull a complete object representing
the events from `begin_object` to `end_object`,
and when positioned on a `begin_array` event, a complete array
representing the events from `begin_array` ro `end_array`.
`basic_cbor_cursor` is noncopyable and nonmoveable.
Aliases for common sources are provided:
Type |Definition
--------------------|------------------------------
cbor_stream_cursor |basic_cbor_cursor<jsoncons::binary_stream_source>
cbor_bytes_cursor |basic_cbor_cursor<jsoncons::bytes_source>
### Implemented interfaces
[staj_cursor](staj_cursor.md)
#### Constructors
basic_cbor_cursor(Sourceable&& source,
const cbor_decode_options& options = cbor_decode_options(),
const Allocator& alloc = Allocator()); (1)
template <typename Sourceable>
basic_cbor_cursor(Sourceable&& source,
std::error_code& ec); (2)
template <typename Sourceable>
basic_cbor_cursor(Sourceable&& source,
const cbor_decode_options& options,
std::error_code& ec); (3)
template <typename Sourceable>
basic_cbor_cursor(std::allocator_arg_t, const Allocator& alloc,
Sourceable&& source,
const cbor_decode_options& options,
std::error_code& ec); (4)
Constructors (1) reads from a buffer or stream source and throws a
[ser_error](ser_error.md) if a parsing error is encountered while processing the initial event.
Constructors (2)-(4) read from a buffer or stream source and set `ec`
if a parsing error is encountered while processing the initial event.
Note: It is the programmer's responsibility to ensure that `basic_cbor_cursor` does not outlive any source passed in the constuctor,
as `basic_cbor_cursor` holds a pointer to but does not own this object.
#### Parameters
`source` - a value from which a `source_type` is constructible.
#### Member functions
uint64_t raw_tag() const; // (since 1.2.0)
Returns the CBOR tag associated with the current value
bool done() const override;
Checks if there are no more events.
const staj_event& current() const override;
Returns the current [staj_event](basic_staj_event.md).
void read_to(json_visitor& visitor) override
Feeds the current and succeeding [staj events](basic_staj_event.md) through the provided
[visitor](basic_json_visitor.md), until the visitor indicates
to stop. If a parsing error is encountered, throws a [ser_error](ser_error.md).
void read_to(json_visitor& visitor, std::error_code& ec) override
Feeds the current and succeeding [staj events](basic_staj_event.md) through the provided
[visitor](basic_json_visitor.md), until the visitor indicates
to stop. If a parsing error is encountered, sets `ec`.
void next() override;
Advances to the next event. If a parsing error is encountered, throws a
[ser_error](ser_error.md).
void next(std::error_code& ec) override;
Advances to the next event. If a parsing error is encountered, sets `ec`.
const ser_context& context() const override;
Returns the current [context](ser_context.md)
void reset();
Reset cursor to read another value from the same source
template <typename Sourceable>
reset(Sourceable&& source)
Reset cursor to read new value from a new sources
#### Non-member functions
template <typename Source,typename Allocator>
staj_filter_view operator|(basic_cbor_cursor<Source,Allocator>& cursor,
std::function<bool(const staj_event&, const ser_context&)> pred);
### Examples
Input JSON file `book_catalog.json`:
```json
[
{
"author" : "Haruki Murakami",
"title" : "Hard-Boiled Wonderland and the End of the World",
"isbn" : "0679743464",
"publisher" : "Vintage",
"date" : "1993-03-02",
"price": 18.90
},
{
"author" : "Graham Greene",
"title" : "The Comedians",
"isbn" : "0099478374",
"publisher" : "Vintage Classics",
"date" : "2005-09-21",
"price": 15.74
}
]
```
#### Read CBOR parse events
```cpp
#include <jsoncons_ext/cbor/cbor_cursor.hpp>
#include <string>
#include <fstream>
using namespace jsoncons;
int main()
{
std::ifstream is("book_catalog.json");
cbor_cursor cursor(is);
for (; !cursor.done(); cursor.next())
{
const auto& event = cursor.current();
switch (event.event_type())
{
case staj_event_type::begin_array:
std::cout << event.event_type() << "\n";
break;
case staj_event_type::end_array:
std::cout << event.event_type() << "\n";
break;
case staj_event_type::begin_object:
std::cout << event.event_type() << "\n";
break;
case staj_event_type::end_object:
std::cout << event.event_type() << "\n";
break;
case staj_event_type::key:
// Or std::string_view, if supported
std::cout << event.event_type() << ": " << event.get<jsoncons::string_view>() << "\n";
break;
case staj_event_type::string_value:
// Or std::string_view, if supported
std::cout << event.event_type() << ": " << event.get<jsoncons::string_view>() << "\n";
break;
case staj_event_type::null_value:
std::cout << event.event_type() << ": " << "\n";
break;
case staj_event_type::bool_value:
std::cout << event.event_type() << ": " << std::boolalpha << event.get<bool>() << "\n";
break;
case staj_event_type::int64_value:
std::cout << event.event_type() << ": " << event.get<int64_t>() << "\n";
break;
case staj_event_type::uint64_value:
std::cout << event.event_type() << ": " << event.get<uint64_t>() << "\n";
break;
case staj_event_type::half_value:
case staj_event_type::double_value:
std::cout << event.event_type() << ": " << event.get<double>() << "\n";
break;
default:
std::cout << "Unhandled event type\n";
break;
}
}
}
```
Output:
```
begin_array
begin_object
key: author
string_value: Haruki Murakami
key: title
string_value: Hard-Boiled Wonderland and the End of the World
key: isbn
string_value: 0679743464
key: publisher
string_value: Vintage
key: date
string_value: 1993-03-02
key: price
double_value: 19
end_object
begin_object
key: author
string_value: Graham Greene
key: title
string_value: The Comedians
key: isbn
string_value: 0099478374
key: publisher
string_value: Vintage Classics
key: date
string_value: 2005-09-21
key: price
double_value: 16
end_object
end_array
```
#### Filter CBOR parse events
```cpp
#include <jsoncons_ext/cbor/cbor_cursor.hpp>
#include <string>
#include <fstream>
using namespace jsoncons;
int main()
{
bool author_next = false;
auto filter = [&](const staj_event& event, const ser_context&) -> bool
{
if (event.event_type() == staj_event_type::key &&
event.get<jsoncons::string_view>() == "author")
{
author_next = true;
return false;
}
if (author_next)
{
author_next = false;
return true;
}
return false;
};
std::ifstream is("book_catalog.json");
cbor_cursor cursor(is);
auto filtered_c = cursor | filter;
for (; !filtered_c.done(); filtered_c.next())
{
const auto& event = filtered_c.current();
switch (event.event_type())
{
case staj_event_type::string_value:
std::cout << event.get<jsoncons::string_view>() << "\n";
break;
}
}
}
```
Output:
```
Haruki Murakami
Graham Greene
```
### Typed Array examples
#### Read a typed array
```cpp
#include <jsoncons/json.hpp>
#include <jsoncons_ext/cbor/cbor.hpp>
#include <iostream>
#include <iomanip>
#include <cassert>
struct my_cbor_visitor : public default_json_visitor
{
std::vector<double> v;
private:
bool visit_typed_array(const span<const double>& data,
semantic_tag,
const ser_context&,
std::error_code&) override
{
v = std::vector<double>(data.begin(),data.end());
return false;
}
};
int main()
{
std::vector<double> v{10.0,20.0,30.0,40.0};
std::vector<uint8_t> buffer;
auto options = cbor::cbor_options{}
.use_typed_arrays(true);
cbor::encode_cbor(v, buffer, options);
std::cout << "(1)\n";
std::cout << byte_string_view(buffer) << "\n\n";
/*
0xd8, // Tag
0x56, // Tag 86, float64, little endian, Typed Array
0x58,0x20, // Byte string value of length 32
0x00,0x00,0x00,0x00,0x00,0x00,0x24,0x40,
0x00,0x00,0x00,0x00,0x00,0x00,0x34,0x40,
0x00,0x00,0x00,0x00,0x00,0x00,0x3e,0x40,
0x00,0x00,0x00,0x00,0x00,0x00,0x44,0x40
*/
cbor::cbor_bytes_cursor cursor(buffer);
assert(cursor.current().event_type() == staj_event_type::begin_array);
assert(cursor.is_typed_array());
my_cbor_visitor visitor;
cursor.read_to(visitor);
std::cout << "(2)\n";
for (auto item : handler.v)
{
std::cout << item << "\n";
}
std::cout << "\n";
}
```
Output:
```
(1)
d8 56 58 20 00 00 00 00 00 00 24 40 00 00 00 00 00 00 34 40 00 00 00 00 00 00 3e 40 00 00 00 00 00 00 44 40
(2)
10
20
30
40
```
#### Navigating Typed Arrays with cursor - multi-dimensional row major with typed array
This example is taken from [CBOR Tags for Typed Arrays](https://tools.ietf.org/html/rfc8746)
```cpp
#include <jsoncons_ext/cbor/cbor_cursor.hpp>
int main()
{
const std::vector<uint8_t> input = {
0xd8,0x28, // Tag 40 (multi-dimensional row major array)
0x82, // array(2)
0x82, // array(2)
0x02, // unsigned(2) 1st Dimension
0x03, // unsigned(3) 2nd Dimension
0xd8,0x41, // Tag 65 (uint16 big endian Typed Array)
0x4c, // byte string(12)
0x00,0x02, // unsigned(2)
0x00,0x04, // unsigned(4)
0x00,0x08, // unsigned(8)
0x00,0x04, // unsigned(4)
0x00,0x10, // unsigned(16)
0x01,0x00 // unsigned(256)
};
cbor::cbor_bytes_cursor cursor(input);
for (; !cursor.done(); cursor.next())
{
const auto& event = cursor.current();
switch (event.event_type())
{
case staj_event_type::begin_array:
std::cout << event.event_type()
<< " " << "(" << event.tag() << ")\n";
break;
case staj_event_type::end_array:
std::cout << event.event_type()
<< " " << "(" << event.tag() << ")\n";
break;
case staj_event_type::uint64_value:
std::cout << event.event_type()
<< ": " << event.get<uint64_t>() << " " << "(" << event.tag() << ")\n";
break;
default:
std::cout << "Unhandled event type " << event.event_type()
<< " " << "(" << event.tag() << ")\n";
break;
}
}
}
```
Output:
```
begin_array (multi-dim-row-major)
begin_array (n/a)
uint64_value: 2 (n/a)
uint64_value: 3 (n/a)
end_array (n/a)
begin_array (n/a)
uint64_value: 2 (n/a)
uint64_value: 4 (n/a)
uint64_value: 8 (n/a)
uint64_value: 4 (n/a)
uint64_value: 10 (n/a)
uint64_value: 100 (n/a)
end_array (n/a)
end_array (n/a)
```
#### Navigating Typed Arrays with cursor - multi-dimensional column major with classical CBOR array
This example is taken from [CBOR Tags for Typed Arrays](https://tools.ietf.org/html/rfc8746)
```cpp
#include <jsoncons_ext/cbor/cbor_cursor.hpp>
int main()
{
const std::vector<uint8_t> input = {
0xd9,0x04,0x10, // Tag 1040 (multi-dimensional column major array)
0x82, // array(2)
0x82, // array(2)
0x02, // unsigned(2) 1st Dimension
0x03, // unsigned(3) 2nd Dimension
0x86, // array(6)
0x02, // unsigned(2)
0x04, // unsigned(4)
0x08, // unsigned(8)
0x04, // unsigned(4)
0x10, // unsigned(16)
0x19,0x01,0x00 // unsigned(256)
};
cbor::cbor_bytes_cursor cursor(input);
for (; !cursor.done(); cursor.next())
{
const auto& event = cursor.current();
switch (event.event_type())
{
case staj_event_type::begin_array:
std::cout << event.event_type()
<< " " << "(" << event.tag() << ")\n";
break;
case staj_event_type::end_array:
std::cout << event.event_type()
<< " " << "(" << event.tag() << ")\n";
break;
case staj_event_type::uint64_value:
std::cout << event.event_type()
<< ": " << event.get<uint64_t>() << " " << "(" << event.tag() << ")\n";
break;
default:
std::cout << "Unhandled event type " << event.event_type()
<< " " << "(" << event.tag() << ")\n";
break;
}
}
}
```
Output:
```
begin_array (multi-dim-column-major)
begin_array (n/a)
uint64_value: 2 (n/a)
uint64_value: 3 (n/a)
end_array (n/a)
begin_array (n/a)
uint64_value: 2 (n/a)
uint64_value: 4 (n/a)
uint64_value: 8 (n/a)
uint64_value: 4 (n/a)
uint64_value: 10 (n/a)
uint64_value: 100 (n/a)
end_array (n/a)
end_array (n/a)
```
### See also
[staj_event](../basic_staj_event.md)
[staj_array_iterator](staj_array_iterator.md)
[staj_object_iterator](staj_object_iterator.md)
|