Skip to content

Commit 849e89c

Browse files
committed
msgpack_cursor
1 parent 912d71c commit 849e89c

File tree

3 files changed

+97
-32
lines changed

3 files changed

+97
-32
lines changed

include/jsoncons_ext/msgpack/msgpack_cursor.hpp

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ class basic_msgpack_cursor : public basic_staj_cursor<char>, private virtual ser
5555
cursor_visitor_(accept_all),
5656
cursor_handler_adaptor_(cursor_visitor_, alloc)
5757
{
58+
parser_.cursor_mode(true);
5859
if (!done())
5960
{
6061
next();
@@ -94,6 +95,7 @@ class basic_msgpack_cursor : public basic_staj_cursor<char>, private virtual ser
9495
cursor_handler_adaptor_(cursor_visitor_, alloc),
9596
eof_(false)
9697
{
98+
parser_.cursor_mode(true);
9799
if (!done())
98100
{
99101
next(ec);
@@ -178,9 +180,23 @@ class basic_msgpack_cursor : public basic_staj_cursor<char>, private virtual ser
178180
void read_to(basic_json_visitor<char_type>& visitor,
179181
std::error_code& ec) override
180182
{
181-
if (cursor_visitor_.dump(visitor, *this, ec))
183+
if (is_begin_container(current().event_type()))
182184
{
183-
read_next(visitor, ec);
185+
parser_.cursor_mode(false);
186+
parser_.mark_level(parser_.level());
187+
if (cursor_visitor_.event().send_json_event(visitor, *this, ec))
188+
{
189+
read_next(visitor, ec);
190+
}
191+
parser_.cursor_mode(true);
192+
parser_.mark_level(0);
193+
}
194+
else
195+
{
196+
if (cursor_visitor_.event().send_json_event(visitor, *this, ec))
197+
{
198+
read_next(visitor, ec);
199+
}
184200
}
185201
}
186202

include/jsoncons_ext/msgpack/msgpack_parser.hpp

Lines changed: 78 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -64,14 +64,17 @@ class basic_msgpack_parser : public ser_context
6464

6565
static constexpr int64_t nanos_in_second = 1000000000;
6666

67-
Source source_;
68-
msgpack_decode_options options_;
6967
bool more_{true};
7068
bool done_{false};
69+
int nesting_depth_{0};
70+
bool cursor_mode_{false};
71+
int mark_level_{0};
72+
73+
Source source_;
74+
msgpack_decode_options options_;
7175
std::basic_string<char,std::char_traits<char>,char_allocator_type> text_buffer_;
7276
std::vector<uint8_t,byte_allocator_type> bytes_buffer_;
7377
std::vector<parse_state,parse_state_allocator_type> state_stack_;
74-
int nesting_depth_{0};
7578

7679
public:
7780
template <typename Sourceable>
@@ -110,6 +113,26 @@ class basic_msgpack_parser : public ser_context
110113
reset();
111114
}
112115

116+
void cursor_mode(bool value)
117+
{
118+
cursor_mode_ = value;
119+
}
120+
121+
int level() const
122+
{
123+
return static_cast<int>(state_stack_.size());
124+
}
125+
126+
int mark_level() const
127+
{
128+
return mark_level_;
129+
}
130+
131+
void mark_level(int value)
132+
{
133+
mark_level_ = value;
134+
}
135+
113136
bool done() const
114137
{
115138
return done_;
@@ -227,7 +250,8 @@ class basic_msgpack_parser : public ser_context
227250
if (type <= 0x7f)
228251
{
229252
// positive fixint
230-
more_ = visitor.uint64_value(type, semantic_tag::none, *this, ec);
253+
visitor.uint64_value(type, semantic_tag::none, *this, ec);
254+
more_ = !cursor_mode_;
231255
}
232256
else if (type <= 0x8f)
233257
{
@@ -258,31 +282,36 @@ class basic_msgpack_parser : public ser_context
258282
more_ = false;
259283
return;
260284
}
261-
more_ = visitor.string_value(jsoncons::basic_string_view<char>(text_buffer_.data(),text_buffer_.length()), semantic_tag::none, *this, ec);
285+
visitor.string_value(jsoncons::basic_string_view<char>(text_buffer_.data(),text_buffer_.length()), semantic_tag::none, *this, ec);
286+
more_ = !cursor_mode_;
262287
}
263288
}
264289
else if (type >= 0xe0)
265290
{
266291
// negative fixint
267-
more_ = visitor.int64_value(static_cast<int8_t>(type), semantic_tag::none, *this, ec);
292+
visitor.int64_value(static_cast<int8_t>(type), semantic_tag::none, *this, ec);
293+
more_ = !cursor_mode_;
268294
}
269295
else
270296
{
271297
switch (type)
272298
{
273299
case jsoncons::msgpack::msgpack_type::nil_type:
274300
{
275-
more_ = visitor.null_value(semantic_tag::none, *this, ec);
301+
visitor.null_value(semantic_tag::none, *this, ec);
302+
more_ = !cursor_mode_;
276303
break;
277304
}
278305
case jsoncons::msgpack::msgpack_type::true_type:
279306
{
280-
more_ = visitor.bool_value(true, semantic_tag::none, *this, ec);
307+
visitor.bool_value(true, semantic_tag::none, *this, ec);
308+
more_ = !cursor_mode_;
281309
break;
282310
}
283311
case jsoncons::msgpack::msgpack_type::false_type:
284312
{
285-
more_ = visitor.bool_value(false, semantic_tag::none, *this, ec);
313+
visitor.bool_value(false, semantic_tag::none, *this, ec);
314+
more_ = !cursor_mode_;
286315
break;
287316
}
288317
case jsoncons::msgpack::msgpack_type::float32_type:
@@ -295,7 +324,8 @@ class basic_msgpack_parser : public ser_context
295324
return;
296325
}
297326
float val = binary::big_to_native<float>(buf, sizeof(buf));
298-
more_ = visitor.double_value(val, semantic_tag::none, *this, ec);
327+
visitor.double_value(val, semantic_tag::none, *this, ec);
328+
more_ = !cursor_mode_;
299329
break;
300330
}
301331

@@ -309,7 +339,8 @@ class basic_msgpack_parser : public ser_context
309339
return;
310340
}
311341
double val = binary::big_to_native<double>(buf, sizeof(buf));
312-
more_ = visitor.double_value(val, semantic_tag::none, *this, ec);
342+
visitor.double_value(val, semantic_tag::none, *this, ec);
343+
more_ = !cursor_mode_;
313344
break;
314345
}
315346

@@ -322,7 +353,8 @@ class basic_msgpack_parser : public ser_context
322353
more_ = false;
323354
return;
324355
}
325-
more_ = visitor.uint64_value(b, semantic_tag::none, *this, ec);
356+
visitor.uint64_value(b, semantic_tag::none, *this, ec);
357+
more_ = !cursor_mode_;
326358
break;
327359
}
328360

@@ -336,7 +368,8 @@ class basic_msgpack_parser : public ser_context
336368
return;
337369
}
338370
uint16_t val = binary::big_to_native<uint16_t>(buf, sizeof(buf));
339-
more_ = visitor.uint64_value(val, semantic_tag::none, *this, ec);
371+
visitor.uint64_value(val, semantic_tag::none, *this, ec);
372+
more_ = !cursor_mode_;
340373
break;
341374
}
342375

@@ -350,7 +383,8 @@ class basic_msgpack_parser : public ser_context
350383
return;
351384
}
352385
uint32_t val = binary::big_to_native<uint32_t>(buf, sizeof(buf));
353-
more_ = visitor.uint64_value(val, semantic_tag::none, *this, ec);
386+
visitor.uint64_value(val, semantic_tag::none, *this, ec);
387+
more_ = !cursor_mode_;
354388
break;
355389
}
356390

@@ -364,7 +398,8 @@ class basic_msgpack_parser : public ser_context
364398
return;
365399
}
366400
uint64_t val = binary::big_to_native<uint64_t>(buf, sizeof(buf));
367-
more_ = visitor.uint64_value(val, semantic_tag::none, *this, ec);
401+
visitor.uint64_value(val, semantic_tag::none, *this, ec);
402+
more_ = !cursor_mode_;
368403
break;
369404
}
370405

@@ -378,7 +413,8 @@ class basic_msgpack_parser : public ser_context
378413
return;
379414
}
380415
int8_t val = binary::big_to_native<int8_t>(buf, sizeof(buf));
381-
more_ = visitor.int64_value(val, semantic_tag::none, *this, ec);
416+
visitor.int64_value(val, semantic_tag::none, *this, ec);
417+
more_ = !cursor_mode_;
382418
break;
383419
}
384420

@@ -392,7 +428,8 @@ class basic_msgpack_parser : public ser_context
392428
return;
393429
}
394430
int16_t val = binary::big_to_native<int16_t>(buf, sizeof(buf));
395-
more_ = visitor.int64_value(val, semantic_tag::none, *this, ec);
431+
visitor.int64_value(val, semantic_tag::none, *this, ec);
432+
more_ = !cursor_mode_;
396433
break;
397434
}
398435

@@ -406,7 +443,8 @@ class basic_msgpack_parser : public ser_context
406443
return;
407444
}
408445
int32_t val = binary::big_to_native<int32_t>(buf, sizeof(buf));
409-
more_ = visitor.int64_value(val, semantic_tag::none, *this, ec);
446+
visitor.int64_value(val, semantic_tag::none, *this, ec);
447+
more_ = !cursor_mode_;
410448
break;
411449
}
412450

@@ -420,7 +458,8 @@ class basic_msgpack_parser : public ser_context
420458
return;
421459
}
422460
int64_t val = binary::big_to_native<int64_t>(buf, sizeof(buf));
423-
more_ = visitor.int64_value(val, semantic_tag::none, *this, ec);
461+
visitor.int64_value(val, semantic_tag::none, *this, ec);
462+
more_ = !cursor_mode_;
424463
break;
425464
}
426465

@@ -449,7 +488,8 @@ class basic_msgpack_parser : public ser_context
449488
more_ = false;
450489
return;
451490
}
452-
more_ = visitor.string_value(jsoncons::basic_string_view<char>(text_buffer_.data(),text_buffer_.length()), semantic_tag::none, *this, ec);
491+
visitor.string_value(jsoncons::basic_string_view<char>(text_buffer_.data(),text_buffer_.length()), semantic_tag::none, *this, ec);
492+
more_ = !cursor_mode_;
453493
break;
454494
}
455495

@@ -470,10 +510,11 @@ class basic_msgpack_parser : public ser_context
470510
return;
471511
}
472512

473-
more_ = visitor.byte_string_value(byte_string_view(bytes_buffer_.data(),bytes_buffer_.size()),
513+
visitor.byte_string_value(byte_string_view(bytes_buffer_.data(),bytes_buffer_.size()),
474514
semantic_tag::none,
475515
*this,
476516
ec);
517+
more_ = !cursor_mode_;
477518
break;
478519
}
479520
case jsoncons::msgpack::msgpack_type::fixext1_type:
@@ -519,7 +560,8 @@ class basic_msgpack_parser : public ser_context
519560
return;
520561
}
521562
uint32_t val = binary::big_to_native<uint32_t>(buf32, sizeof(buf32));
522-
more_ = visitor.uint64_value(val, semantic_tag::epoch_second, *this, ec);
563+
visitor.uint64_value(val, semantic_tag::epoch_second, *this, ec);
564+
more_ = !cursor_mode_;
523565
}
524566
else if (is_timestamp && len == 8)
525567
{
@@ -539,7 +581,8 @@ class basic_msgpack_parser : public ser_context
539581
nano += nsec;
540582
text_buffer_.clear();
541583
nano.write_string(text_buffer_);
542-
more_ = visitor.string_value(text_buffer_, semantic_tag::epoch_nano, *this, ec);
584+
visitor.string_value(text_buffer_, semantic_tag::epoch_nano, *this, ec);
585+
more_ = !cursor_mode_;
543586
if (!more_) return;
544587
}
545588
else if (is_timestamp && len == 12)
@@ -577,7 +620,8 @@ class basic_msgpack_parser : public ser_context
577620

578621
text_buffer_.clear();
579622
nano.write_string(text_buffer_);
580-
more_ = visitor.string_value(text_buffer_, semantic_tag::epoch_nano, *this, ec);
623+
visitor.string_value(text_buffer_, semantic_tag::epoch_nano, *this, ec);
624+
more_ = !cursor_mode_;
581625
if (!more_) return;
582626
}
583627
else
@@ -590,10 +634,11 @@ class basic_msgpack_parser : public ser_context
590634
return;
591635
}
592636

593-
more_ = visitor.byte_string_value(byte_string_view(bytes_buffer_.data(),bytes_buffer_.size()),
637+
visitor.byte_string_value(byte_string_view(bytes_buffer_.data(),bytes_buffer_.size()),
594638
static_cast<uint8_t>(ext_type),
595639
*this,
596640
ec);
641+
more_ = !cursor_mode_;
597642
}
598643
break;
599644
}
@@ -636,14 +681,16 @@ class basic_msgpack_parser : public ser_context
636681
return;
637682
}
638683
state_stack_.emplace_back(parse_mode::array,length);
639-
more_ = visitor.begin_array(length, semantic_tag::none, *this, ec);
684+
visitor.begin_array(length, semantic_tag::none, *this, ec);
685+
more_ = !cursor_mode_;
640686
}
641687

642688
void end_array(item_event_visitor& visitor, std::error_code& ec)
643689
{
644690
--nesting_depth_;
645691

646-
more_ = visitor.end_array(*this, ec);
692+
visitor.end_array(*this, ec);
693+
more_ = !cursor_mode_;
647694
state_stack_.pop_back();
648695
}
649696

@@ -661,13 +708,15 @@ class basic_msgpack_parser : public ser_context
661708
return;
662709
}
663710
state_stack_.emplace_back(parse_mode::map_key,length);
664-
more_ = visitor.begin_object(length, semantic_tag::none, *this, ec);
711+
visitor.begin_object(length, semantic_tag::none, *this, ec);
712+
more_ = !cursor_mode_;
665713
}
666714

667715
void end_object(item_event_visitor& visitor, std::error_code& ec)
668716
{
669717
--nesting_depth_;
670-
more_ = visitor.end_object(*this, ec);
718+
visitor.end_object(*this, ec);
719+
more_ = !cursor_mode_;
671720
state_stack_.pop_back();
672721
}
673722

include/jsoncons_ext/ubjson/ubjson_parser.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,14 +60,14 @@ class basic_ubjson_parser : public ser_context
6060

6161
bool more_{true};
6262
bool done_{false};
63+
int nesting_depth_{0};
6364
bool cursor_mode_{false};
6465
int mark_level_{0};
6566

6667
Source source_;
6768
ubjson_decode_options options_;
6869
std::basic_string<char,std::char_traits<char>,char_allocator_type> text_buffer_;
6970
std::vector<parse_state,parse_state_allocator_type> state_stack_;
70-
int nesting_depth_{0};
7171
public:
7272
template <typename Sourceable>
7373
basic_ubjson_parser(Sourceable&& source,

0 commit comments

Comments
 (0)