5 #include <boost/range/adaptor/reversed.hpp> 10 void mergeJsonIntoConservative(nlohmann::json& into,
const nlohmann::json& from) {
11 for (
const auto& j : nlohmann::json::iterator_wrapper(from)) {
12 if (into.find(j.key()) == into.end()) { into[j.key()] = j.value(); }
16 std::string prettyPrintJson(
const nlohmann::json& j,
int indentLevel) {
17 std::string indentString(indentLevel * 2,
' ');
21 ret += indentString +
"[\n";
23 for (
const auto& elem : j) {
24 ret += prettyPrintJson(elem, indentLevel + 1);
27 ret += indentString +
"]";
29 }
else if (j.is_string()) {
33 for (
auto idx = str.find(
'\n'); idx < str.length(); idx = str.find(
'\n', idx + 1)) {
34 str.insert(idx + 1, indentString);
36 ret += indentString + str;
37 }
else if (j.is_number()) {
38 ret += indentString + j.dump();
39 }
else if (j.is_object()) {
40 for (
auto iter = j.begin(); iter != j.end(); ++iter) {
41 ret += indentString + iter.key() +
"\n";
42 ret += prettyPrintJson(iter.value(), indentLevel + 1) +
"\n";
53 assert((ec[0] ==
'E' || ec[0] ==
'I' || ec[0] ==
'W') &&
54 "error code passed to addEntry must start with E, I , or W");
55 assert((data.is_object() || data.is_null()) &&
56 "data passed to addEntry must be a json object or {}");
61 nlohmann::json({{
"errorcode", ec}, {
"overview", overview}, {
"data", data}}));
69 if (error.find(
"errorcode") == error.end() || !error[
"errorcode"].is_string() ||
70 error.find(
"overview") == error.end() || !error[
"overview"].is_string()) {
73 std::string ec = error[
"errorcode"];
74 std::string desc = error[
"overview"];
75 ret += ec +
": " + desc +
"\n";
77 auto& data = error[
"data"];
78 ret += prettyPrintJson(data, 1);
85 assert(data.is_object() &&
"Json added to context must be an object");
89 mContexts.emplace(ctxId, data);
97 auto merged = nlohmann::json::object();
99 for (
const auto& ctx : mContexts | boost::adaptors::reversed) {
100 mergeJsonIntoConservative(merged, ctx.second);
112 std::back_inserter(ret.
result_json), [&](nlohmann::json j) {
114 mergeJsonIntoConservative(j[
"data"], rhs.
contextJson());
118 std::back_inserter(ret.
result_json), [&](nlohmann::json j) {
120 mergeJsonIntoConservative(j[
"data"], lhs.
contextJson());
132 mergeJsonIntoConservative(entry[
"data"], rhs.
contextJson());
137 std::back_inserter(lhs.
result_json), [&](nlohmann::json j) {
138 mergeJsonIntoConservative(j[
"data"], lhs.
contextJson());
std::string dump() const
Dump to a pretty-printed error message.
void addEntry(const char *ec, const char *overview, nlohmann::json data)
Add a entry to the result, either a warning or an error.
nlohmann::json contextJson() const
Get the JSON associated with the context that's been added.
bool mSuccess
If it's successful.
nlohmann::json result_json
The result JSON.
Defines the Result class and related functions.
Result operator+(const Result &lhs, const Result &rhs)
Append two Result objects.
int addContext(const nlohmann::json &data)
Add some json that will ALWAYS be merged with entries that are added and when results are added to th...
bool success() const
Success test.
Result & operator+=(Result &lhs, const Result &rhs)
Append one result to an existing one.
The namespace where chigraph lives.
The result object, used for identifiying errors with good diagnostics.
void removeContext(int id)
Removes a previously added context.