Use custom RAPIDJSON_ASSERT macro to catch parser errors

This commit is contained in:
Tristan Penman 2021-01-24 14:06:15 +11:00
parent 7ab96207c5
commit af2358b63f
8 changed files with 40 additions and 24 deletions

View File

@ -9,12 +9,10 @@
#include <iostream> #include <iostream>
// jsoncpp // jsoncpp
#include <json/json.h>
#include <valijson/adapters/jsoncpp_adapter.hpp> #include <valijson/adapters/jsoncpp_adapter.hpp>
#include <valijson/utils/jsoncpp_utils.hpp> #include <valijson/utils/jsoncpp_utils.hpp>
// RapidJSON // RapidJSON
#include <rapidjson/document.h>
#include <valijson/adapters/rapidjson_adapter.hpp> #include <valijson/adapters/rapidjson_adapter.hpp>
#include <valijson/utils/rapidjson_utils.hpp> #include <valijson/utils/rapidjson_utils.hpp>
@ -74,7 +72,7 @@ void usingRapidJson(const char *filename)
// We support the old way of doing things... // We support the old way of doing things...
const RapidJsonAdapter::Array array = adapter.asArray(); const RapidJsonAdapter::Array array = adapter.asArray();
for (RapidJsonAdapter::Array::const_iterator itr = array.begin(); for (RapidJsonAdapter::Array::const_iterator itr = array.begin();
itr != array.end(); ++itr) { itr != array.end(); ++itr) {
cout << " " << index++ << ": "; cout << " " << index++ << ": ";
@ -83,11 +81,11 @@ void usingRapidJson(const char *filename)
// maybeString is a loose type check // maybeString is a loose type check
if (value.maybeString()) { if (value.maybeString()) {
// If a value may be a string, we are allowed to get a string // If a value may be a string, we are allowed to get a string
// representation of the value using asString // representation of the value using asString
cout << value.asString(); cout << value.asString();
} }
cout << endl; cout << endl;
} }
} }

View File

@ -59,8 +59,6 @@
#include <iostream> #include <iostream>
#include <stdexcept> #include <stdexcept>
#include <rapidjson/document.h>
#include <valijson/adapters/rapidjson_adapter.hpp> #include <valijson/adapters/rapidjson_adapter.hpp>
#include <valijson/constraints/concrete_constraints.hpp> #include <valijson/constraints/concrete_constraints.hpp>
#include <valijson/utils/rapidjson_utils.hpp> #include <valijson/utils/rapidjson_utils.hpp>

View File

@ -5,12 +5,9 @@
* *
*/ */
#include <fstream>
#include <iostream> #include <iostream>
#include <stdexcept> #include <stdexcept>
#include <rapidjson/document.h>
#include <valijson/adapters/rapidjson_adapter.hpp> #include <valijson/adapters/rapidjson_adapter.hpp>
#include <valijson/utils/rapidjson_utils.hpp> #include <valijson/utils/rapidjson_utils.hpp>
#include <valijson/schema.hpp> #include <valijson/schema.hpp>
@ -68,13 +65,13 @@ int main(int argc, char *argv[])
ValidationResults::Error error; ValidationResults::Error error;
unsigned int errorNum = 1; unsigned int errorNum = 1;
while (results.popError(error)) { while (results.popError(error)) {
std::string context; std::string context;
std::vector<std::string>::iterator itr = error.context.begin(); std::vector<std::string>::iterator itr = error.context.begin();
for (; itr != error.context.end(); itr++) { for (; itr != error.context.end(); itr++) {
context += *itr; context += *itr;
} }
cerr << "Error #" << errorNum << std::endl cerr << "Error #" << errorNum << std::endl
<< " context: " << context << endl << " context: " << context << endl
<< " desc: " << error.description << endl; << " desc: " << error.description << endl;

View File

@ -7,8 +7,6 @@
#include <iostream> #include <iostream>
#include <rapidjson/document.h>
#include <valijson/adapters/rapidjson_adapter.hpp> #include <valijson/adapters/rapidjson_adapter.hpp>
#include <valijson/internal/json_pointer.hpp> #include <valijson/internal/json_pointer.hpp>
#include <valijson/internal/json_reference.hpp> #include <valijson/internal/json_reference.hpp>

View File

@ -9,8 +9,6 @@
#include <iostream> #include <iostream>
#include <stdexcept> #include <stdexcept>
#include <rapidjson/document.h>
#include <valijson/adapters/rapidjson_adapter.hpp> #include <valijson/adapters/rapidjson_adapter.hpp>
#include <valijson/utils/rapidjson_utils.hpp> #include <valijson/utils/rapidjson_utils.hpp>
#include <valijson/schema.hpp> #include <valijson/schema.hpp>

View File

@ -12,8 +12,6 @@
#include <curlpp/cURLpp.hpp> #include <curlpp/cURLpp.hpp>
#include <curlpp/Options.hpp> #include <curlpp/Options.hpp>
#include <rapidjson/document.h>
#include <valijson/adapters/rapidjson_adapter.hpp> #include <valijson/adapters/rapidjson_adapter.hpp>
#include <valijson/utils/rapidjson_utils.hpp> #include <valijson/utils/rapidjson_utils.hpp>
#include <valijson/schema.hpp> #include <valijson/schema.hpp>
@ -88,13 +86,13 @@ int main(int argc, char *argv[])
ValidationResults::Error error; ValidationResults::Error error;
unsigned int errorNum = 1; unsigned int errorNum = 1;
while (results.popError(error)) { while (results.popError(error)) {
std::string context; std::string context;
std::vector<std::string>::iterator itr = error.context.begin(); std::vector<std::string>::iterator itr = error.context.begin();
for (; itr != error.context.end(); itr++) { for (; itr != error.context.end(); itr++) {
context += *itr; context += *itr;
} }
cerr << "Error #" << errorNum << std::endl cerr << "Error #" << errorNum << std::endl
<< " context: " << context << endl << " context: " << context << endl
<< " desc: " << error.description << endl; << " desc: " << error.description << endl;

View File

@ -43,6 +43,24 @@
#include <string> #include <string>
#include <iterator> #include <iterator>
#ifdef VALIJSON_USE_EXCEPTIONS
#ifdef RAPIDJSON_ASSERT
#warning "RAPIDJSON_ASSERT already defined."
#warning "Please include valijson/adapters/rapidjson_adapter.hpp before any RapidJSON headers."
#else
template<typename T>
T rapidjson_assert(T t, const std::string& file, const int line) {
if (t) {
return t;
}
throw std::runtime_error("assertion failed; file: " + file + "; line: " + std::to_string(line));
}
#define RAPIDJSON_ASSERT(x) rapidjson_assert(x, __FILE__, __LINE__)
#endif
#endif
#include <rapidjson/document.h> #include <rapidjson/document.h>
#include <valijson/adapters/adapter.hpp> #include <valijson/adapters/adapter.hpp>

View File

@ -1,6 +1,7 @@
#pragma once #pragma once
#include <iostream> #include <iostream>
#include <stdexcept>
#include <rapidjson/document.h> #include <rapidjson/document.h>
@ -20,13 +21,23 @@ inline bool loadDocument(const std::string &path, rapidjson::GenericDocument<Enc
} }
// Parse schema // Parse schema
document.template Parse<rapidjson::kParseIterativeFlag>(file.c_str()); #if VALIJSON_USE_EXCEPTIONS
if (document.HasParseError()) { try {
#endif
document.template Parse<rapidjson::kParseIterativeFlag>(file.c_str());
if (document.HasParseError()) {
std::cerr << "RapidJson failed to parse the document:" << std::endl;
std::cerr << "Parse error: " << document.GetParseError() << std::endl;
std::cerr << "Near: " << file.substr((std::max)(size_t(0), document.GetErrorOffset() - 20), 40) << std::endl;
return false;
}
#if VALIJSON_USE_EXCEPTIONS
} catch (const std::runtime_error &e) {
std::cerr << "RapidJson failed to parse the document:" << std::endl; std::cerr << "RapidJson failed to parse the document:" << std::endl;
std::cerr << "Parse error: " << document.GetParseError() << std::endl; std::cerr << "Runtime error: " << e.what() << std::endl;
std::cerr << "Near: " << file.substr((std::max)(size_t(0), document.GetErrorOffset() - 20), 40) << std::endl;
return false; return false;
} }
#endif
return true; return true;
} }