Skip to content

Commit

Permalink
cbor_event_reader
Browse files Browse the repository at this point in the history
  • Loading branch information
danielaparker committed Mar 12, 2025
1 parent 3020650 commit dd67430
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 31 deletions.
47 changes: 31 additions & 16 deletions include/jsoncons_ext/cbor/cbor_event_reader.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ namespace cbor {
using allocator_type = Allocator;
private:
basic_cbor_parser<Source,Allocator> parser_;
basic_item_event_receiver<char_type> event_receiver_;
bool eof_;
basic_item_event_receiver<char_type> cursor_visitor_;
bool eof_{false};

public:
using string_view_type = string_view;
Expand All @@ -49,9 +49,9 @@ namespace cbor {
const cbor_decode_options& options = cbor_decode_options(),
const Allocator& alloc = Allocator())
: parser_(std::forward<Sourceable>(source), options, alloc),
event_receiver_(accept_all),
eof_(false)
cursor_visitor_(accept_all)
{
parser_.cursor_mode(true);
if (!done())
{
next();
Expand Down Expand Up @@ -87,9 +87,10 @@ namespace cbor {
const cbor_decode_options& options,
std::error_code& ec)
: parser_(std::forward<Sourceable>(source), options, alloc),
event_receiver_(accept_all),
cursor_visitor_(accept_all),
eof_(false)
{
parser_.cursor_mode(true);
if (!done())
{
next(ec);
Expand All @@ -104,7 +105,7 @@ namespace cbor {
void reset()
{
parser_.reset();
event_receiver_.reset();
cursor_visitor_.reset();
eof_ = false;
if (!done())
{
Expand All @@ -116,7 +117,7 @@ namespace cbor {
void reset(Sourceable&& source)
{
parser_.reset(std::forward<Sourceable>(source));
event_receiver_.reset();
cursor_visitor_.reset();
eof_ = false;
if (!done())
{
Expand All @@ -127,7 +128,7 @@ namespace cbor {
void reset(std::error_code& ec)
{
parser_.reset();
event_receiver_.reset();
cursor_visitor_.reset();
eof_ = false;
if (!done())
{
Expand All @@ -139,7 +140,7 @@ namespace cbor {
void reset(Sourceable&& source, std::error_code& ec)
{
parser_.reset(std::forward<Sourceable>(source));
event_receiver_.reset();
cursor_visitor_.reset();
eof_ = false;
if (!done())
{
Expand All @@ -154,12 +155,12 @@ namespace cbor {

bool is_typed_array() const
{
return event_receiver_.is_typed_array();
return cursor_visitor_.is_typed_array();
}

const basic_staj_event<char_type>& current() const override
{
return event_receiver_.event();
return cursor_visitor_.event();
}

void read_to(basic_item_event_visitor<char_type>& visitor) override
Expand All @@ -175,9 +176,23 @@ namespace cbor {
void read_to(basic_item_event_visitor<char_type>& visitor,
std::error_code& ec) override
{
if (event_receiver_.dump(visitor, *this, ec))
if (is_begin_container(current().event_type()))
{
read_next(visitor, ec);
parser_.cursor_mode(false);
parser_.mark_level(parser_.level());
if (cursor_visitor_.dump(visitor, *this, ec))
{
read_next(visitor, ec);
}
parser_.cursor_mode(true);
parser_.mark_level(0);
}
else
{
if (cursor_visitor_.dump(visitor, *this, ec))
{
read_next(visitor, ec);
}
}
}

Expand Down Expand Up @@ -231,16 +246,16 @@ namespace cbor {

void read_next(std::error_code& ec)
{
if (event_receiver_.in_available())
if (cursor_visitor_.in_available())
{
event_receiver_.send_available(ec);
cursor_visitor_.send_available(ec);
}
else
{
parser_.restart();
while (!parser_.stopped())
{
parser_.parse(event_receiver_, ec);
parser_.parse(cursor_visitor_, ec);
if (ec) {return;}
}
}
Expand Down
45 changes: 30 additions & 15 deletions include/jsoncons_ext/msgpack/msgpack_event_reader.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ namespace msgpack {
using allocator_type = Allocator;
private:
basic_msgpack_parser<Source,Allocator> parser_;
basic_item_event_receiver<char_type> event_receiver_;
bool eof_;
basic_item_event_receiver<char_type> cursor_visitor_;
bool eof_{false};

public:
using string_view_type = string_view;
Expand All @@ -47,9 +47,9 @@ namespace msgpack {
const msgpack_decode_options& options = msgpack_decode_options(),
const Allocator& alloc = Allocator())
: parser_(std::forward<Sourceable>(source), options, alloc),
event_receiver_(accept_all),
eof_(false)
cursor_visitor_(accept_all)
{
parser_.cursor_mode(true);
if (!done())
{
next();
Expand Down Expand Up @@ -89,9 +89,10 @@ namespace msgpack {
const msgpack_decode_options& options,
std::error_code& ec)
: parser_(std::forward<Sourceable>(source), options, alloc),
event_receiver_(accept_all),
cursor_visitor_(accept_all),
eof_(false)
{
parser_.cursor_mode(true);
if (!done())
{
next(ec);
Expand All @@ -106,7 +107,7 @@ namespace msgpack {
void reset()
{
parser_.reset();
event_receiver_.reset();
cursor_visitor_.reset();
eof_ = false;
if (!done())
{
Expand All @@ -118,7 +119,7 @@ namespace msgpack {
void reset(Sourceable&& source)
{
parser_.reset(std::forward<Sourceable>(source));
event_receiver_.reset();
cursor_visitor_.reset();
eof_ = false;
if (!done())
{
Expand All @@ -129,7 +130,7 @@ namespace msgpack {
void reset(std::error_code& ec)
{
parser_.reset();
event_receiver_.reset();
cursor_visitor_.reset();
eof_ = false;
if (!done())
{
Expand All @@ -141,7 +142,7 @@ namespace msgpack {
void reset(Sourceable&& source, std::error_code& ec)
{
parser_.reset(std::forward<Sourceable>(source));
event_receiver_.reset();
cursor_visitor_.reset();
eof_ = false;
if (!done())
{
Expand All @@ -156,7 +157,7 @@ namespace msgpack {

const basic_staj_event<char_type>& current() const override
{
return event_receiver_.event();
return cursor_visitor_.event();
}

void read_to(basic_item_event_visitor<char_type>& visitor) override
Expand All @@ -172,9 +173,23 @@ namespace msgpack {
void read_to(basic_item_event_visitor<char_type>& visitor,
std::error_code& ec) override
{
if (event_receiver_.dump(visitor, *this, ec))
if (is_begin_container(current().event_type()))
{
read_next(visitor, ec);
parser_.cursor_mode(false);
parser_.mark_level(parser_.level());
if (cursor_visitor_.dump(visitor, *this, ec))
{
read_next(visitor, ec);
}
parser_.cursor_mode(true);
parser_.mark_level(0);
}
else
{
if (cursor_visitor_.dump(visitor, *this, ec))
{
read_next(visitor, ec);
}
}
}

Expand Down Expand Up @@ -228,16 +243,16 @@ namespace msgpack {

void read_next(std::error_code& ec)
{
if (event_receiver_.in_available())
if (cursor_visitor_.in_available())
{
event_receiver_.send_available(ec);
cursor_visitor_.send_available(ec);
}
else
{
parser_.restart();
while (!parser_.stopped())
{
parser_.parse(event_receiver_, ec);
parser_.parse(cursor_visitor_, ec);
if (ec) {return;}
}
}
Expand Down

0 comments on commit dd67430

Please sign in to comment.