Skip to content

Commit

Permalink
use an enum to select strategy on comment parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
capitalaslash committed Dec 1, 2015
1 parent f9833b1 commit 49a6197
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 18 deletions.
12 changes: 6 additions & 6 deletions json11.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -338,7 +338,7 @@ struct JsonParser {
size_t i;
string &err;
bool failed;
bool detect_comments;
JsonParse strategy;

/* fail(msg, err_ret = Json())
*
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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
Expand All @@ -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) {
Expand Down
17 changes: 11 additions & 6 deletions json11.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,10 @@

namespace json11 {

enum JsonParse {
STANDARD, COMMENTS
};

class JsonValue;

class Json final {
Expand Down Expand Up @@ -147,21 +151,22 @@ class Json final {
// 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;
Expand Down
12 changes: 6 additions & 6 deletions test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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 {
Expand All @@ -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 {
Expand All @@ -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 {
Expand All @@ -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 {
Expand All @@ -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 {
Expand Down

0 comments on commit 49a6197

Please sign in to comment.