From 5c1a23c74bb0acc1e86354eecf8fc5e0465830dd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rimas=20Misevi=C4=8Dius?= Date: Tue, 4 Apr 2017 22:03:23 +0300 Subject: [PATCH 1/2] Fix example to not report syntax error on valid JSON input Adds parse_array_stop(size_t) override which returns true to the root_context class in the streaming.cc example. If not overriden, picojson::deny_parse_context::parse_array_stop(size_t) returns false what causes syntax error. Related issue: https://github.com/kazuho/picojson/issues/82 --- examples/streaming.cc | 3 +++ 1 file changed, 3 insertions(+) diff --git a/examples/streaming.cc b/examples/streaming.cc index bca0209..1baf75a 100644 --- a/examples/streaming.cc +++ b/examples/streaming.cc @@ -39,6 +39,9 @@ class root_context : public picojson::deny_parse_context { bool parse_array_start() { return true; // only allow array as root } + bool parse_array_stop(size_t) { + return true; + } template bool parse_array_item(picojson::input &in, size_t) { picojson::value item; // parse the array item From 05a0d668b4256abdccbb7d9f1bf6bf0954c49e9e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rimas=20Misevi=C4=8Dius?= Date: Mon, 10 Apr 2017 11:38:58 +0300 Subject: [PATCH 2/2] Use more appropriate istreambuf_iterator in the streaming.cc example Used in the example std::istream_iterator skips whitespace and thus a parser can produce unexpected reult, e.g.: Input: [{"x":"test space","y":20}] Output: "testspace",20 std::istreambuf_iterator does not skip whitespace, so is more appropriate: Output: "test space",20 More info: http://en.cppreference.com/w/cpp/iterator/istream_iterator (see "Notes" section). --- examples/streaming.cc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/streaming.cc b/examples/streaming.cc index 1baf75a..52a7db2 100644 --- a/examples/streaming.cc +++ b/examples/streaming.cc @@ -64,7 +64,7 @@ int main(void) { root_context ctx; std::string err; - picojson::_parse(ctx, std::istream_iterator(std::cin), std::istream_iterator(), &err); + picojson::_parse(ctx, std::istreambuf_iterator(std::cin), std::istreambuf_iterator(), &err); if (!err.empty()) { std::cerr << err << std::endl;