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  

 
29 +
template< class Ctx, class T >
 
30 +
using value_to_attrs = conversion_attrs<
 
31 +
    Ctx, remove_cvref<T>, value_to_conversion>;
 
32 +

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

42  

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

55  

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

66  

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

76  

74  

77  

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

89  

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

97  

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

114  

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

131  

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

148  

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

162  

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

175  

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

191  

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

208  

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

225  

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

235  

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

250  

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

267  

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

277  

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

288  

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

296  

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

301  

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

322  

322  
    return {
323  
    return {
323 -
        boost::system::in_place_value,
324 +
        boost::system::in_place_value, T(std::move(*std::get<Is>(items))...)};
324 -
        T{ (std::move(*std::get<Is>(items)))... }
 
325 -
    };
 
326 -
#if defined(BOOST_CLANG)
 
327 -
# pragma clang diagnostic pop
 
328 -
#endif
 
329  
}
325  
}
330  

326  

331  
template< class T, class Ctx >
327  
template< class T, class Ctx >
332  
system::result<T>
328  
system::result<T>
333  
value_to_impl(
329  
value_to_impl(
334  
    tuple_conversion_tag,
330  
    tuple_conversion_tag,
335  
    try_value_to_tag<T>,
331  
    try_value_to_tag<T>,
336  
    value const& jv,
332  
    value const& jv,
337  
    Ctx const& ctx )
333  
    Ctx const& ctx )
338  
{
334  
{
339  
    system::error_code ec;
335  
    system::error_code ec;
340  

336  

341  
    array const* arr = jv.if_array();
337  
    array const* arr = jv.if_array();
342  
    if( !arr )
338  
    if( !arr )
343  
    {
339  
    {
344  
        BOOST_JSON_FAIL(ec, error::not_array);
340  
        BOOST_JSON_FAIL(ec, error::not_array);
345  
        return {boost::system::in_place_error, ec};
341  
        return {boost::system::in_place_error, ec};
346  
    }
342  
    }
347  

343  

348  
    constexpr std::size_t N = std::tuple_size<remove_cvref<T>>::value;
344  
    constexpr std::size_t N = std::tuple_size<remove_cvref<T>>::value;
349  
    if( N != arr->size() )
345  
    if( N != arr->size() )
350  
    {
346  
    {
351  
        BOOST_JSON_FAIL(ec, error::size_mismatch);
347  
        BOOST_JSON_FAIL(ec, error::size_mismatch);
352  
        return {boost::system::in_place_error, ec};
348  
        return {boost::system::in_place_error, ec};
353  
    }
349  
    }
354  

350  

355  
    return try_make_tuple_like<T>(
351  
    return try_make_tuple_like<T>(
356  
        *arr, ctx, boost::mp11::make_index_sequence<N>());
352  
        *arr, ctx, boost::mp11::make_index_sequence<N>());
357  
}
353  
}
358  

354  

359  
template< class Ctx, class T >
355  
template< class Ctx, class T >
360  
struct to_described_member
356  
struct to_described_member
361 -
    static_assert(
 
362 -
        uniquely_named_members<T>::value,
 
363 -
        "The type has several described members with the same name.");
 
364 -

 
365  
{
357  
{
366  
    using Ds = described_members<T>;
358  
    using Ds = described_members<T>;
367  

359  

368  
    system::result<T>& res;
360  
    system::result<T>& res;
369  
    object const& obj;
361  
    object const& obj;
370  
    Ctx const& ctx;
362  
    Ctx const& ctx;
371  

363  

372  
    template< class I >
364  
    template< class I >
373  
    void
365  
    void
374  
    operator()(I)
366  
    operator()(I)
375  
    {
367  
    {
376  
        if( !res )
368  
        if( !res )
377  
            return;
369  
            return;
378  

370  

379  
        using D = mp11::mp_at<Ds, I>;
371  
        using D = mp11::mp_at<Ds, I>;
380  
        using M = described_member_t<T, D>;
372  
        using M = described_member_t<T, D>;
381  

373  

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

385  

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

401  

410  
// described classes
402  
// described classes
411  
template< class T, class Ctx >
403  
template< class T, class Ctx >
412  
system::result<T>
404  
system::result<T>
413  
value_to_impl(
405  
value_to_impl(
414  
    described_class_conversion_tag,
406  
    described_class_conversion_tag,
415  
    try_value_to_tag<T>,
407  
    try_value_to_tag<T>,
416  
    value const& jv,
408  
    value const& jv,
417  
    Ctx const& ctx )
409  
    Ctx const& ctx )
418  
{
410  
{
419 -
    BOOST_CORE_STATIC_ASSERT( std::is_default_constructible<T>::value );
411 +
    BOOST_STATIC_ASSERT( std::is_default_constructible<T>::value );
420  
    system::result<T> res;
412  
    system::result<T> res;
421  

413  

422  
    auto* obj = jv.if_object();
414  
    auto* obj = jv.if_object();
423  
    if( !obj )
415  
    if( !obj )
424  
    {
416  
    {
425  
        system::error_code ec;
417  
        system::error_code ec;
426  
        BOOST_JSON_FAIL(ec, error::not_object);
418  
        BOOST_JSON_FAIL(ec, error::not_object);
427  
        res = {boost::system::in_place_error, ec};
419  
        res = {boost::system::in_place_error, ec};
428  
        return res;
420  
        return res;
429  
    }
421  
    }
430  

422  

431  
    to_described_member<Ctx, T> member_converter{res, *obj, ctx};
423  
    to_described_member<Ctx, T> member_converter{res, *obj, ctx};
432  

424  

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

428  

437  
    if( !res )
429  
    if( !res )
438  
        return res;
430  
        return res;
439  

431  

440  
    return res;
432  
    return res;
441  
}
433  
}
442  

434  

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

448  

457  
    auto str = jv.if_string();
449  
    auto str = jv.if_string();
458  
    if( !str )
450  
    if( !str )
459  
    {
451  
    {
460  
        BOOST_JSON_FAIL(ec, error::not_string);
452  
        BOOST_JSON_FAIL(ec, error::not_string);
461  
        return {system::in_place_error, ec};
453  
        return {system::in_place_error, ec};
462  
    }
454  
    }
463  

455  

464  
    if( !describe::enum_from_string(str->data(), val) )
456  
    if( !describe::enum_from_string(str->data(), val) )
465  
    {
457  
    {
466  
        BOOST_JSON_FAIL(ec, error::unknown_name);
458  
        BOOST_JSON_FAIL(ec, error::unknown_name);
467  
        return {system::in_place_error, ec};
459  
        return {system::in_place_error, ec};
468  
    }
460  
    }
469  
#endif
461  
#endif
470  

462  

471  
    return {system::in_place_value, val};
463  
    return {system::in_place_value, val};
472  
}
464  
}
473  

465  

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

481  

490  
// variants
482  
// variants
491  
template< class T, class V, class I >
483  
template< class T, class V, class I >
492  
using variant_construction_category = mp11::mp_cond<
484  
using variant_construction_category = mp11::mp_cond<
493  
    std::is_constructible< T, variant2::in_place_index_t<I::value>, V >,
485  
    std::is_constructible< T, variant2::in_place_index_t<I::value>, V >,
494  
        mp11::mp_int<2>,
486  
        mp11::mp_int<2>,
495  
#ifndef BOOST_NO_CXX17_HDR_VARIANT
487  
#ifndef BOOST_NO_CXX17_HDR_VARIANT
496  
    std::is_constructible< T, std::in_place_index_t<I::value>, V >,
488  
    std::is_constructible< T, std::in_place_index_t<I::value>, V >,
497  
        mp11::mp_int<1>,
489  
        mp11::mp_int<1>,
498  
#endif // BOOST_NO_CXX17_HDR_VARIANT
490  
#endif // BOOST_NO_CXX17_HDR_VARIANT
499  
    mp11::mp_true,
491  
    mp11::mp_true,
500  
        mp11::mp_int<0> >;
492  
        mp11::mp_int<0> >;
501  

493  

502  
template< class T, class I, class V >
494  
template< class T, class I, class V >
503  
T
495  
T
504  
initialize_variant( V&& v, mp11::mp_int<0> )
496  
initialize_variant( V&& v, mp11::mp_int<0> )
505  
{
497  
{
506  
    T t;
498  
    T t;
507  
    t.template emplace<I::value>( std::move(v) );
499  
    t.template emplace<I::value>( std::move(v) );
508  
    return t;
500  
    return t;
509  
}
501  
}
510  

502  

511  
template< class T, class I, class V >
503  
template< class T, class I, class V >
512  
T
504  
T
513  
initialize_variant( V&& v, mp11::mp_int<2> )
505  
initialize_variant( V&& v, mp11::mp_int<2> )
514  
{
506  
{
515  
    return T( variant2::in_place_index_t<I::value>(), std::move(v) );
507  
    return T( variant2::in_place_index_t<I::value>(), std::move(v) );
516  
}
508  
}
517  

509  

518  
#ifndef BOOST_NO_CXX17_HDR_VARIANT
510  
#ifndef BOOST_NO_CXX17_HDR_VARIANT
519  
template< class T, class I, class V >
511  
template< class T, class I, class V >
520  
T
512  
T
521  
initialize_variant( V&& v, mp11::mp_int<1> )
513  
initialize_variant( V&& v, mp11::mp_int<1> )
522  
{
514  
{
523  
    return T( std::in_place_index_t<I::value>(), std::move(v) );
515  
    return T( std::in_place_index_t<I::value>(), std::move(v) );
524  
}
516  
}
525  
#endif // BOOST_NO_CXX17_HDR_VARIANT
517  
#endif // BOOST_NO_CXX17_HDR_VARIANT
526  

518  

527  

519  

528  
template< class T, class Ctx >
520  
template< class T, class Ctx >
529  
struct alternative_converter
521  
struct alternative_converter
530  
{
522  
{
531  
    system::result<T>& res;
523  
    system::result<T>& res;
532  
    value const& jv;
524  
    value const& jv;
533  
    Ctx const& ctx;
525  
    Ctx const& ctx;
534  

526  

535  
    template< class I >
527  
    template< class I >
536  
    void operator()( I ) const
528  
    void operator()( I ) const
537  
    {
529  
    {
538  
        if( res )
530  
        if( res )
539  
            return;
531  
            return;
540  

532  

541  
        using V = mp11::mp_at<T, I>;
533  
        using V = mp11::mp_at<T, I>;
542  
        auto attempt = try_value_to<V>(jv, ctx);
534  
        auto attempt = try_value_to<V>(jv, ctx);
543  
        if( attempt )
535  
        if( attempt )
544  
        {
536  
        {
545  
            using cat = variant_construction_category<T, V, I>;
537  
            using cat = variant_construction_category<T, V, I>;
546  
            res = initialize_variant<T, I>( std::move(*attempt), cat() );
538  
            res = initialize_variant<T, I>( std::move(*attempt), cat() );
547  
        }
539  
        }
548  
    }
540  
    }
549  
};
541  
};
550  

542  

551  
template< class T, class Ctx >
543  
template< class T, class Ctx >
552  
system::result<T>
544  
system::result<T>
553  
value_to_impl(
545  
value_to_impl(
554  
    variant_conversion_tag,
546  
    variant_conversion_tag,
555  
    try_value_to_tag<T>,
547  
    try_value_to_tag<T>,
556  
    value const& jv,
548  
    value const& jv,
557  
    Ctx const& ctx)
549  
    Ctx const& ctx)
558  
{
550  
{
559  
    system::error_code ec;
551  
    system::error_code ec;
560  
    BOOST_JSON_FAIL(ec, error::exhausted_variants);
552  
    BOOST_JSON_FAIL(ec, error::exhausted_variants);
561  

553  

562  
    using Is = mp11::mp_iota< mp11::mp_size<T> >;
554  
    using Is = mp11::mp_iota< mp11::mp_size<T> >;
563  

555  

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

560  

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

573  

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

577  

586  
//----------------------------------------------------------
578  
//----------------------------------------------------------
587  
// User-provided conversions; throwing -> throwing
579  
// User-provided conversions; throwing -> throwing
588  
template< class T, class Ctx >
580  
template< class T, class Ctx >
589  
mp11::mp_if< mp11::mp_valid<has_user_conversion_to_impl, T>, T >
581  
mp11::mp_if< mp11::mp_valid<has_user_conversion_to_impl, T>, T >
590  
value_to_impl(
582  
value_to_impl(
591  
    user_conversion_tag, value_to_tag<T> tag, value const& jv, Ctx const&)
583  
    user_conversion_tag, value_to_tag<T> tag, value const& jv, Ctx const&)
592  
{
584  
{
593  
    return tag_invoke(tag, jv);
585  
    return tag_invoke(tag, jv);
594  
}
586  
}
595  

587  

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

603  

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

621  

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

634  

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

652  

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

673  

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

685  

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

703  

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

723  

732  
//----------------------------------------------------------
724  
//----------------------------------------------------------
733  
// User-provided conversions; nonthrowing -> throwing
725  
// User-provided conversions; nonthrowing -> throwing
734  

726  

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

756  

765  
template< class T, class Ctx >
757  
template< class T, class Ctx >
766  
mp11::mp_if_c<
758  
mp11::mp_if_c<
767  
    !mp11::mp_valid<has_nonthrowing_user_conversion_to_impl, T>::value,
759  
    !mp11::mp_valid<has_nonthrowing_user_conversion_to_impl, T>::value,
768  
    system::result<T> >
760  
    system::result<T> >
769  
value_to_impl(
761  
value_to_impl(
770  
    user_conversion_tag, try_value_to_tag<T>, value const& jv, Ctx const& )
762  
    user_conversion_tag, try_value_to_tag<T>, value const& jv, Ctx const& )
771  
{
763  
{
772  
    return wrap_conversion_exceptions(value_to_tag<T>(), jv);
764  
    return wrap_conversion_exceptions(value_to_tag<T>(), jv);
773  
}
765  
}
774  

766  

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

786  

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

807  

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

817  

826  
// generic wrapper over non-throwing implementations
818  
// generic wrapper over non-throwing implementations
827  
template< class Impl, class T, class Ctx >
819  
template< class Impl, class T, class Ctx >
828  
T
820  
T
829  
value_to_impl( Impl impl, value_to_tag<T>, value const& jv, Ctx const& ctx )
821  
value_to_impl( Impl impl, value_to_tag<T>, value const& jv, Ctx const& ctx )
830  
{
822  
{
831  
    return value_to_impl(impl, try_value_to_tag<T>(), jv, ctx).value();
823  
    return value_to_impl(impl, try_value_to_tag<T>(), jv, ctx).value();
832 -

 
833 -
template< class Ctx, class T >
 
834 -
using value_to_category = conversion_category<
 
835 -
    Ctx, T, value_to_conversion >;
 
836  
}
824  
}
837  

825  

838  
} // detail
826  
} // detail
839  

827  

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

842  

855  
} // namespace json
843  
} // namespace json
856  
} // namespace boost
844  
} // namespace boost
857  

845  

858  
#endif
846  
#endif