Skip to content

Commit dff40ee

Browse files
committed
to_json .value fixes
1 parent a8bef0b commit dff40ee

File tree

2 files changed

+19
-14
lines changed

2 files changed

+19
-14
lines changed

include/boost/leaf/serialize.hpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -233,12 +233,6 @@ struct ostream_writer::diagnostic<Enum, true, false, false, false, true>
233233

234234
////////////////////////////////////////
235235

236-
template <class Json, class E>
237-
auto to_json(Json & j, E const & e) -> decltype(to_json(j, e.value), void())
238-
{
239-
j["value"] = e.value;
240-
}
241-
242236
#if BOOST_LEAF_CFG_STD_SYSTEM_ERROR
243237
template <class Json>
244238
void to_json(Json & j, std::error_code const & ec)
@@ -296,6 +290,12 @@ void to_json(Json & j, std::exception_ptr const & ep)
296290
}
297291
}
298292

293+
template <class Json, class E>
294+
auto to_json(Json & j, E const & e) -> decltype(to_json(j, e.value), void())
295+
{
296+
to_json(j["value"], e.value);
297+
}
298+
299299
template <class Json, class E, class = void>
300300
struct has_to_json: std::false_type { };
301301

test/json_test.cpp

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,13 @@ void serialize(writer & w, E const & e)
7070

7171
} } // namespace boost::leaf
7272

73+
struct my_exception { };
74+
75+
struct my_exception_ptr
76+
{
77+
std::exception_ptr value;
78+
};
79+
7380
template <int N>
7481
struct my_error
7582
{
@@ -110,8 +117,6 @@ void leaf_throw()
110117
std::make_error_code(std::errc::invalid_argument) );
111118
}
112119

113-
struct my_exception { };
114-
115120
void throw_()
116121
{
117122
auto load = leaf::on_error(
@@ -312,17 +317,17 @@ int main()
312317
leaf::try_handle_all(
313318
[]() -> leaf::result<void>
314319
{
315-
return leaf::new_error(std::make_exception_ptr(std::runtime_error("test exception")));
320+
return leaf::new_error(my_exception_ptr{std::make_exception_ptr(std::runtime_error("test exception"))});
316321
},
317-
[&j](leaf::diagnostic_details const & dd, std::exception_ptr *)
322+
[&j](leaf::diagnostic_details const & dd, my_exception_ptr *)
318323
{
319324
nlohmann_writer w(j, dd.error());
320325
dd.write_to(w);
321326
}
322327
);
323328
std::cout << "std::exception_ptr JSON output:\n" << std::setw(2) << j << std::endl;
324329

325-
auto const & ep = j["std::exception_ptr"];
330+
auto const & ep = j["my_exception_ptr"]["value"];
326331
std::string type = ep["typeid.name"].get<std::string>();
327332
std::string what = ep["what"].get<std::string>();
328333
BOOST_TEST(type.find("std::runtime_error") != std::string::npos);
@@ -334,17 +339,17 @@ int main()
334339
leaf::try_handle_all(
335340
[]() -> leaf::result<void>
336341
{
337-
return leaf::new_error(std::make_exception_ptr(42));
342+
return leaf::new_error(my_exception_ptr{std::make_exception_ptr(42)});
338343
},
339-
[&j](leaf::diagnostic_details const & dd, std::exception_ptr *)
344+
[&j](leaf::diagnostic_details const & dd, my_exception_ptr *)
340345
{
341346
nlohmann_writer w(j, dd.error());
342347
dd.write_to(w);
343348
}
344349
);
345350
std::cout << "non-std::exception_ptr JSON output:\n" << std::setw(2) << j << std::endl;
346351

347-
auto const & ep = j["std::exception_ptr"];
352+
auto const & ep = j["my_exception_ptr"]["value"];
348353
std::string type = ep["typeid.name"].get<std::string>();
349354
std::string what = ep["what"].get<std::string>();
350355
BOOST_TEST_EQ(type, "<<unknown>>");

0 commit comments

Comments
 (0)