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>
// jsoncpp
#include <json/json.h>
#include <valijson/adapters/jsoncpp_adapter.hpp>
#include <valijson/utils/jsoncpp_utils.hpp>
// RapidJSON
#include <rapidjson/document.h>
#include <valijson/adapters/rapidjson_adapter.hpp>
#include <valijson/utils/rapidjson_utils.hpp>

View File

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

View File

@ -5,12 +5,9 @@
*
*/
#include <fstream>
#include <iostream>
#include <stdexcept>
#include <rapidjson/document.h>
#include <valijson/adapters/rapidjson_adapter.hpp>
#include <valijson/utils/rapidjson_utils.hpp>
#include <valijson/schema.hpp>

View File

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

View File

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

View File

@ -12,8 +12,6 @@
#include <curlpp/cURLpp.hpp>
#include <curlpp/Options.hpp>
#include <rapidjson/document.h>
#include <valijson/adapters/rapidjson_adapter.hpp>
#include <valijson/utils/rapidjson_utils.hpp>
#include <valijson/schema.hpp>

View File

@ -43,6 +43,24 @@
#include <string>
#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 <valijson/adapters/adapter.hpp>

View File

@ -1,6 +1,7 @@
#pragma once
#include <iostream>
#include <stdexcept>
#include <rapidjson/document.h>
@ -20,6 +21,9 @@ inline bool loadDocument(const std::string &path, rapidjson::GenericDocument<Enc
}
// Parse schema
#if VALIJSON_USE_EXCEPTIONS
try {
#endif
document.template Parse<rapidjson::kParseIterativeFlag>(file.c_str());
if (document.HasParseError()) {
std::cerr << "RapidJson failed to parse the document:" << std::endl;
@ -27,6 +31,13 @@ inline bool loadDocument(const std::string &path, rapidjson::GenericDocument<Enc
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 << "Runtime error: " << e.what() << std::endl;
return false;
}
#endif
return true;
}