Skip to content

Commit 8f5f359

Browse files
committed
Changed the SFINAE on the json encoders
1 parent 4c3e942 commit 8f5f359

3 files changed

Lines changed: 32 additions & 27 deletions

File tree

include/boost/exception/serialization/boost_json_encoder.hpp

Lines changed: 15 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -6,43 +6,36 @@
66
#ifndef BOOST_EXCEPTION_SERIALIZATION_BOOST_JSON_ENCODER_HPP_INCLUDED
77
#define BOOST_EXCEPTION_SERIALIZATION_BOOST_JSON_ENCODER_HPP_INCLUDED
88

9+
#include <type_traits>
910
#include <utility>
1011

12+
namespace boost { namespace json {
13+
14+
class value;
15+
16+
template <class T>
17+
void value_from(T &&, value &);
18+
19+
} }
20+
1121
namespace
1222
boost
1323
{
14-
namespace
15-
json
16-
{
17-
class value;
18-
struct value_from_tag;
19-
}
20-
2124
namespace
2225
exception_serialization
2326
{
24-
// Baast.JSON does not provide ADL interface for serializing user-defined types.
25-
// This limits the functionality of boost_json_encoder to only types that provide tag_invoke.
26-
template <class Value = boost::json::value, class ValueFromTag = boost::json::value_from_tag>
27+
template <class Value = boost::json::value>
2728
struct
2829
boost_json_encoder_
2930
{
3031
Value & v_;
3132

32-
template <class T>
33-
friend
34-
auto
35-
output(boost_json_encoder_ & e, T const & x) -> decltype(std::declval<Value &>() = x, void())
36-
{
37-
e.v_ = x;
38-
}
39-
40-
template <class T>
33+
template <class Encoder, class T, class... Deprioritize>
4134
friend
42-
auto
43-
output(boost_json_encoder_ & e, T const & x) -> decltype(tag_invoke(std::declval<ValueFromTag>(), std::declval<Value &>(), x), void())
35+
typename std::enable_if<std::is_same<Encoder, boost_json_encoder_>::value>::type
36+
output(Encoder & e, T const & x, Deprioritize...)
4437
{
45-
tag_invoke(ValueFromTag{}, e.v_, x);
38+
boost::json::value_from(x, e.v_);
4639
}
4740

4841
template <class T>

include/boost/exception/serialization/nlohmann_json_encoder.hpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#ifndef BOOST_EXCEPTION_SERIALIZATION_NLOHMANN_JSON_ENCODER_HPP_INCLUDED
77
#define BOOST_EXCEPTION_SERIALIZATION_NLOHMANN_JSON_ENCODER_HPP_INCLUDED
88

9+
#include <type_traits>
910
#include <utility>
1011

1112
namespace
@@ -20,10 +21,10 @@ boost
2021
{
2122
Json & j_;
2223

23-
template <class T>
24+
template <class Encoder, class T, class... Deprioritize>
2425
friend
25-
auto
26-
output(nlohmann_json_encoder & e, T const & x) -> decltype(to_json(std::declval<Json &>(), x))
26+
typename std::enable_if<std::is_same<Encoder, nlohmann_json_encoder>::value>::type
27+
output(Encoder & e, T const & x, Json * = 0, Deprioritize...)
2728
{
2829
to_json(e.j_, x);
2930
}

test/boost_json_test.cpp

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#include <iomanip>
1919
#include <iostream>
2020
#include <exception>
21+
#include <vector>
2122

2223
using output_encoder = boost::exception_serialization::boost_json_encoder;
2324

@@ -39,6 +40,7 @@ boost
3940

4041
typedef boost::error_info<struct my_error1_, int> my_error1;
4142
typedef boost::error_info<struct my_error2_, std::string> my_error2;
43+
typedef boost::error_info<struct my_error4_, std::vector<int>> my_error4;
4244

4345
struct
4446
my_info
@@ -97,6 +99,13 @@ check_output(boost::json::value const & j, bool has_source_location)
9799
auto const & mij = obj.at("my_error3_").as_object();
98100
BOOST_TEST_EQ(mij.at("code").as_int64(), 1);
99101
BOOST_TEST_EQ(mij.at("message").as_string(), "error one");
102+
103+
BOOST_TEST(obj.contains("my_error4_"));
104+
auto const & vec = obj.at("my_error4_").as_array();
105+
BOOST_TEST_EQ(vec.size(), 3u);
106+
BOOST_TEST_EQ(vec[0].as_int64(), 1);
107+
BOOST_TEST_EQ(vec[1].as_int64(), 2);
108+
BOOST_TEST_EQ(vec[2].as_int64(), 3);
100109
}
101110

102111
int
@@ -111,7 +120,8 @@ main()
111120
e <<
112121
my_error1(42) <<
113122
my_error2("hello") <<
114-
my_error3({1, "error one"});
123+
my_error3({1, "error one"}) <<
124+
my_error4({1, 2, 3});
115125
BOOST_THROW_EXCEPTION(e);
116126
}
117127
catch( test_exception & e )
@@ -132,7 +142,8 @@ main()
132142
e <<
133143
my_error1(42) <<
134144
my_error2("hello") <<
135-
my_error3({1, "error one"});
145+
my_error3({1, "error one"}) <<
146+
my_error4({1, 2, 3});
136147
BOOST_THROW_EXCEPTION(e);
137148
}
138149
catch( ... )

0 commit comments

Comments
 (0)