From 49a6197d08fbdd5a2eb9b0e8ae3aeab88f2a6d29 Mon Sep 17 00:00:00 2001 From: Antonio Cervone Date: Tue, 1 Dec 2015 11:08:37 +0100 Subject: [PATCH] use an enum to select strategy on comment parsing --- json11.cpp | 12 ++++++------ json11.hpp | 17 +++++++++++------ test.cpp | 12 ++++++------ 3 files changed, 23 insertions(+), 18 deletions(-) diff --git a/json11.cpp b/json11.cpp index 3ea9a67..07e43c0 100644 --- a/json11.cpp +++ b/json11.cpp @@ -338,7 +338,7 @@ struct JsonParser { size_t i; string &err; bool failed; - bool detect_comments; + JsonParse strategy; /* fail(msg, err_ret = Json()) * @@ -416,7 +416,7 @@ struct JsonParser { */ void consume_garbage() { consume_whitespace(); - if(detect_comments) { + if(strategy == JsonParse::COMMENTS) { bool comment_found = false; do { comment_found = consume_comment(); @@ -719,8 +719,8 @@ struct JsonParser { } }; -Json Json::parse(const string &in, string &err, bool detect_comments) { - JsonParser parser { in, 0, err, false, detect_comments }; +Json Json::parse(const string &in, string &err, JsonParse strategy) { + JsonParser parser { in, 0, err, false, strategy }; Json result = parser.parse_json(0); // Check for any trailing garbage @@ -734,8 +734,8 @@ Json Json::parse(const string &in, string &err, bool detect_comments) { // Documented in json11.hpp vector Json::parse_multi(const string &in, string &err, - bool detect_comments) { - JsonParser parser { in, 0, err, false, detect_comments }; + JsonParse strategy) { + JsonParser parser { in, 0, err, false, strategy }; vector json_vec; while (parser.i != in.size() && !parser.failed) { diff --git a/json11.hpp b/json11.hpp index 582c43f..a99e241 100644 --- a/json11.hpp +++ b/json11.hpp @@ -58,6 +58,10 @@ namespace json11 { +enum JsonParse { + STANDARD, COMMENTS +}; + class JsonValue; class Json final { @@ -147,21 +151,22 @@ public: // Parse. If parse fails, return Json() and assign an error message to err. static Json parse(const std::string & in, std::string & err, - bool detect_comments = false); + JsonParse strategy = JsonParse::STANDARD); static Json parse(const char * in, std::string & err, - bool detect_comments = false) { + JsonParse strategy = JsonParse::STANDARD) { if (in) { - return parse(std::string(in), err, detect_comments); + return parse(std::string(in), err, strategy); } else { err = "null input"; return nullptr; } } // Parse multiple objects, concatenated or separated by whitespace - static std::vector parse_multi(const std::string & in, - std::string & err, - bool detect_comments = false); + static std::vector parse_multi( + const std::string & in, + std::string & err, + JsonParse strategy = JsonParse::STANDARD); bool operator== (const Json &rhs) const; bool operator< (const Json &rhs) const; diff --git a/test.cpp b/test.cpp index ece0e04..bd60705 100644 --- a/test.cpp +++ b/test.cpp @@ -73,7 +73,7 @@ int main(int argc, char **argv) { string err_comment; auto json_comment = Json::parse( - comment_test, err_comment, /*detect_comments=*/ true); + comment_test, err_comment, JsonParse::COMMENTS); if (!err_comment.empty()) { printf("Failed: %s\n", err_comment.c_str()); } else { @@ -87,7 +87,7 @@ int main(int argc, char **argv) { string err_failing_comment; auto json_failing_comment = Json::parse( - failing_comment_test, err_failing_comment, /*detect_comments=*/ true); + failing_comment_test, err_failing_comment, JsonParse::COMMENTS); if (!err_failing_comment.empty()) { printf("Failed: %s\n", err_failing_comment.c_str()); } else { @@ -98,7 +98,7 @@ int main(int argc, char **argv) { / / bad comment })"; json_failing_comment = Json::parse( - failing_comment_test, err_failing_comment, /*detect_comments=*/ true); + failing_comment_test, err_failing_comment, JsonParse::COMMENTS); if (!err_failing_comment.empty()) { printf("Failed: %s\n", err_failing_comment.c_str()); } else { @@ -108,7 +108,7 @@ int main(int argc, char **argv) { failing_comment_test = R"({// bad comment })"; json_failing_comment = Json::parse( - failing_comment_test, err_failing_comment, /*detect_comments=*/ true); + failing_comment_test, err_failing_comment, JsonParse::COMMENTS); if (!err_failing_comment.empty()) { printf("Failed: %s\n", err_failing_comment.c_str()); } else { @@ -120,7 +120,7 @@ int main(int argc, char **argv) { }/)"; json_failing_comment = Json::parse( - failing_comment_test, err_failing_comment, /*detect_comments=*/ true); + failing_comment_test, err_failing_comment, JsonParse::COMMENTS); if (!err_failing_comment.empty()) { printf("Failed: %s\n", err_failing_comment.c_str()); } else { @@ -131,7 +131,7 @@ int main(int argc, char **argv) { comment *})"; json_failing_comment = Json::parse( - failing_comment_test, err_failing_comment, /*detect_comments=*/ true); + failing_comment_test, err_failing_comment, JsonParse::COMMENTS); if (!err_failing_comment.empty()) { printf("Failed: %s\n", err_failing_comment.c_str()); } else {