diff --git a/json11.cpp b/json11.cpp index 4c473c1..4d239c0 100644 --- a/json11.cpp +++ b/json11.cpp @@ -338,6 +338,7 @@ struct JsonParser { size_t i; string &err; bool failed; + bool detect_comments; /* fail(msg, err_ret = Json()) * @@ -397,10 +398,10 @@ struct JsonParser { */ void consume_garbage() { consume_whitespace(); -#ifdef JSON11_COMMENTS - consume_comment(); - consume_whitespace(); -#endif + if(detect_comments) { + consume_comment(); + consume_whitespace(); + } } /* get_next_token() @@ -696,8 +697,8 @@ struct JsonParser { } }; -Json Json::parse(const string &in, string &err) { - JsonParser parser { in, 0, err, false }; +Json Json::parse(const string &in, string &err, bool detect_comments) { + JsonParser parser { in, 0, err, false, detect_comments }; Json result = parser.parse_json(0); // Check for any trailing garbage @@ -709,8 +710,10 @@ Json Json::parse(const string &in, string &err) { } // Documented in json11.hpp -vector Json::parse_multi(const string &in, string &err) { - JsonParser parser { in, 0, err, false }; +vector Json::parse_multi(const string &in, + string &err, + bool detect_comments) { + JsonParser parser { in, 0, err, false, detect_comments }; vector json_vec; while (parser.i != in.size() && !parser.failed) { diff --git a/json11.hpp b/json11.hpp index ff399d1..582c43f 100644 --- a/json11.hpp +++ b/json11.hpp @@ -56,8 +56,6 @@ #include #include -#define JSON11_COMMENTS 1 - namespace json11 { class JsonValue; @@ -147,17 +145,23 @@ public: } // Parse. If parse fails, return Json() and assign an error message to err. - static Json parse(const std::string & in, std::string & err); - static Json parse(const char * in, std::string & err) { + static Json parse(const std::string & in, + std::string & err, + bool detect_comments = false); + static Json parse(const char * in, + std::string & err, + bool detect_comments = false) { if (in) { - return parse(std::string(in), err); + return parse(std::string(in), err, detect_comments); } 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); + static std::vector parse_multi(const std::string & in, + std::string & err, + bool detect_comments = false); bool operator== (const Json &rhs) const; bool operator< (const Json &rhs) const; diff --git a/test.cpp b/test.cpp index 7a40857..26fd5b8 100644 --- a/test.cpp +++ b/test.cpp @@ -58,7 +58,6 @@ int main(int argc, char **argv) { std::cout << " - " << k.dump() << "\n"; } -#ifdef JSON11_COMMENTS const string comment_test = R"({ // comment "a": 1, @@ -72,13 +71,13 @@ int main(int argc, char **argv) { })"; string err_comment; - auto json_comment = Json::parse(comment_test, err_comment); + auto json_comment = Json::parse( + comment_test, err_comment, /*detect_comments=*/ true); if (!err_comment.empty()) { printf("Failed: %s\n", err_comment.c_str()); } else { printf("Result: %s\n", json_comment.dump().c_str()); } -#endif std::list l1 { 1, 2, 3 }; std::vector l2 { 1, 2, 3 };