mirror of
https://github.com/tristanpenman/valijson.git
synced 2024-12-13 10:32:58 +01:00
Use custom RAPIDJSON_ASSERT macro to catch parser errors
This commit is contained in:
parent
7ab96207c5
commit
af2358b63f
@ -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>
|
||||
|
||||
@ -74,7 +72,7 @@ void usingRapidJson(const char *filename)
|
||||
|
||||
// We support the old way of doing things...
|
||||
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) {
|
||||
cout << " " << index++ << ": ";
|
||||
|
||||
@ -83,11 +81,11 @@ void usingRapidJson(const char *filename)
|
||||
|
||||
// maybeString is a loose type check
|
||||
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
|
||||
cout << value.asString();
|
||||
}
|
||||
|
||||
|
||||
cout << endl;
|
||||
}
|
||||
}
|
||||
|
@ -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>
|
||||
|
@ -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>
|
||||
@ -68,13 +65,13 @@ int main(int argc, char *argv[])
|
||||
ValidationResults::Error error;
|
||||
unsigned int errorNum = 1;
|
||||
while (results.popError(error)) {
|
||||
|
||||
|
||||
std::string context;
|
||||
std::vector<std::string>::iterator itr = error.context.begin();
|
||||
for (; itr != error.context.end(); itr++) {
|
||||
context += *itr;
|
||||
}
|
||||
|
||||
|
||||
cerr << "Error #" << errorNum << std::endl
|
||||
<< " context: " << context << endl
|
||||
<< " desc: " << error.description << endl;
|
||||
|
@ -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>
|
||||
|
@ -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>
|
||||
|
@ -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>
|
||||
@ -88,13 +86,13 @@ int main(int argc, char *argv[])
|
||||
ValidationResults::Error error;
|
||||
unsigned int errorNum = 1;
|
||||
while (results.popError(error)) {
|
||||
|
||||
|
||||
std::string context;
|
||||
std::vector<std::string>::iterator itr = error.context.begin();
|
||||
for (; itr != error.context.end(); itr++) {
|
||||
context += *itr;
|
||||
}
|
||||
|
||||
|
||||
cerr << "Error #" << errorNum << std::endl
|
||||
<< " context: " << context << endl
|
||||
<< " desc: " << error.description << endl;
|
||||
|
@ -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>
|
||||
|
@ -1,6 +1,7 @@
|
||||
#pragma once
|
||||
|
||||
#include <iostream>
|
||||
#include <stdexcept>
|
||||
|
||||
#include <rapidjson/document.h>
|
||||
|
||||
@ -20,13 +21,23 @@ inline bool loadDocument(const std::string &path, rapidjson::GenericDocument<Enc
|
||||
}
|
||||
|
||||
// Parse schema
|
||||
document.template Parse<rapidjson::kParseIterativeFlag>(file.c_str());
|
||||
if (document.HasParseError()) {
|
||||
#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;
|
||||
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 << "Parse error: " << document.GetParseError() << std::endl;
|
||||
std::cerr << "Near: " << file.substr((std::max)(size_t(0), document.GetErrorOffset() - 20), 40) << std::endl;
|
||||
std::cerr << "Runtime error: " << e.what() << std::endl;
|
||||
return false;
|
||||
}
|
||||
#endif
|
||||
|
||||
return true;
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user