use an enum to select strategy on comment parsing

mutable-v2
Antonio Cervone 2015-12-01 11:08:37 +01:00
parent f9833b1e7d
commit 49a6197d08
3 changed files with 23 additions and 18 deletions

View File

@ -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> 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> json_vec;
while (parser.i != in.size() && !parser.failed) {

View File

@ -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<Json> parse_multi(const std::string & in,
std::string & err,
bool detect_comments = false);
static std::vector<Json> 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;

View File

@ -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 {