From 08c391f89ad253eafb21fcad6b430a8c4df70c15 Mon Sep 17 00:00:00 2001 From: Antonio Cervone Date: Fri, 27 Nov 2015 16:41:05 +0100 Subject: [PATCH] introduce consume_garbage() new routine that consumes whitespaces and comments. activated by JSON11_COMMENTS pre-processor flag. --- json11.cpp | 18 +++++++++++++++--- json11.hpp | 2 ++ 2 files changed, 17 insertions(+), 3 deletions(-) diff --git a/json11.cpp b/json11.cpp index e2085a1..4c473c1 100644 --- a/json11.cpp +++ b/json11.cpp @@ -391,13 +391,25 @@ struct JsonParser { } } + /* consume_garbage() + * + * Advance until the current character is non-whitespace and non-comment. + */ + void consume_garbage() { + consume_whitespace(); +#ifdef JSON11_COMMENTS + consume_comment(); + consume_whitespace(); +#endif + } + /* get_next_token() * * Return the next non-whitespace character. If the end of the input is reached, * flag an error and return 0. */ char get_next_token() { - consume_whitespace(); + consume_garbage(); if (i == str.size()) return fail("unexpected end of input", 0); @@ -689,7 +701,7 @@ Json Json::parse(const string &in, string &err) { Json result = parser.parse_json(0); // Check for any trailing garbage - parser.consume_whitespace(); + parser.consume_garbage(); if (parser.i != in.size()) return parser.fail("unexpected trailing " + esc(in[parser.i])); @@ -704,7 +716,7 @@ vector Json::parse_multi(const string &in, string &err) { while (parser.i != in.size() && !parser.failed) { json_vec.push_back(parser.parse_json(0)); // Check for another object - parser.consume_whitespace(); + parser.consume_garbage(); } return json_vec; } diff --git a/json11.hpp b/json11.hpp index fe9bba4..ff399d1 100644 --- a/json11.hpp +++ b/json11.hpp @@ -56,6 +56,8 @@ #include #include +#define JSON11_COMMENTS 1 + namespace json11 { class JsonValue;