1  
//
1  
//
2  
// Copyright (c) 2019 Vinnie Falco (vinnie.falco@gmail.com)
2  
// Copyright (c) 2019 Vinnie Falco (vinnie.falco@gmail.com)
3  
// Copyright (c) 2020 Krystian Stasiowski (sdkrystian@gmail.com)
3  
// Copyright (c) 2020 Krystian Stasiowski (sdkrystian@gmail.com)
4  
// Copyright (c) 2021 Dmitry Arkhipov (grisumbras@gmail.com)
4  
// Copyright (c) 2021 Dmitry Arkhipov (grisumbras@gmail.com)
5  
//
5  
//
6  
// Distributed under the Boost Software License, Version 1.0. (See accompanying
6  
// Distributed under the Boost Software License, Version 1.0. (See accompanying
7  
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
7  
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
8  
//
8  
//
9  
// Official repository: https://github.com/boostorg/json
9  
// Official repository: https://github.com/boostorg/json
10  
//
10  
//
11  

11  

12  
#ifndef BOOST_JSON_DETAIL_VALUE_TO_HPP
12  
#ifndef BOOST_JSON_DETAIL_VALUE_TO_HPP
13  
#define BOOST_JSON_DETAIL_VALUE_TO_HPP
13  
#define BOOST_JSON_DETAIL_VALUE_TO_HPP
14 -
#include <boost/core/detail/static_assert.hpp>
 
15  

14  

16  
#include <boost/json/value.hpp>
15  
#include <boost/json/value.hpp>
17  
#include <boost/json/conversion.hpp>
16  
#include <boost/json/conversion.hpp>
18  
#include <boost/json/result_for.hpp>
17  
#include <boost/json/result_for.hpp>
19  
#include <boost/describe/enum_from_string.hpp>
18  
#include <boost/describe/enum_from_string.hpp>
20  

19  

21  
#ifndef BOOST_NO_CXX17_HDR_OPTIONAL
20  
#ifndef BOOST_NO_CXX17_HDR_OPTIONAL
22  
# include <optional>
21  
# include <optional>
23  
#endif
22  
#endif
24  

23  

25  
namespace boost {
24  
namespace boost {
26  
namespace json {
25  
namespace json {
27  

26  

28  
namespace detail {
27  
namespace detail {
29  

28  

30  
template<class T>
29  
template<class T>
31  
using has_reserve_member_helper = decltype(std::declval<T&>().reserve(0));
30  
using has_reserve_member_helper = decltype(std::declval<T&>().reserve(0));
32  
template<class T>
31  
template<class T>
33  
using has_reserve_member = mp11::mp_valid<has_reserve_member_helper, T>;
32  
using has_reserve_member = mp11::mp_valid<has_reserve_member_helper, T>;
34  
template<class T>
33  
template<class T>
35  
using reserve_implementation = mp11::mp_cond<
34  
using reserve_implementation = mp11::mp_cond<
36  
    is_tuple_like<T>,      mp11::mp_int<2>,
35  
    is_tuple_like<T>,      mp11::mp_int<2>,
37  
    has_reserve_member<T>, mp11::mp_int<1>,
36  
    has_reserve_member<T>, mp11::mp_int<1>,
38  
    mp11::mp_true,         mp11::mp_int<0>>;
37  
    mp11::mp_true,         mp11::mp_int<0>>;
39  

38  

40  
template<class T>
39  
template<class T>
41  
error
40  
error
42  
try_reserve(
41  
try_reserve(
43  
    T&,
42  
    T&,
44  
    std::size_t size,
43  
    std::size_t size,
45  
    mp11::mp_int<2>)
44  
    mp11::mp_int<2>)
46  
{
45  
{
47  
    constexpr std::size_t N = std::tuple_size<remove_cvref<T>>::value;
46  
    constexpr std::size_t N = std::tuple_size<remove_cvref<T>>::value;
48  
    if ( N != size )
47  
    if ( N != size )
49  
        return error::size_mismatch;
48  
        return error::size_mismatch;
50  
    return error();
49  
    return error();
51  
}
50  
}
52  

51  

53  
template<typename T>
52  
template<typename T>
54  
error
53  
error
55  
try_reserve(
54  
try_reserve(
56  
    T& cont,
55  
    T& cont,
57  
    std::size_t size,
56  
    std::size_t size,
58  
    mp11::mp_int<1>)
57  
    mp11::mp_int<1>)
59  
{
58  
{
60  
    cont.reserve(size);
59  
    cont.reserve(size);
61  
    return error();
60  
    return error();
62  
}
61  
}
63  

62  

64  
template<typename T>
63  
template<typename T>
65  
error
64  
error
66  
try_reserve(
65  
try_reserve(
67  
    T&,
66  
    T&,
68  
    std::size_t,
67  
    std::size_t,
69  
    mp11::mp_int<0>)
68  
    mp11::mp_int<0>)
70  
{
69  
{
71  
    return error();
70  
    return error();
72  
}
71  
}
73  

72  

74  

73  

75  
// identity conversion
74  
// identity conversion
76  
template< class Ctx >
75  
template< class Ctx >
77  
system::result<value>
76  
system::result<value>
78  
value_to_impl(
77  
value_to_impl(
79  
    value_conversion_tag,
78  
    value_conversion_tag,
80  
    try_value_to_tag<value>,
79  
    try_value_to_tag<value>,
81  
    value const& jv,
80  
    value const& jv,
82  
    Ctx const& )
81  
    Ctx const& )
83  
{
82  
{
84  
    return jv;
83  
    return jv;
85  
}
84  
}
86  

85  

87  
template< class Ctx >
86  
template< class Ctx >
88  
value
87  
value
89  
value_to_impl(
88  
value_to_impl(
90  
    value_conversion_tag, value_to_tag<value>, value const& jv, Ctx const& )
89  
    value_conversion_tag, value_to_tag<value>, value const& jv, Ctx const& )
91  
{
90  
{
92  
    return jv;
91  
    return jv;
93  
}
92  
}
94  

93  

95  
// object
94  
// object
96  
template< class Ctx >
95  
template< class Ctx >
97  
system::result<object>
96  
system::result<object>
98  
value_to_impl(
97  
value_to_impl(
99  
    object_conversion_tag,
98  
    object_conversion_tag,
100  
    try_value_to_tag<object>,
99  
    try_value_to_tag<object>,
101  
    value const& jv,
100  
    value const& jv,
102  
    Ctx const& )
101  
    Ctx const& )
103  
{
102  
{
104  
    object const* obj = jv.if_object();
103  
    object const* obj = jv.if_object();
105  
    if( obj )
104  
    if( obj )
106  
        return *obj;
105  
        return *obj;
107  
    system::error_code ec;
106  
    system::error_code ec;
108  
    BOOST_JSON_FAIL(ec, error::not_object);
107  
    BOOST_JSON_FAIL(ec, error::not_object);
109  
    return ec;
108  
    return ec;
110  
}
109  
}
111  

110  

112  
// array
111  
// array
113  
template< class Ctx >
112  
template< class Ctx >
114  
system::result<array>
113  
system::result<array>
115  
value_to_impl(
114  
value_to_impl(
116  
    array_conversion_tag,
115  
    array_conversion_tag,
117  
    try_value_to_tag<array>,
116  
    try_value_to_tag<array>,
118  
    value const& jv,
117  
    value const& jv,
119  
    Ctx const& )
118  
    Ctx const& )
120  
{
119  
{
121  
    array const* arr = jv.if_array();
120  
    array const* arr = jv.if_array();
122  
    if( arr )
121  
    if( arr )
123  
        return *arr;
122  
        return *arr;
124  
    system::error_code ec;
123  
    system::error_code ec;
125  
    BOOST_JSON_FAIL(ec, error::not_array);
124  
    BOOST_JSON_FAIL(ec, error::not_array);
126  
    return ec;
125  
    return ec;
127  
}
126  
}
128  

127  

129  
// string
128  
// string
130  
template< class Ctx >
129  
template< class Ctx >
131  
system::result<string>
130  
system::result<string>
132  
value_to_impl(
131  
value_to_impl(
133  
    string_conversion_tag,
132  
    string_conversion_tag,
134  
    try_value_to_tag<string>,
133  
    try_value_to_tag<string>,
135  
    value const& jv,
134  
    value const& jv,
136  
    Ctx const& )
135  
    Ctx const& )
137  
{
136  
{
138  
    string const* str = jv.if_string();
137  
    string const* str = jv.if_string();
139  
    if( str )
138  
    if( str )
140  
        return *str;
139  
        return *str;
141  
    system::error_code ec;
140  
    system::error_code ec;
142  
    BOOST_JSON_FAIL(ec, error::not_string);
141  
    BOOST_JSON_FAIL(ec, error::not_string);
143  
    return ec;
142  
    return ec;
144  
}
143  
}
145  

144  

146  
// bool
145  
// bool
147  
template< class Ctx >
146  
template< class Ctx >
148  
system::result<bool>
147  
system::result<bool>
149  
value_to_impl(
148  
value_to_impl(
150  
    bool_conversion_tag, try_value_to_tag<bool>, value const& jv, Ctx const& )
149  
    bool_conversion_tag, try_value_to_tag<bool>, value const& jv, Ctx const& )
151  
{
150  
{
152  
    auto b = jv.if_bool();
151  
    auto b = jv.if_bool();
153  
    if( b )
152  
    if( b )
154  
        return *b;
153  
        return *b;
155  
    system::error_code ec;
154  
    system::error_code ec;
156  
    BOOST_JSON_FAIL(ec, error::not_bool);
155  
    BOOST_JSON_FAIL(ec, error::not_bool);
157  
    return {boost::system::in_place_error, ec};
156  
    return {boost::system::in_place_error, ec};
158  
}
157  
}
159  

158  

160  
// integral and floating point
159  
// integral and floating point
161  
template< class T, class Ctx >
160  
template< class T, class Ctx >
162  
system::result<T>
161  
system::result<T>
163  
value_to_impl(
162  
value_to_impl(
164  
    number_conversion_tag, try_value_to_tag<T>, value const& jv, Ctx const& )
163  
    number_conversion_tag, try_value_to_tag<T>, value const& jv, Ctx const& )
165  
{
164  
{
166  
    system::error_code ec;
165  
    system::error_code ec;
167  
    auto const n = jv.to_number<T>(ec);
166  
    auto const n = jv.to_number<T>(ec);
168  
    if( ec.failed() )
167  
    if( ec.failed() )
169  
        return {boost::system::in_place_error, ec};
168  
        return {boost::system::in_place_error, ec};
170  
    return {boost::system::in_place_value, n};
169  
    return {boost::system::in_place_value, n};
171  
}
170  
}
172  

171  

173  
// null-like conversion
172  
// null-like conversion
174  
template< class T, class Ctx >
173  
template< class T, class Ctx >
175  
system::result<T>
174  
system::result<T>
176  
value_to_impl(
175  
value_to_impl(
177  
    null_like_conversion_tag,
176  
    null_like_conversion_tag,
178  
    try_value_to_tag<T>,
177  
    try_value_to_tag<T>,
179  
    value const& jv,
178  
    value const& jv,
180  
    Ctx const& )
179  
    Ctx const& )
181  
{
180  
{
182  
    if( jv.is_null() )
181  
    if( jv.is_null() )
183  
        return {boost::system::in_place_value, T{}};
182  
        return {boost::system::in_place_value, T{}};
184  
    system::error_code ec;
183  
    system::error_code ec;
185  
    BOOST_JSON_FAIL(ec, error::not_null);
184  
    BOOST_JSON_FAIL(ec, error::not_null);
186  
    return {boost::system::in_place_error, ec};
185  
    return {boost::system::in_place_error, ec};
187  
}
186  
}
188  

187  

189  
// string-like types
188  
// string-like types
190  
template< class T, class Ctx >
189  
template< class T, class Ctx >
191  
system::result<T>
190  
system::result<T>
192  
value_to_impl(
191  
value_to_impl(
193  
    string_like_conversion_tag,
192  
    string_like_conversion_tag,
194  
    try_value_to_tag<T>,
193  
    try_value_to_tag<T>,
195  
    value const& jv,
194  
    value const& jv,
196  
    Ctx const& )
195  
    Ctx const& )
197  
{
196  
{
198  
    auto str = jv.if_string();
197  
    auto str = jv.if_string();
199  
    if( str )
198  
    if( str )
200  
        return {boost::system::in_place_value, T(str->subview())};
199  
        return {boost::system::in_place_value, T(str->subview())};
201  
    system::error_code ec;
200  
    system::error_code ec;
202  
    BOOST_JSON_FAIL(ec, error::not_string);
201  
    BOOST_JSON_FAIL(ec, error::not_string);
203  
    return {boost::system::in_place_error, ec};
202  
    return {boost::system::in_place_error, ec};
204  
}
203  
}
205  

204  

206  
// map-like containers
205  
// map-like containers
207  
template< class T, class Ctx >
206  
template< class T, class Ctx >
208  
system::result<T>
207  
system::result<T>
209  
value_to_impl(
208  
value_to_impl(
210  
    map_like_conversion_tag,
209  
    map_like_conversion_tag,
211  
    try_value_to_tag<T>,
210  
    try_value_to_tag<T>,
212  
    value const& jv,
211  
    value const& jv,
213  
    Ctx const& ctx )
212  
    Ctx const& ctx )
214  
{
213  
{
215  
    object const* obj = jv.if_object();
214  
    object const* obj = jv.if_object();
216  
    if( !obj )
215  
    if( !obj )
217  
    {
216  
    {
218  
        system::error_code ec;
217  
        system::error_code ec;
219  
        BOOST_JSON_FAIL(ec, error::not_object);
218  
        BOOST_JSON_FAIL(ec, error::not_object);
220  
        return {boost::system::in_place_error, ec};
219  
        return {boost::system::in_place_error, ec};
221  
    }
220  
    }
222  

221  

223  
    T res;
222  
    T res;
224  
    error const e = detail::try_reserve(
223  
    error const e = detail::try_reserve(
225  
        res, obj->size(), reserve_implementation<T>());
224  
        res, obj->size(), reserve_implementation<T>());
226  
    if( e != error() )
225  
    if( e != error() )
227  
    {
226  
    {
228  
        system::error_code ec;
227  
        system::error_code ec;
229  
        BOOST_JSON_FAIL( ec, e );
228  
        BOOST_JSON_FAIL( ec, e );
230  
        return {boost::system::in_place_error, ec};
229  
        return {boost::system::in_place_error, ec};
231  
    }
230  
    }
232  

231  

233  
    auto ins = detail::inserter(res, inserter_implementation<T>());
232  
    auto ins = detail::inserter(res, inserter_implementation<T>());
234  
    for( key_value_pair const& kv: *obj )
233  
    for( key_value_pair const& kv: *obj )
235  
    {
234  
    {
236  
        auto elem_res = try_value_to<mapped_type<T>>( kv.value(), ctx );
235  
        auto elem_res = try_value_to<mapped_type<T>>( kv.value(), ctx );
237  
        if( elem_res.has_error() )
236  
        if( elem_res.has_error() )
238  
            return {boost::system::in_place_error, elem_res.error()};
237  
            return {boost::system::in_place_error, elem_res.error()};
239  
        *ins++ = value_type<T>{
238  
        *ins++ = value_type<T>{
240  
            key_type<T>(kv.key()),
239  
            key_type<T>(kv.key()),
241  
            std::move(*elem_res)};
240  
            std::move(*elem_res)};
242  
    }
241  
    }
243  
    return res;
242  
    return res;
244  
}
243  
}
245  

244  

246  
// all other containers
245  
// all other containers
247  
template< class T, class Ctx >
246  
template< class T, class Ctx >
248  
system::result<T>
247  
system::result<T>
249  
value_to_impl(
248  
value_to_impl(
250  
    sequence_conversion_tag,
249  
    sequence_conversion_tag,
251  
    try_value_to_tag<T>,
250  
    try_value_to_tag<T>,
252  
    value const& jv,
251  
    value const& jv,
253  
    Ctx const& ctx )
252  
    Ctx const& ctx )
254  
{
253  
{
255  
    array const* arr = jv.if_array();
254  
    array const* arr = jv.if_array();
256  
    if( !arr )
255  
    if( !arr )
257  
    {
256  
    {
258  
        system::error_code ec;
257  
        system::error_code ec;
259  
        BOOST_JSON_FAIL(ec, error::not_array);
258  
        BOOST_JSON_FAIL(ec, error::not_array);
260  
        return {boost::system::in_place_error, ec};
259  
        return {boost::system::in_place_error, ec};
261  
    }
260  
    }
262  

261  

263  
    T result;
262  
    T result;
264  
    error const e = detail::try_reserve(
263  
    error const e = detail::try_reserve(
265  
        result, arr->size(), reserve_implementation<T>());
264  
        result, arr->size(), reserve_implementation<T>());
266  
    if( e != error() )
265  
    if( e != error() )
267  
    {
266  
    {
268  
        system::error_code ec;
267  
        system::error_code ec;
269  
        BOOST_JSON_FAIL( ec, e );
268  
        BOOST_JSON_FAIL( ec, e );
270  
        return {boost::system::in_place_error, ec};
269  
        return {boost::system::in_place_error, ec};
271  
    }
270  
    }
272  

271  

273  
    auto ins = detail::inserter(result, inserter_implementation<T>());
272  
    auto ins = detail::inserter(result, inserter_implementation<T>());
274  
    for( value const& val: *arr )
273  
    for( value const& val: *arr )
275  
    {
274  
    {
276  
        auto elem_res = try_value_to<value_type<T>>( val, ctx );
275  
        auto elem_res = try_value_to<value_type<T>>( val, ctx );
277  
        if( elem_res.has_error() )
276  
        if( elem_res.has_error() )
278  
            return {boost::system::in_place_error, elem_res.error()};
277  
            return {boost::system::in_place_error, elem_res.error()};
279  
        *ins++ = std::move(*elem_res);
278  
        *ins++ = std::move(*elem_res);
280  
    }
279  
    }
281  
    return result;
280  
    return result;
282  
}
281  
}
283  

282  

284  
// tuple-like types
283  
// tuple-like types
285  
template< class T, class Ctx >
284  
template< class T, class Ctx >
286  
system::result<T>
285  
system::result<T>
287  
try_make_tuple_elem(value const& jv, Ctx const& ctx, system::error_code& ec)
286  
try_make_tuple_elem(value const& jv, Ctx const& ctx, system::error_code& ec)
288  
{
287  
{
289  
    if( ec.failed() )
288  
    if( ec.failed() )
290  
        return {boost::system::in_place_error, ec};
289  
        return {boost::system::in_place_error, ec};
291  

290  

292  
    auto result = try_value_to<T>( jv, ctx );
291  
    auto result = try_value_to<T>( jv, ctx );
293  
    ec = result.error();
292  
    ec = result.error();
294  
    return result;
293  
    return result;
295  
}
294  
}
296  

295  

297  
template <class T, class Ctx, std::size_t... Is>
296  
template <class T, class Ctx, std::size_t... Is>
298  
system::result<T>
297  
system::result<T>
299  
try_make_tuple_like(
298  
try_make_tuple_like(
300  
    array const& arr, Ctx const& ctx, boost::mp11::index_sequence<Is...>)
299  
    array const& arr, Ctx const& ctx, boost::mp11::index_sequence<Is...>)
301  
{
300  
{
302  
    system::error_code ec;
301  
    system::error_code ec;
303  
    auto items = std::make_tuple(
302  
    auto items = std::make_tuple(
304  
        try_make_tuple_elem<
303  
        try_make_tuple_elem<
305  
            typename std::decay<tuple_element_t<Is, T>>::type >(
304  
            typename std::decay<tuple_element_t<Is, T>>::type >(
306  
                arr[Is], ctx, ec)
305  
                arr[Is], ctx, ec)
307  
            ...);
306  
            ...);
308  
#if defined(BOOST_GCC)
307  
#if defined(BOOST_GCC)
309  
# pragma GCC diagnostic push
308  
# pragma GCC diagnostic push
310  
# pragma GCC diagnostic ignored "-Wmaybe-uninitialized"
309  
# pragma GCC diagnostic ignored "-Wmaybe-uninitialized"
311  
#endif
310  
#endif
312  
    if( ec.failed() )
311  
    if( ec.failed() )
313  
        return {boost::system::in_place_error, ec};
312  
        return {boost::system::in_place_error, ec};
314  
#if defined(BOOST_GCC)
313  
#if defined(BOOST_GCC)
315  
# pragma GCC diagnostic pop
314  
# pragma GCC diagnostic pop
316  
#endif
315  
#endif
317  

316  

318  
    return {
317  
    return {
319  
        boost::system::in_place_value, T(std::move(*std::get<Is>(items))...)};
318  
        boost::system::in_place_value, T(std::move(*std::get<Is>(items))...)};
320  
}
319  
}
321  

320  

322  
template< class T, class Ctx >
321  
template< class T, class Ctx >
323  
system::result<T>
322  
system::result<T>
324  
value_to_impl(
323  
value_to_impl(
325  
    tuple_conversion_tag,
324  
    tuple_conversion_tag,
326  
    try_value_to_tag<T>,
325  
    try_value_to_tag<T>,
327  
    value const& jv,
326  
    value const& jv,
328  
    Ctx const& ctx )
327  
    Ctx const& ctx )
329  
{
328  
{
330  
    system::error_code ec;
329  
    system::error_code ec;
331  

330  

332  
    array const* arr = jv.if_array();
331  
    array const* arr = jv.if_array();
333  
    if( !arr )
332  
    if( !arr )
334  
    {
333  
    {
335  
        BOOST_JSON_FAIL(ec, error::not_array);
334  
        BOOST_JSON_FAIL(ec, error::not_array);
336  
        return {boost::system::in_place_error, ec};
335  
        return {boost::system::in_place_error, ec};
337  
    }
336  
    }
338  

337  

339  
    constexpr std::size_t N = std::tuple_size<remove_cvref<T>>::value;
338  
    constexpr std::size_t N = std::tuple_size<remove_cvref<T>>::value;
340  
    if( N != arr->size() )
339  
    if( N != arr->size() )
341  
    {
340  
    {
342  
        BOOST_JSON_FAIL(ec, error::size_mismatch);
341  
        BOOST_JSON_FAIL(ec, error::size_mismatch);
343  
        return {boost::system::in_place_error, ec};
342  
        return {boost::system::in_place_error, ec};
344  
    }
343  
    }
345  

344  

346  
    return try_make_tuple_like<T>(
345  
    return try_make_tuple_like<T>(
347  
        *arr, ctx, boost::mp11::make_index_sequence<N>());
346  
        *arr, ctx, boost::mp11::make_index_sequence<N>());
348  
}
347  
}
349  

348  

350  
template< class Ctx, class T >
349  
template< class Ctx, class T >
351  
struct to_described_member
350  
struct to_described_member
352  
{
351  
{
353  
    static_assert(
352  
    static_assert(
354  
        uniquely_named_members<T>::value,
353  
        uniquely_named_members<T>::value,
355  
        "The type has several described members with the same name.");
354  
        "The type has several described members with the same name.");
356  

355  

357  
    using Ds = described_members<T>;
356  
    using Ds = described_members<T>;
358  

357  

359  
    system::result<T>& res;
358  
    system::result<T>& res;
360  
    object const& obj;
359  
    object const& obj;
361  
    Ctx const& ctx;
360  
    Ctx const& ctx;
362  

361  

363  
    template< class I >
362  
    template< class I >
364  
    void
363  
    void
365  
    operator()(I)
364  
    operator()(I)
366  
    {
365  
    {
367  
        if( !res )
366  
        if( !res )
368  
            return;
367  
            return;
369  

368  

370  
        using D = mp11::mp_at<Ds, I>;
369  
        using D = mp11::mp_at<Ds, I>;
371  
        using M = described_member_t<T, D>;
370  
        using M = described_member_t<T, D>;
372  

371  

373  
        auto const found = obj.find(D::name);
372  
        auto const found = obj.find(D::name);
374  
        if( found == obj.end() )
373  
        if( found == obj.end() )
375  
        {
374  
        {
376  
            BOOST_IF_CONSTEXPR( !is_optional_like<M>::value )
375  
            BOOST_IF_CONSTEXPR( !is_optional_like<M>::value )
377  
            {
376  
            {
378  
                system::error_code ec;
377  
                system::error_code ec;
379  
                BOOST_JSON_FAIL(ec, error::size_mismatch);
378  
                BOOST_JSON_FAIL(ec, error::size_mismatch);
380  
                res = {boost::system::in_place_error, ec};
379  
                res = {boost::system::in_place_error, ec};
381  
            }
380  
            }
382  
            return;
381  
            return;
383  
        }
382  
        }
384  

383  

385  
#if defined(__GNUC__) && BOOST_GCC_VERSION >= 80000 && BOOST_GCC_VERSION < 11000
384  
#if defined(__GNUC__) && BOOST_GCC_VERSION >= 80000 && BOOST_GCC_VERSION < 11000
386  
# pragma GCC diagnostic push
385  
# pragma GCC diagnostic push
387  
# pragma GCC diagnostic ignored "-Wunused"
386  
# pragma GCC diagnostic ignored "-Wunused"
388  
# pragma GCC diagnostic ignored "-Wunused-variable"
387  
# pragma GCC diagnostic ignored "-Wunused-variable"
389  
#endif
388  
#endif
390  
        auto member_res = try_value_to<M>( found->value(), ctx );
389  
        auto member_res = try_value_to<M>( found->value(), ctx );
391  
#if defined(__GNUC__) && BOOST_GCC_VERSION >= 80000 && BOOST_GCC_VERSION < 11000
390  
#if defined(__GNUC__) && BOOST_GCC_VERSION >= 80000 && BOOST_GCC_VERSION < 11000
392  
# pragma GCC diagnostic pop
391  
# pragma GCC diagnostic pop
393  
#endif
392  
#endif
394  
        if( member_res )
393  
        if( member_res )
395  
            (*res).* D::pointer = std::move(*member_res);
394  
            (*res).* D::pointer = std::move(*member_res);
396  
        else
395  
        else
397  
            res = {boost::system::in_place_error, member_res.error()};
396  
            res = {boost::system::in_place_error, member_res.error()};
398  
    }
397  
    }
399  
};
398  
};
400  

399  

401  
// described classes
400  
// described classes
402  
template< class T, class Ctx >
401  
template< class T, class Ctx >
403  
system::result<T>
402  
system::result<T>
404  
value_to_impl(
403  
value_to_impl(
405  
    described_class_conversion_tag,
404  
    described_class_conversion_tag,
406  
    try_value_to_tag<T>,
405  
    try_value_to_tag<T>,
407  
    value const& jv,
406  
    value const& jv,
408  
    Ctx const& ctx )
407  
    Ctx const& ctx )
409  
{
408  
{
410 -
    BOOST_CORE_STATIC_ASSERT( std::is_default_constructible<T>::value );
409 +
    BOOST_STATIC_ASSERT( std::is_default_constructible<T>::value );
411  
    system::result<T> res;
410  
    system::result<T> res;
412  

411  

413  
    auto* obj = jv.if_object();
412  
    auto* obj = jv.if_object();
414  
    if( !obj )
413  
    if( !obj )
415  
    {
414  
    {
416  
        system::error_code ec;
415  
        system::error_code ec;
417  
        BOOST_JSON_FAIL(ec, error::not_object);
416  
        BOOST_JSON_FAIL(ec, error::not_object);
418  
        res = {boost::system::in_place_error, ec};
417  
        res = {boost::system::in_place_error, ec};
419  
        return res;
418  
        return res;
420  
    }
419  
    }
421  

420  

422  
    to_described_member<Ctx, T> member_converter{res, *obj, ctx};
421  
    to_described_member<Ctx, T> member_converter{res, *obj, ctx};
423  

422  

424  
    using Ds = typename decltype(member_converter)::Ds;
423  
    using Ds = typename decltype(member_converter)::Ds;
425  
    constexpr std::size_t N = mp11::mp_size<Ds>::value;
424  
    constexpr std::size_t N = mp11::mp_size<Ds>::value;
426  
    mp11::mp_for_each< mp11::mp_iota_c<N> >(member_converter);
425  
    mp11::mp_for_each< mp11::mp_iota_c<N> >(member_converter);
427  

426  

428  
    if( !res )
427  
    if( !res )
429  
        return res;
428  
        return res;
430  

429  

431  
    return res;
430  
    return res;
432  
}
431  
}
433  

432  

434  
// described enums
433  
// described enums
435  
template< class T, class Ctx >
434  
template< class T, class Ctx >
436  
system::result<T>
435  
system::result<T>
437  
value_to_impl(
436  
value_to_impl(
438  
    described_enum_conversion_tag,
437  
    described_enum_conversion_tag,
439  
    try_value_to_tag<T>,
438  
    try_value_to_tag<T>,
440  
    value const& jv,
439  
    value const& jv,
441  
    Ctx const& )
440  
    Ctx const& )
442  
{
441  
{
443  
    T val = {};
442  
    T val = {};
444  
    (void)jv;
443  
    (void)jv;
445  
#ifdef BOOST_DESCRIBE_CXX14
444  
#ifdef BOOST_DESCRIBE_CXX14
446  
    system::error_code ec;
445  
    system::error_code ec;
447  

446  

448  
    auto str = jv.if_string();
447  
    auto str = jv.if_string();
449  
    if( !str )
448  
    if( !str )
450  
    {
449  
    {
451  
        BOOST_JSON_FAIL(ec, error::not_string);
450  
        BOOST_JSON_FAIL(ec, error::not_string);
452  
        return {system::in_place_error, ec};
451  
        return {system::in_place_error, ec};
453  
    }
452  
    }
454  

453  

455  
    if( !describe::enum_from_string(str->data(), val) )
454  
    if( !describe::enum_from_string(str->data(), val) )
456  
    {
455  
    {
457  
        BOOST_JSON_FAIL(ec, error::unknown_name);
456  
        BOOST_JSON_FAIL(ec, error::unknown_name);
458  
        return {system::in_place_error, ec};
457  
        return {system::in_place_error, ec};
459  
    }
458  
    }
460  
#endif
459  
#endif
461  

460  

462  
    return {system::in_place_value, val};
461  
    return {system::in_place_value, val};
463  
}
462  
}
464  

463  

465  
// optionals
464  
// optionals
466  
template< class T, class Ctx >
465  
template< class T, class Ctx >
467  
system::result<T>
466  
system::result<T>
468  
value_to_impl(
467  
value_to_impl(
469  
    optional_conversion_tag,
468  
    optional_conversion_tag,
470  
    try_value_to_tag<T>,
469  
    try_value_to_tag<T>,
471  
    value const& jv,
470  
    value const& jv,
472  
    Ctx const& ctx)
471  
    Ctx const& ctx)
473  
{
472  
{
474  
    using Inner = value_result_type<T>;
473  
    using Inner = value_result_type<T>;
475  
    if( jv.is_null() )
474  
    if( jv.is_null() )
476  
        return {};
475  
        return {};
477  
    else
476  
    else
478  
        return try_value_to<Inner>(jv, ctx);
477  
        return try_value_to<Inner>(jv, ctx);
479  
}
478  
}
480  

479  

481  
// variants
480  
// variants
482  
template< class T, class V, class I >
481  
template< class T, class V, class I >
483  
using variant_construction_category = mp11::mp_cond<
482  
using variant_construction_category = mp11::mp_cond<
484  
    std::is_constructible< T, variant2::in_place_index_t<I::value>, V >,
483  
    std::is_constructible< T, variant2::in_place_index_t<I::value>, V >,
485  
        mp11::mp_int<2>,
484  
        mp11::mp_int<2>,
486  
#ifndef BOOST_NO_CXX17_HDR_VARIANT
485  
#ifndef BOOST_NO_CXX17_HDR_VARIANT
487  
    std::is_constructible< T, std::in_place_index_t<I::value>, V >,
486  
    std::is_constructible< T, std::in_place_index_t<I::value>, V >,
488  
        mp11::mp_int<1>,
487  
        mp11::mp_int<1>,
489  
#endif // BOOST_NO_CXX17_HDR_VARIANT
488  
#endif // BOOST_NO_CXX17_HDR_VARIANT
490  
    mp11::mp_true,
489  
    mp11::mp_true,
491  
        mp11::mp_int<0> >;
490  
        mp11::mp_int<0> >;
492  

491  

493  
template< class T, class I, class V >
492  
template< class T, class I, class V >
494  
T
493  
T
495  
initialize_variant( V&& v, mp11::mp_int<0> )
494  
initialize_variant( V&& v, mp11::mp_int<0> )
496  
{
495  
{
497  
    T t;
496  
    T t;
498  
    t.template emplace<I::value>( std::move(v) );
497  
    t.template emplace<I::value>( std::move(v) );
499  
    return t;
498  
    return t;
500  
}
499  
}
501  

500  

502  
template< class T, class I, class V >
501  
template< class T, class I, class V >
503  
T
502  
T
504  
initialize_variant( V&& v, mp11::mp_int<2> )
503  
initialize_variant( V&& v, mp11::mp_int<2> )
505  
{
504  
{
506  
    return T( variant2::in_place_index_t<I::value>(), std::move(v) );
505  
    return T( variant2::in_place_index_t<I::value>(), std::move(v) );
507  
}
506  
}
508  

507  

509  
#ifndef BOOST_NO_CXX17_HDR_VARIANT
508  
#ifndef BOOST_NO_CXX17_HDR_VARIANT
510  
template< class T, class I, class V >
509  
template< class T, class I, class V >
511  
T
510  
T
512  
initialize_variant( V&& v, mp11::mp_int<1> )
511  
initialize_variant( V&& v, mp11::mp_int<1> )
513  
{
512  
{
514  
    return T( std::in_place_index_t<I::value>(), std::move(v) );
513  
    return T( std::in_place_index_t<I::value>(), std::move(v) );
515  
}
514  
}
516  
#endif // BOOST_NO_CXX17_HDR_VARIANT
515  
#endif // BOOST_NO_CXX17_HDR_VARIANT
517  

516  

518  

517  

519  
template< class T, class Ctx >
518  
template< class T, class Ctx >
520  
struct alternative_converter
519  
struct alternative_converter
521  
{
520  
{
522  
    system::result<T>& res;
521  
    system::result<T>& res;
523  
    value const& jv;
522  
    value const& jv;
524  
    Ctx const& ctx;
523  
    Ctx const& ctx;
525  

524  

526  
    template< class I >
525  
    template< class I >
527  
    void operator()( I ) const
526  
    void operator()( I ) const
528  
    {
527  
    {
529  
        if( res )
528  
        if( res )
530  
            return;
529  
            return;
531  

530  

532  
        using V = mp11::mp_at<T, I>;
531  
        using V = mp11::mp_at<T, I>;
533  
        auto attempt = try_value_to<V>(jv, ctx);
532  
        auto attempt = try_value_to<V>(jv, ctx);
534  
        if( attempt )
533  
        if( attempt )
535  
        {
534  
        {
536  
            using cat = variant_construction_category<T, V, I>;
535  
            using cat = variant_construction_category<T, V, I>;
537  
            res = initialize_variant<T, I>( std::move(*attempt), cat() );
536  
            res = initialize_variant<T, I>( std::move(*attempt), cat() );
538  
        }
537  
        }
539  
    }
538  
    }
540  
};
539  
};
541  

540  

542  
template< class T, class Ctx >
541  
template< class T, class Ctx >
543  
system::result<T>
542  
system::result<T>
544  
value_to_impl(
543  
value_to_impl(
545  
    variant_conversion_tag,
544  
    variant_conversion_tag,
546  
    try_value_to_tag<T>,
545  
    try_value_to_tag<T>,
547  
    value const& jv,
546  
    value const& jv,
548  
    Ctx const& ctx)
547  
    Ctx const& ctx)
549  
{
548  
{
550  
    system::error_code ec;
549  
    system::error_code ec;
551  
    BOOST_JSON_FAIL(ec, error::exhausted_variants);
550  
    BOOST_JSON_FAIL(ec, error::exhausted_variants);
552  

551  

553  
    using Is = mp11::mp_iota< mp11::mp_size<T> >;
552  
    using Is = mp11::mp_iota< mp11::mp_size<T> >;
554  

553  

555  
    system::result<T> res = {system::in_place_error, ec};
554  
    system::result<T> res = {system::in_place_error, ec};
556  
    mp11::mp_for_each<Is>( alternative_converter<T, Ctx>{res, jv, ctx} );
555  
    mp11::mp_for_each<Is>( alternative_converter<T, Ctx>{res, jv, ctx} );
557  
    return res;
556  
    return res;
558  
}
557  
}
559  

558  

560  
template< class T, class Ctx >
559  
template< class T, class Ctx >
561  
system::result<T>
560  
system::result<T>
562  
value_to_impl(
561  
value_to_impl(
563  
    path_conversion_tag, try_value_to_tag<T>, value const& jv, Ctx const& )
562  
    path_conversion_tag, try_value_to_tag<T>, value const& jv, Ctx const& )
564  
{
563  
{
565  
    auto str = jv.if_string();
564  
    auto str = jv.if_string();
566  
    if( !str )
565  
    if( !str )
567  
    {
566  
    {
568  
        system::error_code ec;
567  
        system::error_code ec;
569  
        BOOST_JSON_FAIL(ec, error::not_string);
568  
        BOOST_JSON_FAIL(ec, error::not_string);
570  
        return {boost::system::in_place_error, ec};
569  
        return {boost::system::in_place_error, ec};
571  
    }
570  
    }
572  

571  

573  
    string_view sv = str->subview();
572  
    string_view sv = str->subview();
574  
    return {boost::system::in_place_value, T( sv.begin(), sv.end() )};
573  
    return {boost::system::in_place_value, T( sv.begin(), sv.end() )};
575  
}
574  
}
576  

575  

577  
//----------------------------------------------------------
576  
//----------------------------------------------------------
578  
// User-provided conversions; throwing -> throwing
577  
// User-provided conversions; throwing -> throwing
579  
template< class T, class Ctx >
578  
template< class T, class Ctx >
580  
mp11::mp_if< mp11::mp_valid<has_user_conversion_to_impl, T>, T >
579  
mp11::mp_if< mp11::mp_valid<has_user_conversion_to_impl, T>, T >
581  
value_to_impl(
580  
value_to_impl(
582  
    user_conversion_tag, value_to_tag<T> tag, value const& jv, Ctx const&)
581  
    user_conversion_tag, value_to_tag<T> tag, value const& jv, Ctx const&)
583  
{
582  
{
584  
    return tag_invoke(tag, jv);
583  
    return tag_invoke(tag, jv);
585  
}
584  
}
586  

585  

587  
template<
586  
template<
588  
    class T,
587  
    class T,
589  
    class Ctx,
588  
    class Ctx,
590  
    class Sup = supported_context<Ctx, T, value_to_conversion>
589  
    class Sup = supported_context<Ctx, T, value_to_conversion>
591  
>
590  
>
592  
mp11::mp_if<
591  
mp11::mp_if<
593  
    mp11::mp_valid< has_context_conversion_to_impl, typename Sup::type, T>, T >
592  
    mp11::mp_valid< has_context_conversion_to_impl, typename Sup::type, T>, T >
594  
value_to_impl(
593  
value_to_impl(
595  
    context_conversion_tag,
594  
    context_conversion_tag,
596  
    value_to_tag<T> tag,
595  
    value_to_tag<T> tag,
597  
    value const& jv,
596  
    value const& jv,
598  
    Ctx const& ctx )
597  
    Ctx const& ctx )
599  
{
598  
{
600  
    return tag_invoke( tag, jv, Sup::get(ctx) );
599  
    return tag_invoke( tag, jv, Sup::get(ctx) );
601  
}
600  
}
602  

601  

603  
template<
602  
template<
604  
    class T,
603  
    class T,
605  
    class Ctx,
604  
    class Ctx,
606  
    class Sup = supported_context<Ctx, T, value_to_conversion>
605  
    class Sup = supported_context<Ctx, T, value_to_conversion>
607  
>
606  
>
608  
mp11::mp_if<
607  
mp11::mp_if<
609  
    mp11::mp_valid<
608  
    mp11::mp_valid<
610  
        has_full_context_conversion_to_impl, typename Sup::type, T>,
609  
        has_full_context_conversion_to_impl, typename Sup::type, T>,
611  
    T>
610  
    T>
612  
value_to_impl(
611  
value_to_impl(
613  
    full_context_conversion_tag,
612  
    full_context_conversion_tag,
614  
    value_to_tag<T> tag,
613  
    value_to_tag<T> tag,
615  
    value const& jv,
614  
    value const& jv,
616  
    Ctx const& ctx )
615  
    Ctx const& ctx )
617  
{
616  
{
618  
    return tag_invoke( tag, jv, Sup::get(ctx), ctx );
617  
    return tag_invoke( tag, jv, Sup::get(ctx), ctx );
619  
}
618  
}
620  

619  

621  
//----------------------------------------------------------
620  
//----------------------------------------------------------
622  
// User-provided conversions; throwing -> nonthrowing
621  
// User-provided conversions; throwing -> nonthrowing
623  
template< class T, class Ctx >
622  
template< class T, class Ctx >
624  
mp11::mp_if_c< !mp11::mp_valid<has_user_conversion_to_impl, T>::value, T>
623  
mp11::mp_if_c< !mp11::mp_valid<has_user_conversion_to_impl, T>::value, T>
625  
value_to_impl(
624  
value_to_impl(
626  
    user_conversion_tag, value_to_tag<T>, value const& jv, Ctx const& )
625  
    user_conversion_tag, value_to_tag<T>, value const& jv, Ctx const& )
627  
{
626  
{
628  
    auto res = tag_invoke(try_value_to_tag<T>(), jv);
627  
    auto res = tag_invoke(try_value_to_tag<T>(), jv);
629  
    if( res.has_error() )
628  
    if( res.has_error() )
630  
        throw_system_error( res.error() );
629  
        throw_system_error( res.error() );
631  
    return std::move(*res);
630  
    return std::move(*res);
632  
}
631  
}
633  

632  

634  
template<
633  
template<
635  
    class T,
634  
    class T,
636  
    class Ctx,
635  
    class Ctx,
637  
    class Sup = supported_context<Ctx, T, value_to_conversion>
636  
    class Sup = supported_context<Ctx, T, value_to_conversion>
638  
>
637  
>
639  
mp11::mp_if_c<
638  
mp11::mp_if_c<
640  
    !mp11::mp_valid<
639  
    !mp11::mp_valid<
641  
        has_context_conversion_to_impl, typename Sup::type, T>::value,
640  
        has_context_conversion_to_impl, typename Sup::type, T>::value,
642  
    T>
641  
    T>
643  
value_to_impl(
642  
value_to_impl(
644  
    context_conversion_tag, value_to_tag<T>, value const& jv, Ctx const& ctx )
643  
    context_conversion_tag, value_to_tag<T>, value const& jv, Ctx const& ctx )
645  
{
644  
{
646  
    auto res = tag_invoke( try_value_to_tag<T>(), jv, Sup::get(ctx) );
645  
    auto res = tag_invoke( try_value_to_tag<T>(), jv, Sup::get(ctx) );
647  
    if( res.has_error() )
646  
    if( res.has_error() )
648  
        throw_system_error( res.error() );
647  
        throw_system_error( res.error() );
649  
    return std::move(*res);
648  
    return std::move(*res);
650  
}
649  
}
651  

650  

652  
template<
651  
template<
653  
    class T,
652  
    class T,
654  
    class Ctx,
653  
    class Ctx,
655  
    class Sup = supported_context<Ctx, T, value_to_conversion>
654  
    class Sup = supported_context<Ctx, T, value_to_conversion>
656  
>
655  
>
657  
mp11::mp_if_c<
656  
mp11::mp_if_c<
658  
    !mp11::mp_valid<
657  
    !mp11::mp_valid<
659  
        has_full_context_conversion_to_impl, typename Sup::type, T>::value,
658  
        has_full_context_conversion_to_impl, typename Sup::type, T>::value,
660  
    T>
659  
    T>
661  
value_to_impl(
660  
value_to_impl(
662  
    full_context_conversion_tag,
661  
    full_context_conversion_tag,
663  
    value_to_tag<T>,
662  
    value_to_tag<T>,
664  
    value const& jv,
663  
    value const& jv,
665  
    Ctx const& ctx )
664  
    Ctx const& ctx )
666  
{
665  
{
667  
    auto res = tag_invoke(try_value_to_tag<T>(), jv, Sup::get(ctx), ctx);
666  
    auto res = tag_invoke(try_value_to_tag<T>(), jv, Sup::get(ctx), ctx);
668  
    if( res.has_error() )
667  
    if( res.has_error() )
669  
        throw_system_error( res.error() );
668  
        throw_system_error( res.error() );
670  
    return std::move(*res);
669  
    return std::move(*res);
671  
}
670  
}
672  

671  

673  
//----------------------------------------------------------
672  
//----------------------------------------------------------
674  
// User-provided conversions; nonthrowing -> nonthrowing
673  
// User-provided conversions; nonthrowing -> nonthrowing
675  
template< class T, class Ctx >
674  
template< class T, class Ctx >
676  
mp11::mp_if<
675  
mp11::mp_if<
677  
    mp11::mp_valid<
676  
    mp11::mp_valid<
678  
        has_nonthrowing_user_conversion_to_impl, T>, system::result<T> >
677  
        has_nonthrowing_user_conversion_to_impl, T>, system::result<T> >
679  
value_to_impl(
678  
value_to_impl(
680  
    user_conversion_tag, try_value_to_tag<T>, value const& jv, Ctx const& )
679  
    user_conversion_tag, try_value_to_tag<T>, value const& jv, Ctx const& )
681  
{
680  
{
682  
    return tag_invoke(try_value_to_tag<T>(), jv);
681  
    return tag_invoke(try_value_to_tag<T>(), jv);
683  
}
682  
}
684  

683  

685  
template<
684  
template<
686  
    class T,
685  
    class T,
687  
    class Ctx,
686  
    class Ctx,
688  
    class Sup = supported_context<Ctx, T, value_to_conversion>
687  
    class Sup = supported_context<Ctx, T, value_to_conversion>
689  
>
688  
>
690  
mp11::mp_if<
689  
mp11::mp_if<
691  
    mp11::mp_valid<
690  
    mp11::mp_valid<
692  
        has_nonthrowing_context_conversion_to_impl, typename Sup::type, T>,
691  
        has_nonthrowing_context_conversion_to_impl, typename Sup::type, T>,
693  
    system::result<T> >
692  
    system::result<T> >
694  
value_to_impl(
693  
value_to_impl(
695  
    context_conversion_tag,
694  
    context_conversion_tag,
696  
    try_value_to_tag<T> tag,
695  
    try_value_to_tag<T> tag,
697  
    value const& jv,
696  
    value const& jv,
698  
    Ctx const& ctx )
697  
    Ctx const& ctx )
699  
{
698  
{
700  
    return tag_invoke( tag, jv, Sup::get(ctx) );
699  
    return tag_invoke( tag, jv, Sup::get(ctx) );
701  
}
700  
}
702  

701  

703  
template<
702  
template<
704  
    class T,
703  
    class T,
705  
    class Ctx,
704  
    class Ctx,
706  
    class Sup = supported_context<Ctx, T, value_to_conversion>
705  
    class Sup = supported_context<Ctx, T, value_to_conversion>
707  
>
706  
>
708  
mp11::mp_if<
707  
mp11::mp_if<
709  
    mp11::mp_valid<
708  
    mp11::mp_valid<
710  
        has_nonthrowing_full_context_conversion_to_impl,
709  
        has_nonthrowing_full_context_conversion_to_impl,
711  
        typename Sup::type,
710  
        typename Sup::type,
712  
        T>,
711  
        T>,
713  
    system::result<T> >
712  
    system::result<T> >
714  
value_to_impl(
713  
value_to_impl(
715  
    full_context_conversion_tag,
714  
    full_context_conversion_tag,
716  
    try_value_to_tag<T> tag,
715  
    try_value_to_tag<T> tag,
717  
    value const& jv,
716  
    value const& jv,
718  
    Ctx const& ctx )
717  
    Ctx const& ctx )
719  
{
718  
{
720  
    return tag_invoke( tag, jv, Sup::get(ctx), ctx );
719  
    return tag_invoke( tag, jv, Sup::get(ctx), ctx );
721  
}
720  
}
722  

721  

723  
//----------------------------------------------------------
722  
//----------------------------------------------------------
724  
// User-provided conversions; nonthrowing -> throwing
723  
// User-provided conversions; nonthrowing -> throwing
725  

724  

726  
template< class T, class... Args >
725  
template< class T, class... Args >
727  
system::result<T>
726  
system::result<T>
728  
wrap_conversion_exceptions( value_to_tag<T>, Args&& ... args )
727  
wrap_conversion_exceptions( value_to_tag<T>, Args&& ... args )
729  
{
728  
{
730  
#ifndef BOOST_NO_EXCEPTIONS
729  
#ifndef BOOST_NO_EXCEPTIONS
731  
    try
730  
    try
732  
    {
731  
    {
733  
#endif
732  
#endif
734  
        return {
733  
        return {
735  
            boost::system::in_place_value,
734  
            boost::system::in_place_value,
736  
            tag_invoke( value_to_tag<T>(), static_cast<Args&&>(args)... )};
735  
            tag_invoke( value_to_tag<T>(), static_cast<Args&&>(args)... )};
737  
#ifndef BOOST_NO_EXCEPTIONS
736  
#ifndef BOOST_NO_EXCEPTIONS
738  
    }
737  
    }
739  
    catch( std::bad_alloc const&)
738  
    catch( std::bad_alloc const&)
740  
    {
739  
    {
741  
        throw;
740  
        throw;
742  
    }
741  
    }
743  
    catch( system::system_error const& e)
742  
    catch( system::system_error const& e)
744  
    {
743  
    {
745  
        return {boost::system::in_place_error, e.code()};
744  
        return {boost::system::in_place_error, e.code()};
746  
    }
745  
    }
747  
    catch( ... )
746  
    catch( ... )
748  
    {
747  
    {
749  
        system::error_code ec;
748  
        system::error_code ec;
750  
        BOOST_JSON_FAIL(ec, error::exception);
749  
        BOOST_JSON_FAIL(ec, error::exception);
751  
        return {boost::system::in_place_error, ec};
750  
        return {boost::system::in_place_error, ec};
752  
    }
751  
    }
753  
#endif
752  
#endif
754  
}
753  
}
755  

754  

756  
template< class T, class Ctx >
755  
template< class T, class Ctx >
757  
mp11::mp_if_c<
756  
mp11::mp_if_c<
758  
    !mp11::mp_valid<has_nonthrowing_user_conversion_to_impl, T>::value,
757  
    !mp11::mp_valid<has_nonthrowing_user_conversion_to_impl, T>::value,
759  
    system::result<T> >
758  
    system::result<T> >
760  
value_to_impl(
759  
value_to_impl(
761  
    user_conversion_tag, try_value_to_tag<T>, value const& jv, Ctx const& )
760  
    user_conversion_tag, try_value_to_tag<T>, value const& jv, Ctx const& )
762  
{
761  
{
763  
    return wrap_conversion_exceptions(value_to_tag<T>(), jv);
762  
    return wrap_conversion_exceptions(value_to_tag<T>(), jv);
764  
}
763  
}
765  

764  

766  
template<
765  
template<
767  
    class T,
766  
    class T,
768  
    class Ctx,
767  
    class Ctx,
769  
    class Sup = supported_context<Ctx, T, value_to_conversion>
768  
    class Sup = supported_context<Ctx, T, value_to_conversion>
770  
>
769  
>
771  
mp11::mp_if_c<
770  
mp11::mp_if_c<
772  
    !mp11::mp_valid<
771  
    !mp11::mp_valid<
773  
        has_nonthrowing_context_conversion_to_impl,
772  
        has_nonthrowing_context_conversion_to_impl,
774  
        typename Sup::type,
773  
        typename Sup::type,
775  
        T>::value,
774  
        T>::value,
776  
    system::result<T> >
775  
    system::result<T> >
777  
value_to_impl(
776  
value_to_impl(
778  
    context_conversion_tag,
777  
    context_conversion_tag,
779  
    try_value_to_tag<T>,
778  
    try_value_to_tag<T>,
780  
    value const& jv,
779  
    value const& jv,
781  
    Ctx const& ctx )
780  
    Ctx const& ctx )
782  
{
781  
{
783  
    return wrap_conversion_exceptions( value_to_tag<T>(), jv, Sup::get(ctx) );
782  
    return wrap_conversion_exceptions( value_to_tag<T>(), jv, Sup::get(ctx) );
784  
}
783  
}
785  

784  

786  
template<
785  
template<
787  
    class T,
786  
    class T,
788  
    class Ctx,
787  
    class Ctx,
789  
    class Sup = supported_context<Ctx, T, value_to_conversion>
788  
    class Sup = supported_context<Ctx, T, value_to_conversion>
790  
>
789  
>
791  
mp11::mp_if_c<
790  
mp11::mp_if_c<
792  
    !mp11::mp_valid<
791  
    !mp11::mp_valid<
793  
        has_nonthrowing_full_context_conversion_to_impl,
792  
        has_nonthrowing_full_context_conversion_to_impl,
794  
        typename Sup::type,
793  
        typename Sup::type,
795  
        T>::value,
794  
        T>::value,
796  
    system::result<T> >
795  
    system::result<T> >
797  
value_to_impl(
796  
value_to_impl(
798  
    full_context_conversion_tag,
797  
    full_context_conversion_tag,
799  
    try_value_to_tag<T>,
798  
    try_value_to_tag<T>,
800  
    value const& jv,
799  
    value const& jv,
801  
    Ctx const& ctx )
800  
    Ctx const& ctx )
802  
{
801  
{
803  
    return wrap_conversion_exceptions(
802  
    return wrap_conversion_exceptions(
804  
        value_to_tag<T>(), jv, Sup::get(ctx), ctx);
803  
        value_to_tag<T>(), jv, Sup::get(ctx), ctx);
805  
}
804  
}
806  

805  

807  
// no suitable conversion implementation
806  
// no suitable conversion implementation
808  
template< class T, class Ctx >
807  
template< class T, class Ctx >
809  
T
808  
T
810  
value_to_impl( no_conversion_tag, value_to_tag<T>, value const&, Ctx const& )
809  
value_to_impl( no_conversion_tag, value_to_tag<T>, value const&, Ctx const& )
811  
{
810  
{
812  
    static_assert(
811  
    static_assert(
813  
        !std::is_same<T, T>::value,
812  
        !std::is_same<T, T>::value,
814  
        "No suitable tag_invoke overload found for the type");
813  
        "No suitable tag_invoke overload found for the type");
815  
}
814  
}
816  

815  

817  
// generic wrapper over non-throwing implementations
816  
// generic wrapper over non-throwing implementations
818  
template< class Impl, class T, class Ctx >
817  
template< class Impl, class T, class Ctx >
819  
T
818  
T
820  
value_to_impl( Impl impl, value_to_tag<T>, value const& jv, Ctx const& ctx )
819  
value_to_impl( Impl impl, value_to_tag<T>, value const& jv, Ctx const& ctx )
821  
{
820  
{
822  
    return value_to_impl(impl, try_value_to_tag<T>(), jv, ctx).value();
821  
    return value_to_impl(impl, try_value_to_tag<T>(), jv, ctx).value();
823  
}
822  
}
824  

823  

825  
template< class Ctx, class T >
824  
template< class Ctx, class T >
826  
using value_to_category = conversion_category<
825  
using value_to_category = conversion_category<
827  
    Ctx, T, value_to_conversion >;
826  
    Ctx, T, value_to_conversion >;
828  

827  

829  
} // detail
828  
} // detail
830  

829  

831  
#ifndef BOOST_NO_CXX17_HDR_OPTIONAL
830  
#ifndef BOOST_NO_CXX17_HDR_OPTIONAL
832  
inline
831  
inline
833  
system::result<std::nullopt_t>
832  
system::result<std::nullopt_t>
834  
tag_invoke(
833  
tag_invoke(
835  
    try_value_to_tag<std::nullopt_t>,
834  
    try_value_to_tag<std::nullopt_t>,
836  
    value const& jv)
835  
    value const& jv)
837  
{
836  
{
838  
    if( jv.is_null() )
837  
    if( jv.is_null() )
839  
        return std::nullopt;
838  
        return std::nullopt;
840  
    system::error_code ec;
839  
    system::error_code ec;
841  
    BOOST_JSON_FAIL(ec, error::not_null);
840  
    BOOST_JSON_FAIL(ec, error::not_null);
842  
    return ec;
841  
    return ec;
843  
}
842  
}
844  
#endif
843  
#endif
845  

844  

846  
} // namespace json
845  
} // namespace json
847  
} // namespace boost
846  
} // namespace boost
848  

847  

849  
#endif
848  
#endif