mirror of
https://github.com/nlohmann/json.git
synced 2026-09-30 03:30:31 +00:00
Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
a05b22ddf3 |
@@ -287,7 +287,7 @@ struct external_constructor<value_t::object>
|
|||||||
#ifdef JSON_HAS_CPP_17
|
#ifdef JSON_HAS_CPP_17
|
||||||
template<typename BasicJsonType, typename T,
|
template<typename BasicJsonType, typename T,
|
||||||
enable_if_t<std::is_constructible<BasicJsonType, T>::value, int> = 0>
|
enable_if_t<std::is_constructible<BasicJsonType, T>::value, int> = 0>
|
||||||
void to_json(BasicJsonType& j, const std::optional<T>& opt) noexcept
|
void to_json(BasicJsonType& j, const std::optional<T>& opt)
|
||||||
{
|
{
|
||||||
if (opt.has_value())
|
if (opt.has_value())
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -6816,7 +6816,7 @@ struct external_constructor<value_t::object>
|
|||||||
#ifdef JSON_HAS_CPP_17
|
#ifdef JSON_HAS_CPP_17
|
||||||
template<typename BasicJsonType, typename T,
|
template<typename BasicJsonType, typename T,
|
||||||
enable_if_t<std::is_constructible<BasicJsonType, T>::value, int> = 0>
|
enable_if_t<std::is_constructible<BasicJsonType, T>::value, int> = 0>
|
||||||
void to_json(BasicJsonType& j, const std::optional<T>& opt) noexcept
|
void to_json(BasicJsonType& j, const std::optional<T>& opt)
|
||||||
{
|
{
|
||||||
if (opt.has_value())
|
if (opt.has_value())
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -1826,6 +1826,16 @@ TEST_CASE("std::u8string")
|
|||||||
#endif
|
#endif
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
// a type whose to_json reports an error by throwing, used below to check that
|
||||||
|
// converting a std::optional<T> to JSON propagates an exception thrown while
|
||||||
|
// converting its contained value instead of calling std::terminate (#5642)
|
||||||
|
struct throwing_to_json_type {};
|
||||||
|
|
||||||
|
void to_json(json&, const throwing_to_json_type&)
|
||||||
|
{
|
||||||
|
throw std::runtime_error("cannot serialize throwing_to_json_type");
|
||||||
|
}
|
||||||
|
|
||||||
TEST_CASE("std::optional")
|
TEST_CASE("std::optional")
|
||||||
{
|
{
|
||||||
SECTION("null")
|
SECTION("null")
|
||||||
@@ -1908,6 +1918,19 @@ TEST_CASE("std::optional")
|
|||||||
CHECK(json(opt_object) == j_object);
|
CHECK(json(opt_object) == j_object);
|
||||||
CHECK(std::map<std::string, std::optional<int>>(j_object) == opt_object);
|
CHECK(std::map<std::string, std::optional<int>>(j_object) == opt_object);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#if !defined(JSON_NOEXCEPTION)
|
||||||
|
SECTION("exception from contained value's to_json propagates (#5642)")
|
||||||
|
{
|
||||||
|
// to_json(BasicJsonType&, const std::optional<T>&) must not be
|
||||||
|
// noexcept: it calls T's to_json, which may throw (a user-defined
|
||||||
|
// to_json that reports an error, or std::bad_alloc for T =
|
||||||
|
// std::string/vector/json). Before the fix, this called
|
||||||
|
// std::terminate() instead of letting the exception propagate.
|
||||||
|
const std::optional<throwing_to_json_type> opt = throwing_to_json_type{};
|
||||||
|
CHECK_THROWS_WITH_AS(json(opt), "cannot serialize throwing_to_json_type", std::runtime_error&);
|
||||||
|
}
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user