Make raising exceptions optional for parse()
This allows users to parse json without raising exceptions but still get full exception details. This is very similar to how std::networking and std::filesystem work. In that if the overload with std::error_code& is used then no exceptions are raised. - Removed static create functions from exceptions. Replaced with normal constructor so that it can be created on heap for assignment to std::unique_ptr. - Removed bool allow_exceptions, no longer necessary with the &ex overload.
This commit is contained in:
parent
992c836b30
commit
c040438e33
466
src/json.hpp
466
src/json.hpp
File diff suppressed because it is too large
Load Diff
@ -45,7 +45,8 @@ json parser_helper(const std::string& s)
|
||||
// if this line was reached, no exception ocurred
|
||||
// -> check if result is the same without exceptions
|
||||
json j_nothrow;
|
||||
CHECK_NOTHROW(json::parser(nlohmann::detail::input_adapter(s), nullptr, false).parse(true, j_nothrow));
|
||||
std::unique_ptr<json::exception> ex;
|
||||
CHECK_NOTHROW(json::parser(nlohmann::detail::input_adapter(s), nullptr).parse(true, j_nothrow, ex));
|
||||
CHECK(j_nothrow == j);
|
||||
|
||||
return j;
|
||||
@ -55,7 +56,8 @@ bool accept_helper(const std::string& s)
|
||||
{
|
||||
// 1. parse s without exceptions
|
||||
json j;
|
||||
CHECK_NOTHROW(json::parser(nlohmann::detail::input_adapter(s), nullptr, false).parse(true, j));
|
||||
std::unique_ptr<json::exception> ex;
|
||||
CHECK_NOTHROW(json::parser(nlohmann::detail::input_adapter(s)).parse(true, j, ex));
|
||||
const bool ok_noexcept = not j.is_discarded();
|
||||
|
||||
// 2. accept s
|
||||
|
||||
@ -103,7 +103,8 @@ TEST_CASE("deserialization")
|
||||
CHECK(not json::accept(ss3));
|
||||
|
||||
json j_error;
|
||||
CHECK_NOTHROW(j_error = json::parse(ss1, nullptr, false));
|
||||
std::unique_ptr<json::exception> ex;
|
||||
CHECK_NOTHROW(j_error = json::parse(ss1, ex));
|
||||
CHECK(j_error.is_discarded());
|
||||
}
|
||||
|
||||
@ -116,7 +117,8 @@ TEST_CASE("deserialization")
|
||||
CHECK(not json::accept(s));
|
||||
|
||||
json j_error;
|
||||
CHECK_NOTHROW(j_error = json::parse(s, nullptr, false));
|
||||
std::unique_ptr<json::exception> ex;
|
||||
CHECK_NOTHROW(j_error = json::parse(s, ex));
|
||||
CHECK(j_error.is_discarded());
|
||||
}
|
||||
|
||||
@ -272,7 +274,8 @@ TEST_CASE("deserialization")
|
||||
CHECK(not json::accept(std::begin(v), std::end(v)));
|
||||
|
||||
json j_error;
|
||||
CHECK_NOTHROW(j_error = json::parse(std::begin(v), std::end(v), nullptr, false));
|
||||
std::unique_ptr<json::exception> ex;
|
||||
CHECK_NOTHROW(j_error = json::parse(std::begin(v), std::end(v), ex));
|
||||
CHECK(j_error.is_discarded());
|
||||
}
|
||||
|
||||
@ -283,7 +286,8 @@ TEST_CASE("deserialization")
|
||||
CHECK(not json::accept(std::begin(v), std::end(v)));
|
||||
|
||||
json j_error;
|
||||
CHECK_NOTHROW(j_error = json::parse(std::begin(v), std::end(v), nullptr, false));
|
||||
std::unique_ptr<json::exception> ex;
|
||||
CHECK_NOTHROW(j_error = json::parse(std::begin(v), std::end(v), ex));
|
||||
CHECK(j_error.is_discarded());
|
||||
}
|
||||
|
||||
@ -294,7 +298,8 @@ TEST_CASE("deserialization")
|
||||
CHECK(not json::accept(std::begin(v), std::end(v)));
|
||||
|
||||
json j_error;
|
||||
CHECK_NOTHROW(j_error = json::parse(std::begin(v), std::end(v), nullptr, false));
|
||||
std::unique_ptr<json::exception> ex;
|
||||
CHECK_NOTHROW(j_error = json::parse(std::begin(v), std::end(v), ex));
|
||||
CHECK(j_error.is_discarded());
|
||||
}
|
||||
|
||||
@ -305,7 +310,8 @@ TEST_CASE("deserialization")
|
||||
CHECK(not json::accept(std::begin(v), std::end(v)));
|
||||
|
||||
json j_error;
|
||||
CHECK_NOTHROW(j_error = json::parse(std::begin(v), std::end(v), nullptr, false));
|
||||
std::unique_ptr<json::exception> ex;
|
||||
CHECK_NOTHROW(j_error = json::parse(std::begin(v), std::end(v), ex));
|
||||
CHECK(j_error.is_discarded());
|
||||
}
|
||||
|
||||
@ -316,7 +322,8 @@ TEST_CASE("deserialization")
|
||||
CHECK(not json::accept(std::begin(v), std::end(v)));
|
||||
|
||||
json j_error;
|
||||
CHECK_NOTHROW(j_error = json::parse(std::begin(v), std::end(v), nullptr, false));
|
||||
std::unique_ptr<json::exception> ex;
|
||||
CHECK_NOTHROW(j_error = json::parse(std::begin(v), std::end(v), ex));
|
||||
CHECK(j_error.is_discarded());
|
||||
}
|
||||
|
||||
@ -329,7 +336,8 @@ TEST_CASE("deserialization")
|
||||
CHECK(not json::accept(std::begin(v), std::end(v)));
|
||||
|
||||
json j_error;
|
||||
CHECK_NOTHROW(j_error = json::parse(std::begin(v), std::end(v), nullptr, false));
|
||||
std::unique_ptr<json::exception> ex;
|
||||
CHECK_NOTHROW(j_error = json::parse(std::begin(v), std::end(v), ex));
|
||||
CHECK(j_error.is_discarded());
|
||||
}
|
||||
|
||||
@ -340,7 +348,8 @@ TEST_CASE("deserialization")
|
||||
CHECK(not json::accept(std::begin(v), std::end(v)));
|
||||
|
||||
json j_error;
|
||||
CHECK_NOTHROW(j_error = json::parse(std::begin(v), std::end(v), nullptr, false));
|
||||
std::unique_ptr<json::exception> ex;
|
||||
CHECK_NOTHROW(j_error = json::parse(std::begin(v), std::end(v), ex));
|
||||
CHECK(j_error.is_discarded());
|
||||
}
|
||||
|
||||
@ -351,7 +360,8 @@ TEST_CASE("deserialization")
|
||||
CHECK(not json::accept(std::begin(v), std::end(v)));
|
||||
|
||||
json j_error;
|
||||
CHECK_NOTHROW(j_error = json::parse(std::begin(v), std::end(v), nullptr, false));
|
||||
std::unique_ptr<json::exception> ex;
|
||||
CHECK_NOTHROW(j_error = json::parse(std::begin(v), std::end(v), ex));
|
||||
CHECK(j_error.is_discarded());
|
||||
}
|
||||
|
||||
@ -362,7 +372,8 @@ TEST_CASE("deserialization")
|
||||
CHECK(not json::accept(std::begin(v), std::end(v)));
|
||||
|
||||
json j_error;
|
||||
CHECK_NOTHROW(j_error = json::parse(std::begin(v), std::end(v), nullptr, false));
|
||||
std::unique_ptr<json::exception> ex;
|
||||
CHECK_NOTHROW(j_error = json::parse(std::begin(v), std::end(v), ex));
|
||||
CHECK(j_error.is_discarded());
|
||||
}
|
||||
|
||||
@ -373,7 +384,8 @@ TEST_CASE("deserialization")
|
||||
CHECK(not json::accept(std::begin(v), std::end(v)));
|
||||
|
||||
json j_error;
|
||||
CHECK_NOTHROW(j_error = json::parse(std::begin(v), std::end(v), nullptr, false));
|
||||
std::unique_ptr<json::exception> ex;
|
||||
CHECK_NOTHROW(j_error = json::parse(std::begin(v), std::end(v), ex));
|
||||
CHECK(j_error.is_discarded());
|
||||
}
|
||||
|
||||
@ -384,7 +396,8 @@ TEST_CASE("deserialization")
|
||||
CHECK(not json::accept(std::begin(v), std::end(v)));
|
||||
|
||||
json j_error;
|
||||
CHECK_NOTHROW(j_error = json::parse(std::begin(v), std::end(v), nullptr, false));
|
||||
std::unique_ptr<json::exception> ex;
|
||||
CHECK_NOTHROW(j_error = json::parse(std::begin(v), std::end(v), ex));
|
||||
CHECK(j_error.is_discarded());
|
||||
}
|
||||
|
||||
@ -395,7 +408,8 @@ TEST_CASE("deserialization")
|
||||
CHECK(not json::accept(std::begin(v), std::end(v)));
|
||||
|
||||
json j_error;
|
||||
CHECK_NOTHROW(j_error = json::parse(std::begin(v), std::end(v), nullptr, false));
|
||||
std::unique_ptr<json::exception> ex;
|
||||
CHECK_NOTHROW(j_error = json::parse(std::begin(v), std::end(v), ex));
|
||||
CHECK(j_error.is_discarded());
|
||||
}
|
||||
|
||||
@ -406,7 +420,8 @@ TEST_CASE("deserialization")
|
||||
CHECK(not json::accept(std::begin(v), std::end(v)));
|
||||
|
||||
json j_error;
|
||||
CHECK_NOTHROW(j_error = json::parse(std::begin(v), std::end(v), nullptr, false));
|
||||
std::unique_ptr<json::exception> ex;
|
||||
CHECK_NOTHROW(j_error = json::parse(std::begin(v), std::end(v), ex));
|
||||
CHECK(j_error.is_discarded());
|
||||
}
|
||||
|
||||
@ -417,7 +432,8 @@ TEST_CASE("deserialization")
|
||||
CHECK(not json::accept(std::begin(v), std::end(v)));
|
||||
|
||||
json j_error;
|
||||
CHECK_NOTHROW(j_error = json::parse(std::begin(v), std::end(v), nullptr, false));
|
||||
std::unique_ptr<json::exception> ex;
|
||||
CHECK_NOTHROW(j_error = json::parse(std::begin(v), std::end(v), ex));
|
||||
CHECK(j_error.is_discarded());
|
||||
}
|
||||
|
||||
@ -428,7 +444,8 @@ TEST_CASE("deserialization")
|
||||
CHECK(not json::accept(std::begin(v), std::end(v)));
|
||||
|
||||
json j_error;
|
||||
CHECK_NOTHROW(j_error = json::parse(std::begin(v), std::end(v), nullptr, false));
|
||||
std::unique_ptr<json::exception> ex;
|
||||
CHECK_NOTHROW(j_error = json::parse(std::begin(v), std::end(v), ex));
|
||||
CHECK(j_error.is_discarded());
|
||||
}
|
||||
|
||||
@ -439,7 +456,8 @@ TEST_CASE("deserialization")
|
||||
CHECK(not json::accept(std::begin(v), std::end(v)));
|
||||
|
||||
json j_error;
|
||||
CHECK_NOTHROW(j_error = json::parse(std::begin(v), std::end(v), nullptr, false));
|
||||
std::unique_ptr<json::exception> ex;
|
||||
CHECK_NOTHROW(j_error = json::parse(std::begin(v), std::end(v), ex));
|
||||
CHECK(j_error.is_discarded());
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user