Skip to content

Commit

Permalink
incremental parsing example and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
danielaparker committed Nov 11, 2024
1 parent 6ea30f2 commit b8bea86
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 57 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
0.179.0 (on master)
-------

Changes:
Changes to basic_json::operator[]:

- Removed `basic_json` proxy type. The rationale for this change is given in #315.

Expand Down
80 changes: 33 additions & 47 deletions examples/src/json_parser_examples.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@

using namespace jsoncons;

void incremental_parsing_example1()
#if JSONCONS_VERSION_MAJOR == 0 && JSONCONS_VERSION_MINOR < 179

void incremental_parsing_example()
{
jsoncons::json_decoder<json> decoder;
json_parser parser;
Expand Down Expand Up @@ -37,63 +39,48 @@ void incremental_parsing_example1()
}
}

void incremental_parsing_example2()
{
jsoncons::json_decoder<json> decoder;
json_parser parser;
try
{
parser.update("10");
parser.parse_some(decoder);
std::cout << "(1) done: " << std::boolalpha << parser.done() << ", source_exhausted: " << parser.source_exhausted() << "\n\n";

parser.update(".5");
parser.parse_some(decoder); // This is the end, but the parser can't tell
std::cout << "(2) done: " << std::boolalpha << parser.done() << ", source_exhausted: " << parser.source_exhausted() << "\n\n";
#else

parser.finish_parse(decoder); // Indicates that this is the end
std::cout << "(3) done: " << std::boolalpha << parser.done() << ", source_exhausted: " << parser.source_exhausted() << "\n\n";

parser.check_done(); // Checks if there are any unconsumed
// non-whitespace characters in the input
std::cout << "(4) done: " << std::boolalpha << parser.done() << ", source_exhausted: " << parser.source_exhausted() << "\n\n";
void incremental_parsing_example()
{
std::vector<std::string> chunks = {"[fal", "se,", "9", "0", "]"};
std::size_t index = 0;

json j = decoder.get_result();
std::cout << "(5) " << j << "\n";
}
catch (const ser_error& e)
auto read_chunk = [&](jsoncons::parser_input& input, std::error_code& /*ec*/) -> bool
{
std::cout << e.what() << std::endl;
}
}
if (index < chunks.size())
{
input.update(chunks[index].data(), chunks[index].size());
++index;
return true;
}
else
{
return false;
}
};

void incremental_parsing_example3()
{
jsoncons::json_decoder<json> decoder;
json_parser parser;
json_parser parser{read_chunk};

parser.reset();
try
{
parser.update("[10");
parser.parse_some(decoder);
std::cout << "(1) done: " << std::boolalpha << parser.done() << ", source_exhausted: " << parser.source_exhausted() << "\n\n";

parser.update(".5]{}");
parser.parse_some(decoder); // The parser reaches the end at ']'
std::cout << "(2) done: " << std::boolalpha << parser.done() << ", source_exhausted: " << parser.source_exhausted() << "\n\n";

parser.finish_parse(decoder); // Indicates that this is the end
std::cout << "(3) done: " << std::boolalpha << parser.done() << ", source_exhausted: " << parser.source_exhausted() << "\n\n";
parser.finish_parse(decoder);
parser.check_done();

parser.check_done(); // Checks if there are any unconsumed
// non-whitespace characters in the input
// (there are)
json j = decoder.get_result();
std::cout << j << "\n\n";
}
catch (const ser_error& e)
{
std::cout << "(4) " << e.what() << std::endl;
std::cout << e.what() << std::endl;
}
}

#endif

void parse_nan_replacement_example()
{
std::string s = R"(
Expand Down Expand Up @@ -140,10 +127,9 @@ void parse_nan_replacement_example()
int main()
{
std::cout << "\njson_parser examples\n\n";
incremental_parsing_example1();
incremental_parsing_example2();
incremental_parsing_example3();
parse_nan_replacement_example();

incremental_parsing_example();
//parse_nan_replacement_example();

std::cout << std::endl;
}
Expand Down
2 changes: 1 addition & 1 deletion include/jsoncons/config/version.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#include <iostream>

#define JSONCONS_VERSION_MAJOR 0
#define JSONCONS_VERSION_MINOR 178
#define JSONCONS_VERSION_MINOR 179
#define JSONCONS_VERSION_PATCH 0

namespace jsoncons {
Expand Down
11 changes: 4 additions & 7 deletions include/jsoncons/json_parser.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -606,20 +606,18 @@ class basic_json_parser : public ser_context, public virtual basic_parser_input<
more_ = false;
return;
}
const char_type* local_input_end = input_end_;

if (input_ptr_ == local_input_end && more_)
if (input_ptr_ == input_end_ && more_)
{
if (input_ptr_ == local_input_end)
if (input_ptr_ == input_end_)
{
chunk_rdr_->read_chunk(*this, ec);
if (ec)
{
return;
}
local_input_end = input_end_;
}
if (input_ptr_ == local_input_end)
if (input_ptr_ == input_end_)
{
switch (state_)
{
Expand Down Expand Up @@ -647,13 +645,12 @@ class basic_json_parser : public ser_context, public virtual basic_parser_input<

while (more_)
{
if (input_ptr_ == local_input_end)
if (input_ptr_ == input_end_)
{
if (!chunk_rdr_->read_chunk(*this, ec))
{
break;
}
local_input_end = input_end_;
}

switch (state_)
Expand Down
7 changes: 6 additions & 1 deletion test/corelib/src/json_parser_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ TEST_CASE("test_incremental_parsing")
{
SECTION("Array of strings")
{
std::vector<std::string> chunks = {"[fal", "se]"};
std::vector<std::string> chunks = {"[fal", "se, ", "t", "rue", ", null", ",", "1", "234", ",\"Hello ", "World\"", "]"};
std::size_t index = 0;

auto read_chunk = [&](jsoncons::parser_input& input, std::error_code& /*ec*/) -> bool
Expand Down Expand Up @@ -265,7 +265,12 @@ TEST_CASE("test_incremental_parsing")

json j = decoder.get_result();
REQUIRE(j.is_array());
REQUIRE(j.size() == 5);
CHECK_FALSE(j[0].as<bool>());
CHECK(j[1].as<bool>());
CHECK(j[2].is_null());
CHECK(j[3].as<int>() == 1234);
CHECK(j[4].as<std::string>() == "Hello World");
}
}

Expand Down

0 comments on commit b8bea86

Please sign in to comment.