use an enum to select strategy on comment parsing
parent
f9833b1e7d
commit
49a6197d08
12
json11.cpp
12
json11.cpp
|
@ -338,7 +338,7 @@ struct JsonParser {
|
||||||
size_t i;
|
size_t i;
|
||||||
string &err;
|
string &err;
|
||||||
bool failed;
|
bool failed;
|
||||||
bool detect_comments;
|
JsonParse strategy;
|
||||||
|
|
||||||
/* fail(msg, err_ret = Json())
|
/* fail(msg, err_ret = Json())
|
||||||
*
|
*
|
||||||
|
@ -416,7 +416,7 @@ struct JsonParser {
|
||||||
*/
|
*/
|
||||||
void consume_garbage() {
|
void consume_garbage() {
|
||||||
consume_whitespace();
|
consume_whitespace();
|
||||||
if(detect_comments) {
|
if(strategy == JsonParse::COMMENTS) {
|
||||||
bool comment_found = false;
|
bool comment_found = false;
|
||||||
do {
|
do {
|
||||||
comment_found = consume_comment();
|
comment_found = consume_comment();
|
||||||
|
@ -719,8 +719,8 @@ struct JsonParser {
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
Json Json::parse(const string &in, string &err, bool detect_comments) {
|
Json Json::parse(const string &in, string &err, JsonParse strategy) {
|
||||||
JsonParser parser { in, 0, err, false, detect_comments };
|
JsonParser parser { in, 0, err, false, strategy };
|
||||||
Json result = parser.parse_json(0);
|
Json result = parser.parse_json(0);
|
||||||
|
|
||||||
// Check for any trailing garbage
|
// Check for any trailing garbage
|
||||||
|
@ -734,8 +734,8 @@ Json Json::parse(const string &in, string &err, bool detect_comments) {
|
||||||
// Documented in json11.hpp
|
// Documented in json11.hpp
|
||||||
vector<Json> Json::parse_multi(const string &in,
|
vector<Json> Json::parse_multi(const string &in,
|
||||||
string &err,
|
string &err,
|
||||||
bool detect_comments) {
|
JsonParse strategy) {
|
||||||
JsonParser parser { in, 0, err, false, detect_comments };
|
JsonParser parser { in, 0, err, false, strategy };
|
||||||
|
|
||||||
vector<Json> json_vec;
|
vector<Json> json_vec;
|
||||||
while (parser.i != in.size() && !parser.failed) {
|
while (parser.i != in.size() && !parser.failed) {
|
||||||
|
|
17
json11.hpp
17
json11.hpp
|
@ -58,6 +58,10 @@
|
||||||
|
|
||||||
namespace json11 {
|
namespace json11 {
|
||||||
|
|
||||||
|
enum JsonParse {
|
||||||
|
STANDARD, COMMENTS
|
||||||
|
};
|
||||||
|
|
||||||
class JsonValue;
|
class JsonValue;
|
||||||
|
|
||||||
class Json final {
|
class Json final {
|
||||||
|
@ -147,21 +151,22 @@ public:
|
||||||
// Parse. If parse fails, return Json() and assign an error message to err.
|
// Parse. If parse fails, return Json() and assign an error message to err.
|
||||||
static Json parse(const std::string & in,
|
static Json parse(const std::string & in,
|
||||||
std::string & err,
|
std::string & err,
|
||||||
bool detect_comments = false);
|
JsonParse strategy = JsonParse::STANDARD);
|
||||||
static Json parse(const char * in,
|
static Json parse(const char * in,
|
||||||
std::string & err,
|
std::string & err,
|
||||||
bool detect_comments = false) {
|
JsonParse strategy = JsonParse::STANDARD) {
|
||||||
if (in) {
|
if (in) {
|
||||||
return parse(std::string(in), err, detect_comments);
|
return parse(std::string(in), err, strategy);
|
||||||
} else {
|
} else {
|
||||||
err = "null input";
|
err = "null input";
|
||||||
return nullptr;
|
return nullptr;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// Parse multiple objects, concatenated or separated by whitespace
|
// Parse multiple objects, concatenated or separated by whitespace
|
||||||
static std::vector<Json> parse_multi(const std::string & in,
|
static std::vector<Json> parse_multi(
|
||||||
std::string & err,
|
const std::string & in,
|
||||||
bool detect_comments = false);
|
std::string & err,
|
||||||
|
JsonParse strategy = JsonParse::STANDARD);
|
||||||
|
|
||||||
bool operator== (const Json &rhs) const;
|
bool operator== (const Json &rhs) const;
|
||||||
bool operator< (const Json &rhs) const;
|
bool operator< (const Json &rhs) const;
|
||||||
|
|
12
test.cpp
12
test.cpp
|
@ -73,7 +73,7 @@ int main(int argc, char **argv) {
|
||||||
|
|
||||||
string err_comment;
|
string err_comment;
|
||||||
auto json_comment = Json::parse(
|
auto json_comment = Json::parse(
|
||||||
comment_test, err_comment, /*detect_comments=*/ true);
|
comment_test, err_comment, JsonParse::COMMENTS);
|
||||||
if (!err_comment.empty()) {
|
if (!err_comment.empty()) {
|
||||||
printf("Failed: %s\n", err_comment.c_str());
|
printf("Failed: %s\n", err_comment.c_str());
|
||||||
} else {
|
} else {
|
||||||
|
@ -87,7 +87,7 @@ int main(int argc, char **argv) {
|
||||||
|
|
||||||
string err_failing_comment;
|
string err_failing_comment;
|
||||||
auto json_failing_comment = Json::parse(
|
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()) {
|
if (!err_failing_comment.empty()) {
|
||||||
printf("Failed: %s\n", err_failing_comment.c_str());
|
printf("Failed: %s\n", err_failing_comment.c_str());
|
||||||
} else {
|
} else {
|
||||||
|
@ -98,7 +98,7 @@ int main(int argc, char **argv) {
|
||||||
/ / bad comment })";
|
/ / bad comment })";
|
||||||
|
|
||||||
json_failing_comment = Json::parse(
|
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()) {
|
if (!err_failing_comment.empty()) {
|
||||||
printf("Failed: %s\n", err_failing_comment.c_str());
|
printf("Failed: %s\n", err_failing_comment.c_str());
|
||||||
} else {
|
} else {
|
||||||
|
@ -108,7 +108,7 @@ int main(int argc, char **argv) {
|
||||||
failing_comment_test = R"({// bad comment })";
|
failing_comment_test = R"({// bad comment })";
|
||||||
|
|
||||||
json_failing_comment = Json::parse(
|
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()) {
|
if (!err_failing_comment.empty()) {
|
||||||
printf("Failed: %s\n", err_failing_comment.c_str());
|
printf("Failed: %s\n", err_failing_comment.c_str());
|
||||||
} else {
|
} else {
|
||||||
|
@ -120,7 +120,7 @@ int main(int argc, char **argv) {
|
||||||
}/)";
|
}/)";
|
||||||
|
|
||||||
json_failing_comment = Json::parse(
|
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()) {
|
if (!err_failing_comment.empty()) {
|
||||||
printf("Failed: %s\n", err_failing_comment.c_str());
|
printf("Failed: %s\n", err_failing_comment.c_str());
|
||||||
} else {
|
} else {
|
||||||
|
@ -131,7 +131,7 @@ int main(int argc, char **argv) {
|
||||||
comment *})";
|
comment *})";
|
||||||
|
|
||||||
json_failing_comment = Json::parse(
|
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()) {
|
if (!err_failing_comment.empty()) {
|
||||||
printf("Failed: %s\n", err_failing_comment.c_str());
|
printf("Failed: %s\n", err_failing_comment.c_str());
|
||||||
} else {
|
} else {
|
||||||
|
|
Loading…
Reference in New Issue