Skip to content

Commit

Permalink
json_parser update -> set_buffer
Browse files Browse the repository at this point in the history
  • Loading branch information
danielaparker committed Nov 13, 2024
1 parent d81bf80 commit 5bb97b9
Show file tree
Hide file tree
Showing 13 changed files with 138 additions and 105 deletions.
42 changes: 19 additions & 23 deletions doc/ref/corelib/basic_json_parser.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,24 +13,6 @@ A `basic_json_parser` is an incremental json parser. It can be fed its input
in chunks, and does not require an entire file to be loaded in memory
at one time.

A buffer of text is supplied to the parser with a call to `update(buffer)`.
If a subsequent call to `parse_some` reaches the end of the buffer in the middle of parsing,
say after digesting the sequence 'f', 'a', 'l', member function `stopped()` will return `false`
and `source_exhausted()` will return `true`. Additional JSON text can be supplied to the parser,
`parse_some` called again, and parsing will resume from where it left off.

A typical application will repeatedly call the `parse_some` function
until `stopped()` returns true. A stopped state indicates that a content
visitor function returned `false`, an error occured, or a complete JSON
text has been consumed. If the latter, `done() `will return `true`.

As an alternative to repeatedly calling `parse_some()` until `stopped()`
returns `true`, when `source_exhausted()` is `true` and there is
no more input, `finish_parse` may be called.

`check_done` can be called to check if the input has any unconsumed
non-whitespace characters, which would normally be considered an error.

`basic_json_parser` is used by the push parser [basic_json_reader](basic_json_reader.md),
and by the pull parser [basic_json_cursor](basic_json_cursor.md).

Expand Down Expand Up @@ -79,14 +61,15 @@ and a specified [err_handler](err_handler.md).
(4) Constructs a `json_parser` that uses the specified [basic_json_options](basic_json_options.md)
and a specified [err_handler](err_handler.md).

Note: It is the programmer's responsibility to ensure that a `basic_json_parser` does not outlive any string passed in the constuctor.

#### Member functions

void update(const string_view_type& sv)
void update(const char* data, std::size_t length)
void update(const string_view_type& sv)
void update(const CharT* data, std::size_t length) (deprecated in 0.179.0)
Update the parser with a chunk of JSON

void set_buffer(const CharT* data, std::size_t length) final (since 0.179.0)
Initializes the buffer to parse from with a chunk of JSON text

bool done() const
Returns `true` when the parser has consumed a complete JSON text, `false` otherwise

Expand Down Expand Up @@ -247,6 +230,19 @@ int main()
Output:

```
(1) done: false, source_exhausted: true
(2) done: false, source_exhausted: true
(3) done: false, source_exhausted: true
(4) done: false, source_exhausted: true
(5) done: true, source_exhausted: true
(6) done: true, source_exhausted: true
(7) [false,90]
```

#### Incremental parsing (since 0.179.0)
Expand All @@ -264,7 +260,7 @@ int main()
{
if (index < chunks.size())
{
input.update(chunks[index].data(), chunks[index].size());
input.set_buffer(chunks[index].data(), chunks[index].size());
++index;
return true;
}
Expand Down
44 changes: 22 additions & 22 deletions doc/ref/corelib/basic_json_reader.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,48 +39,48 @@ string_view_type |
#### Constructors
template <typename Sourceable>
explicit basic_json_reader(Sourceable&& source,
const TempAllocator& alloc = TempAllocator()); (1)
explicit basic_json_reader(Sourceable&& source, (1)
const TempAllocator& alloc = TempAllocator());
template <typename Sourceable>
basic_json_reader(Sourceable&& source,
const basic_json_options<CharT>& options,
const TempAllocator& alloc = TempAllocator()); (2)
const basic_json_options<CharT>& options,
const TempAllocator& alloc = TempAllocator()); (2)
template <typename Sourceable>
basic_json_reader(Sourceable&& source,
std::function<bool(json_errc,const ser_context&)> err_handler,
const TempAllocator& alloc = TempAllocator()); (3)
std::function<bool(json_errc,const ser_context&)> err_handler, (3) (deprecated in 0.171.0)
const TempAllocator& alloc = TempAllocator());
template <typename Sourceable>
basic_json_reader(Sourceable&& source,
const basic_json_options<CharT>& options,
std::function<bool(json_errc,const ser_context&)> err_handler,
const TempAllocator& alloc = TempAllocator()); (4)
const basic_json_options<CharT>& options, (4) (deprecated in 0.171.0)
std::function<bool(json_errc,const ser_context&)> err_handler,
const TempAllocator& alloc = TempAllocator());
template <typename Sourceable>
basic_json_reader(Sourceable&& source,
basic_json_visitor<CharT>& visitor,
const TempAllocator& alloc = TempAllocator()); (5)
basic_json_reader(Sourceable&& source, (5)
basic_json_visitor<CharT>& visitor,
const TempAllocator& alloc = TempAllocator());
template <typename Sourceable>
basic_json_reader(Sourceable&& source,
basic_json_visitor<CharT>& visitor,
const basic_json_options<CharT>& options,
const TempAllocator& alloc = TempAllocator()); (6)
basic_json_visitor<CharT>& visitor, (6)
const basic_json_options<CharT>& options,
const TempAllocator& alloc = TempAllocator());
template <typename Sourceable>
basic_json_reader(Sourceable&& source,
basic_json_visitor<CharT>& visitor,
std::function<bool(json_errc,const ser_context&)> err_handler,
const TempAllocator& alloc = TempAllocator()); (7)
basic_json_visitor<CharT>& visitor, (7) (deprecated in 0.171.0)
std::function<bool(json_errc,const ser_context&)> err_handler,
const TempAllocator& alloc = TempAllocator());
template <typename Sourceable>
basic_json_reader(Sourceable&& source,
basic_json_visitor<CharT>& visitor,
const basic_json_options<CharT>& options,
std::function<bool(json_errc,const ser_context&)> err_handler,
const TempAllocator& alloc = TempAllocator()); (8)
basic_json_visitor<CharT>& visitor, (8) (deprecated in 0.171.0)
const basic_json_options<CharT>& options,
std::function<bool(json_errc,const ser_context&)> err_handler,
const TempAllocator& alloc = TempAllocator());
Constructors (1)-(4) use a default [basic_json_visitor](basic_json_visitor.md) that discards the JSON parse events, and are for validation only.
Expand Down
11 changes: 11 additions & 0 deletions doc/ref/corelib/basic_parser_input.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
### jsoncons::basic_parser_input

```cpp
#include <jsoncons/source.hpp>

template <typename CharT>
class basic_parser_input;
```

virtual void set_buffer(const CharT* data, std::size_t length) = 0;

5 changes: 4 additions & 1 deletion doc/ref/deprecated.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@ Category/class|Old name|Replacement
__corelib__|&nbsp;|&nbsp;
`basic_json_parser`|&nbsp;|&nbsp;
&nbsp;|`basic_json_parser(std::function<bool(json_errc,const ser_context&)>, const TempAllocator&`|Set error handler in options
&nbsp;|`basic_json_parser(const basic_json_decode_options<char_type>&,std::function<bool(json_errc,const ser_context&)>,`const TempAllocator&`|Set error handler in options
&nbsp;|`basic_json_parser(const basic_json_decode_options<char_type>&,std::function<bool(json_errc,const ser_context&)>, const TempAllocator&`|Set error handler in options
&nbsp;|`void update(string_view_type)`|Use `set_buffer` once or provide a chunk reader
&nbsp;|`void update(const char_type*, std::size_t)`|Use `set_buffer` once or provide a chunk reader
`basic_json_reader`|&nbsp;|&nbsp;



8 changes: 6 additions & 2 deletions examples/src/json_parser_examples.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ void incremental_parsing_example()
{
if (index < chunks.size())
{
input.update(chunks[index].data(), chunks[index].size());
input.set_buffer(chunks[index].data(), chunks[index].size());
++index;
return true;
}
Expand Down Expand Up @@ -106,7 +106,11 @@ void parse_nan_replacement_example()
jsoncons::json_parser parser(options);
try
{
parser.update(s);
#if JSONCONS_VERSION_MAJOR == 0 && JSONCONS_VERSION_MINOR < 179
parser.update(s); // until 0.179.0
#else
parser.set_buffer(s.data(), s.size()); // since 0.179.0
#endif
parser.parse_some(decoder);
parser.finish_parse(decoder);
parser.check_done();
Expand Down
6 changes: 3 additions & 3 deletions include/jsoncons/basic_json.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -1800,7 +1800,7 @@ namespace jsoncons {
JSONCONS_THROW(ser_error(json_errc::illegal_unicode_character,parser.line(),parser.column()));
}
std::size_t offset = (r.ptr - source.data());
parser.update(source.data()+offset,source.size()-offset);
parser.set_buffer(source.data()+offset,source.size()-offset);
parser.parse_some(decoder);
parser.finish_parse(decoder);
parser.check_done();
Expand All @@ -1826,7 +1826,7 @@ namespace jsoncons {
JSONCONS_THROW(ser_error(json_errc::illegal_unicode_character,parser.line(),parser.column()));
}
std::size_t offset = (r.ptr - source.data());
parser.update(source.data()+offset,source.size()-offset);
parser.set_buffer(source.data()+offset,source.size()-offset);
parser.parse_some(decoder);
parser.finish_parse(decoder);
parser.check_done();
Expand Down Expand Up @@ -1997,7 +1997,7 @@ namespace jsoncons {
JSONCONS_THROW(ser_error(json_errc::illegal_unicode_character,parser.line(),parser.column()));
}
std::size_t offset = (r.ptr - source.data());
parser.update(source.data()+offset,source.size()-offset);
parser.set_buffer(source.data()+offset,source.size()-offset);
parser.parse_some(decoder);
parser.finish_parse(decoder);
parser.check_done();
Expand Down
15 changes: 13 additions & 2 deletions include/jsoncons/json_parser.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -559,12 +559,23 @@ class basic_json_parser : public ser_context, public virtual basic_parser_input<
}
}

void update(const string_view_type sv)
#if !defined(JSONCONS_NO_DEPRECATED)
JSONCONS_DEPRECATED_MSG("Instead, use set_buffer once or provide a chunk reader")
void update(string_view_type sv)
{
update(sv.data(),sv.length());
}

void update(const char_type* data, std::size_t length) final
JSONCONS_DEPRECATED_MSG("Instead, use set_buffer once or provide a chunk reader")
void update(const char_type* data, std::size_t length)
{
begin_input_ = data;
input_end_ = data + length;
input_ptr_ = begin_input_;
}
#endif

void set_buffer(const char_type* data, std::size_t length) final
{
begin_input_ = data;
input_end_ = data + length;
Expand Down
57 changes: 29 additions & 28 deletions include/jsoncons/json_reader.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ namespace jsoncons {
private:
basic_default_json_visitor<CharT> default_visitor_;
basic_json_visitor<CharT>& other_visitor_;
//std::function<bool(json_errc,const ser_context&)> err_handler_;

// noncopyable and nonmoveable
json_utf8_to_other_visitor_adaptor(const json_utf8_to_other_visitor_adaptor<CharT>&) = delete;
Expand All @@ -44,10 +43,8 @@ namespace jsoncons {
{
}

json_utf8_to_other_visitor_adaptor(basic_json_visitor<CharT>& other_visitor/*,
std::function<bool(json_errc,const ser_context&)> err_handler*/)
: other_visitor_(other_visitor)/*,
err_handler_(err_handler)*/
json_utf8_to_other_visitor_adaptor(basic_json_visitor<CharT>& other_visitor)
: other_visitor_(other_visitor)
{
}

Expand Down Expand Up @@ -194,56 +191,58 @@ namespace jsoncons {
}

template <typename Sourceable>
basic_json_reader(Sourceable&& source,
std::function<bool(json_errc,const ser_context&)> err_handler,
basic_json_reader(Sourceable&& source,
basic_json_visitor<CharT>& visitor,
const TempAllocator& temp_alloc = TempAllocator())
: basic_json_reader(std::forward<Sourceable>(source),
default_visitor_,
visitor,
basic_json_decode_options<CharT>(),
err_handler,
temp_alloc)
{
}

template <typename Sourceable>
basic_json_reader(Sourceable&& source,
const basic_json_decode_options<CharT>& options,
std::function<bool(json_errc,const ser_context&)> err_handler,
basic_json_visitor<CharT>& visitor,
const basic_json_decode_options<CharT>& options,
const TempAllocator& temp_alloc = TempAllocator())
: basic_json_reader(std::forward<Sourceable>(source),
default_visitor_,
options,
err_handler,
temp_alloc)
: source_(std::forward<Sourceable>(source)),
visitor_(visitor),
parser_(this, options, options.err_handler(), temp_alloc),
eof_(false)
{
}

#if !defined(JSONCONS_NO_DEPRECATED)
template <typename Sourceable>
basic_json_reader(Sourceable&& source,
basic_json_visitor<CharT>& visitor,
JSONCONS_DEPRECATED_MSG("Instead, set err_handler in options")
basic_json_reader(Sourceable&& source,
std::function<bool(json_errc,const ser_context&)> err_handler,
const TempAllocator& temp_alloc = TempAllocator())
: basic_json_reader(std::forward<Sourceable>(source),
visitor,
default_visitor_,
basic_json_decode_options<CharT>(),
default_json_parsing(),
err_handler,
temp_alloc)
{
}

template <typename Sourceable>
JSONCONS_DEPRECATED_MSG("Instead, set err_handler in options")
basic_json_reader(Sourceable&& source,
basic_json_visitor<CharT>& visitor,
const basic_json_decode_options<CharT>& options,
const basic_json_decode_options<CharT>& options,
std::function<bool(json_errc,const ser_context&)> err_handler,
const TempAllocator& temp_alloc = TempAllocator())
: basic_json_reader(std::forward<Sourceable>(source),
visitor,
default_visitor_,
options,
options.err_handler(),
err_handler,
temp_alloc)
{
}

template <typename Sourceable>
JSONCONS_DEPRECATED_MSG("Instead, set err_handler in options")
basic_json_reader(Sourceable&& source,
basic_json_visitor<CharT>& visitor,
std::function<bool(json_errc,const ser_context&)> err_handler,
Expand All @@ -257,6 +256,7 @@ namespace jsoncons {
}

template <typename Sourceable>
JSONCONS_DEPRECATED_MSG("Instead, set err_handler in options")
basic_json_reader(Sourceable&& source,
basic_json_visitor<CharT>& visitor,
const basic_json_decode_options<CharT>& options,
Expand All @@ -268,6 +268,7 @@ namespace jsoncons {
eof_(false)
{
}
#endif

bool read_chunk(basic_parser_input<char_type>&, std::error_code& ec) final
{
Expand All @@ -277,7 +278,7 @@ namespace jsoncons {
if (ec) return false;
if (s.size() > 0)
{
parser_.update(s.data(),s.size());
parser_.set_buffer(s.data(),s.size());
success = true;
}
else
Expand Down Expand Up @@ -310,7 +311,7 @@ namespace jsoncons {
if (ec) return;
if (s.size() > 0)
{
parser_.update(s.data(),s.size());
parser_.set_buffer(s.data(),s.size());
}
parser_.parse_some(visitor_, ec);
if (ec) return;
Expand All @@ -329,7 +330,7 @@ namespace jsoncons {
if (ec) return;
if (s1.size() > 0)
{
parser_.update(s1.data(),s1.size());
parser_.set_buffer(s1.data(),s1.size());
}
}
else
Expand Down Expand Up @@ -381,7 +382,7 @@ namespace jsoncons {
if (ec) return;
if (s.size() > 0)
{
parser_.update(s.data(),s.size());
parser_.set_buffer(s.data(),s.size());
}
}
if (!parser_.source_exhausted())
Expand Down
Loading

0 comments on commit 5bb97b9

Please sign in to comment.