value_to.hpp 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850
  1. //
  2. // Copyright (c) 2019 Vinnie Falco (vinnie.falco@gmail.com)
  3. // Copyright (c) 2020 Krystian Stasiowski (sdkrystian@gmail.com)
  4. // Copyright (c) 2021 Dmitry Arkhipov (grisumbras@gmail.com)
  5. //
  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)
  8. //
  9. // Official repository: https://github.com/boostorg/json
  10. //
  11. #ifndef BOOST_JSON_DETAIL_VALUE_TO_HPP
  12. #define BOOST_JSON_DETAIL_VALUE_TO_HPP
  13. #include <boost/core/detail/static_assert.hpp>
  14. #include <boost/json/value.hpp>
  15. #include <boost/json/conversion.hpp>
  16. #include <boost/json/result_for.hpp>
  17. #include <boost/describe/enum_from_string.hpp>
  18. #ifndef BOOST_NO_CXX17_HDR_OPTIONAL
  19. # include <optional>
  20. #endif
  21. namespace boost {
  22. namespace json {
  23. namespace detail {
  24. template<class T>
  25. using has_reserve_member_helper = decltype(std::declval<T&>().reserve(0));
  26. template<class T>
  27. using has_reserve_member = mp11::mp_valid<has_reserve_member_helper, T>;
  28. template<class T>
  29. using reserve_implementation = mp11::mp_cond<
  30. is_tuple_like<T>, mp11::mp_int<2>,
  31. has_reserve_member<T>, mp11::mp_int<1>,
  32. mp11::mp_true, mp11::mp_int<0>>;
  33. template<class T>
  34. error
  35. try_reserve(
  36. T&,
  37. std::size_t size,
  38. mp11::mp_int<2>)
  39. {
  40. constexpr std::size_t N = std::tuple_size<remove_cvref<T>>::value;
  41. if ( N != size )
  42. return error::size_mismatch;
  43. return error();
  44. }
  45. template<typename T>
  46. error
  47. try_reserve(
  48. T& cont,
  49. std::size_t size,
  50. mp11::mp_int<1>)
  51. {
  52. cont.reserve(size);
  53. return error();
  54. }
  55. template<typename T>
  56. error
  57. try_reserve(
  58. T&,
  59. std::size_t,
  60. mp11::mp_int<0>)
  61. {
  62. return error();
  63. }
  64. // identity conversion
  65. template< class Ctx >
  66. system::result<value>
  67. value_to_impl(
  68. value_conversion_tag,
  69. try_value_to_tag<value>,
  70. value const& jv,
  71. Ctx const& )
  72. {
  73. return jv;
  74. }
  75. template< class Ctx >
  76. value
  77. value_to_impl(
  78. value_conversion_tag, value_to_tag<value>, value const& jv, Ctx const& )
  79. {
  80. return jv;
  81. }
  82. // object
  83. template< class Ctx >
  84. system::result<object>
  85. value_to_impl(
  86. object_conversion_tag,
  87. try_value_to_tag<object>,
  88. value const& jv,
  89. Ctx const& )
  90. {
  91. object const* obj = jv.if_object();
  92. if( obj )
  93. return *obj;
  94. system::error_code ec;
  95. BOOST_JSON_FAIL(ec, error::not_object);
  96. return ec;
  97. }
  98. // array
  99. template< class Ctx >
  100. system::result<array>
  101. value_to_impl(
  102. array_conversion_tag,
  103. try_value_to_tag<array>,
  104. value const& jv,
  105. Ctx const& )
  106. {
  107. array const* arr = jv.if_array();
  108. if( arr )
  109. return *arr;
  110. system::error_code ec;
  111. BOOST_JSON_FAIL(ec, error::not_array);
  112. return ec;
  113. }
  114. // string
  115. template< class Ctx >
  116. system::result<string>
  117. value_to_impl(
  118. string_conversion_tag,
  119. try_value_to_tag<string>,
  120. value const& jv,
  121. Ctx const& )
  122. {
  123. string const* str = jv.if_string();
  124. if( str )
  125. return *str;
  126. system::error_code ec;
  127. BOOST_JSON_FAIL(ec, error::not_string);
  128. return ec;
  129. }
  130. // bool
  131. template< class Ctx >
  132. system::result<bool>
  133. value_to_impl(
  134. bool_conversion_tag, try_value_to_tag<bool>, value const& jv, Ctx const& )
  135. {
  136. auto b = jv.if_bool();
  137. if( b )
  138. return *b;
  139. system::error_code ec;
  140. BOOST_JSON_FAIL(ec, error::not_bool);
  141. return {boost::system::in_place_error, ec};
  142. }
  143. // integral and floating point
  144. template< class T, class Ctx >
  145. system::result<T>
  146. value_to_impl(
  147. number_conversion_tag, try_value_to_tag<T>, value const& jv, Ctx const& )
  148. {
  149. system::error_code ec;
  150. auto const n = jv.to_number<T>(ec);
  151. if( ec.failed() )
  152. return {boost::system::in_place_error, ec};
  153. return {boost::system::in_place_value, n};
  154. }
  155. // null-like conversion
  156. template< class T, class Ctx >
  157. system::result<T>
  158. value_to_impl(
  159. null_like_conversion_tag,
  160. try_value_to_tag<T>,
  161. value const& jv,
  162. Ctx const& )
  163. {
  164. if( jv.is_null() )
  165. return {boost::system::in_place_value, T{}};
  166. system::error_code ec;
  167. BOOST_JSON_FAIL(ec, error::not_null);
  168. return {boost::system::in_place_error, ec};
  169. }
  170. // string-like types
  171. template< class T, class Ctx >
  172. system::result<T>
  173. value_to_impl(
  174. string_like_conversion_tag,
  175. try_value_to_tag<T>,
  176. value const& jv,
  177. Ctx const& )
  178. {
  179. auto str = jv.if_string();
  180. if( str )
  181. return {boost::system::in_place_value, T(str->subview())};
  182. system::error_code ec;
  183. BOOST_JSON_FAIL(ec, error::not_string);
  184. return {boost::system::in_place_error, ec};
  185. }
  186. // map-like containers
  187. template< class T, class Ctx >
  188. system::result<T>
  189. value_to_impl(
  190. map_like_conversion_tag,
  191. try_value_to_tag<T>,
  192. value const& jv,
  193. Ctx const& ctx )
  194. {
  195. object const* obj = jv.if_object();
  196. if( !obj )
  197. {
  198. system::error_code ec;
  199. BOOST_JSON_FAIL(ec, error::not_object);
  200. return {boost::system::in_place_error, ec};
  201. }
  202. T res;
  203. error const e = detail::try_reserve(
  204. res, obj->size(), reserve_implementation<T>());
  205. if( e != error() )
  206. {
  207. system::error_code ec;
  208. BOOST_JSON_FAIL( ec, e );
  209. return {boost::system::in_place_error, ec};
  210. }
  211. auto ins = detail::inserter(res, inserter_implementation<T>());
  212. for( key_value_pair const& kv: *obj )
  213. {
  214. auto elem_res = try_value_to<mapped_type<T>>( kv.value(), ctx );
  215. if( elem_res.has_error() )
  216. return {boost::system::in_place_error, elem_res.error()};
  217. *ins++ = value_type<T>{
  218. key_type<T>(kv.key()),
  219. std::move(*elem_res)};
  220. }
  221. return res;
  222. }
  223. // all other containers
  224. template< class T, class Ctx >
  225. system::result<T>
  226. value_to_impl(
  227. sequence_conversion_tag,
  228. try_value_to_tag<T>,
  229. value const& jv,
  230. Ctx const& ctx )
  231. {
  232. array const* arr = jv.if_array();
  233. if( !arr )
  234. {
  235. system::error_code ec;
  236. BOOST_JSON_FAIL(ec, error::not_array);
  237. return {boost::system::in_place_error, ec};
  238. }
  239. T result;
  240. error const e = detail::try_reserve(
  241. result, arr->size(), reserve_implementation<T>());
  242. if( e != error() )
  243. {
  244. system::error_code ec;
  245. BOOST_JSON_FAIL( ec, e );
  246. return {boost::system::in_place_error, ec};
  247. }
  248. auto ins = detail::inserter(result, inserter_implementation<T>());
  249. for( value const& val: *arr )
  250. {
  251. auto elem_res = try_value_to<value_type<T>>( val, ctx );
  252. if( elem_res.has_error() )
  253. return {boost::system::in_place_error, elem_res.error()};
  254. *ins++ = std::move(*elem_res);
  255. }
  256. return result;
  257. }
  258. // tuple-like types
  259. template< class T, class Ctx >
  260. system::result<T>
  261. try_make_tuple_elem(value const& jv, Ctx const& ctx, system::error_code& ec)
  262. {
  263. if( ec.failed() )
  264. return {boost::system::in_place_error, ec};
  265. auto result = try_value_to<T>( jv, ctx );
  266. ec = result.error();
  267. return result;
  268. }
  269. template <class T, class Ctx, std::size_t... Is>
  270. system::result<T>
  271. try_make_tuple_like(
  272. array const& arr, Ctx const& ctx, boost::mp11::index_sequence<Is...>)
  273. {
  274. system::error_code ec;
  275. auto items = std::make_tuple(
  276. try_make_tuple_elem<
  277. typename std::decay<tuple_element_t<Is, T>>::type >(
  278. arr[Is], ctx, ec)
  279. ...);
  280. #if defined(BOOST_GCC)
  281. # pragma GCC diagnostic push
  282. # pragma GCC diagnostic ignored "-Wmaybe-uninitialized"
  283. #endif
  284. if( ec.failed() )
  285. return {boost::system::in_place_error, ec};
  286. #if defined(BOOST_GCC)
  287. # pragma GCC diagnostic pop
  288. #endif
  289. return {
  290. boost::system::in_place_value, T(std::move(*std::get<Is>(items))...)};
  291. }
  292. template< class T, class Ctx >
  293. system::result<T>
  294. value_to_impl(
  295. tuple_conversion_tag,
  296. try_value_to_tag<T>,
  297. value const& jv,
  298. Ctx const& ctx )
  299. {
  300. system::error_code ec;
  301. array const* arr = jv.if_array();
  302. if( !arr )
  303. {
  304. BOOST_JSON_FAIL(ec, error::not_array);
  305. return {boost::system::in_place_error, ec};
  306. }
  307. constexpr std::size_t N = std::tuple_size<remove_cvref<T>>::value;
  308. if( N != arr->size() )
  309. {
  310. BOOST_JSON_FAIL(ec, error::size_mismatch);
  311. return {boost::system::in_place_error, ec};
  312. }
  313. return try_make_tuple_like<T>(
  314. *arr, ctx, boost::mp11::make_index_sequence<N>());
  315. }
  316. template< class Ctx, class T >
  317. struct to_described_member
  318. {
  319. static_assert(
  320. uniquely_named_members<T>::value,
  321. "The type has several described members with the same name.");
  322. using Ds = described_members<T>;
  323. system::result<T>& res;
  324. object const& obj;
  325. Ctx const& ctx;
  326. template< class I >
  327. void
  328. operator()(I)
  329. {
  330. if( !res )
  331. return;
  332. using D = mp11::mp_at<Ds, I>;
  333. using M = described_member_t<T, D>;
  334. auto const found = obj.find(D::name);
  335. if( found == obj.end() )
  336. {
  337. BOOST_IF_CONSTEXPR( !is_optional_like<M>::value )
  338. {
  339. system::error_code ec;
  340. BOOST_JSON_FAIL(ec, error::size_mismatch);
  341. res = {boost::system::in_place_error, ec};
  342. }
  343. return;
  344. }
  345. #if defined(__GNUC__) && BOOST_GCC_VERSION >= 80000 && BOOST_GCC_VERSION < 11000
  346. # pragma GCC diagnostic push
  347. # pragma GCC diagnostic ignored "-Wunused"
  348. # pragma GCC diagnostic ignored "-Wunused-variable"
  349. #endif
  350. auto member_res = try_value_to<M>( found->value(), ctx );
  351. #if defined(__GNUC__) && BOOST_GCC_VERSION >= 80000 && BOOST_GCC_VERSION < 11000
  352. # pragma GCC diagnostic pop
  353. #endif
  354. if( member_res )
  355. (*res).* D::pointer = std::move(*member_res);
  356. else
  357. res = {boost::system::in_place_error, member_res.error()};
  358. }
  359. };
  360. // described classes
  361. template< class T, class Ctx >
  362. system::result<T>
  363. value_to_impl(
  364. described_class_conversion_tag,
  365. try_value_to_tag<T>,
  366. value const& jv,
  367. Ctx const& ctx )
  368. {
  369. BOOST_CORE_STATIC_ASSERT( std::is_default_constructible<T>::value );
  370. system::result<T> res;
  371. auto* obj = jv.if_object();
  372. if( !obj )
  373. {
  374. system::error_code ec;
  375. BOOST_JSON_FAIL(ec, error::not_object);
  376. res = {boost::system::in_place_error, ec};
  377. return res;
  378. }
  379. to_described_member<Ctx, T> member_converter{res, *obj, ctx};
  380. using Ds = typename decltype(member_converter)::Ds;
  381. constexpr std::size_t N = mp11::mp_size<Ds>::value;
  382. mp11::mp_for_each< mp11::mp_iota_c<N> >(member_converter);
  383. if( !res )
  384. return res;
  385. return res;
  386. }
  387. // described enums
  388. template< class T, class Ctx >
  389. system::result<T>
  390. value_to_impl(
  391. described_enum_conversion_tag,
  392. try_value_to_tag<T>,
  393. value const& jv,
  394. Ctx const& )
  395. {
  396. T val = {};
  397. (void)jv;
  398. #ifdef BOOST_DESCRIBE_CXX14
  399. system::error_code ec;
  400. auto str = jv.if_string();
  401. if( !str )
  402. {
  403. BOOST_JSON_FAIL(ec, error::not_string);
  404. return {system::in_place_error, ec};
  405. }
  406. if( !describe::enum_from_string(str->data(), val) )
  407. {
  408. BOOST_JSON_FAIL(ec, error::unknown_name);
  409. return {system::in_place_error, ec};
  410. }
  411. #endif
  412. return {system::in_place_value, val};
  413. }
  414. // optionals
  415. template< class T, class Ctx >
  416. system::result<T>
  417. value_to_impl(
  418. optional_conversion_tag,
  419. try_value_to_tag<T>,
  420. value const& jv,
  421. Ctx const& ctx)
  422. {
  423. using Inner = value_result_type<T>;
  424. if( jv.is_null() )
  425. return {};
  426. else
  427. return try_value_to<Inner>(jv, ctx);
  428. }
  429. // variants
  430. template< class T, class V, class I >
  431. using variant_construction_category = mp11::mp_cond<
  432. std::is_constructible< T, variant2::in_place_index_t<I::value>, V >,
  433. mp11::mp_int<2>,
  434. #ifndef BOOST_NO_CXX17_HDR_VARIANT
  435. std::is_constructible< T, std::in_place_index_t<I::value>, V >,
  436. mp11::mp_int<1>,
  437. #endif // BOOST_NO_CXX17_HDR_VARIANT
  438. mp11::mp_true,
  439. mp11::mp_int<0> >;
  440. template< class T, class I, class V >
  441. T
  442. initialize_variant( V&& v, mp11::mp_int<0> )
  443. {
  444. T t;
  445. t.template emplace<I::value>( std::move(v) );
  446. return t;
  447. }
  448. template< class T, class I, class V >
  449. T
  450. initialize_variant( V&& v, mp11::mp_int<2> )
  451. {
  452. return T( variant2::in_place_index_t<I::value>(), std::move(v) );
  453. }
  454. #ifndef BOOST_NO_CXX17_HDR_VARIANT
  455. template< class T, class I, class V >
  456. T
  457. initialize_variant( V&& v, mp11::mp_int<1> )
  458. {
  459. return T( std::in_place_index_t<I::value>(), std::move(v) );
  460. }
  461. #endif // BOOST_NO_CXX17_HDR_VARIANT
  462. template< class T, class Ctx >
  463. struct alternative_converter
  464. {
  465. system::result<T>& res;
  466. value const& jv;
  467. Ctx const& ctx;
  468. template< class I >
  469. void operator()( I ) const
  470. {
  471. if( res )
  472. return;
  473. using V = mp11::mp_at<T, I>;
  474. auto attempt = try_value_to<V>(jv, ctx);
  475. if( attempt )
  476. {
  477. using cat = variant_construction_category<T, V, I>;
  478. res = initialize_variant<T, I>( std::move(*attempt), cat() );
  479. }
  480. }
  481. };
  482. template< class T, class Ctx >
  483. system::result<T>
  484. value_to_impl(
  485. variant_conversion_tag,
  486. try_value_to_tag<T>,
  487. value const& jv,
  488. Ctx const& ctx)
  489. {
  490. system::error_code ec;
  491. BOOST_JSON_FAIL(ec, error::exhausted_variants);
  492. using Is = mp11::mp_iota< mp11::mp_size<T> >;
  493. system::result<T> res = {system::in_place_error, ec};
  494. mp11::mp_for_each<Is>( alternative_converter<T, Ctx>{res, jv, ctx} );
  495. return res;
  496. }
  497. template< class T, class Ctx >
  498. system::result<T>
  499. value_to_impl(
  500. path_conversion_tag, try_value_to_tag<T>, value const& jv, Ctx const& )
  501. {
  502. auto str = jv.if_string();
  503. if( !str )
  504. {
  505. system::error_code ec;
  506. BOOST_JSON_FAIL(ec, error::not_string);
  507. return {boost::system::in_place_error, ec};
  508. }
  509. string_view sv = str->subview();
  510. return {boost::system::in_place_value, T( sv.begin(), sv.end() )};
  511. }
  512. //----------------------------------------------------------
  513. // User-provided conversions; throwing -> throwing
  514. template< class T, class Ctx >
  515. mp11::mp_if< mp11::mp_valid<has_user_conversion_to_impl, T>, T >
  516. value_to_impl(
  517. user_conversion_tag, value_to_tag<T> tag, value const& jv, Ctx const&)
  518. {
  519. return tag_invoke(tag, jv);
  520. }
  521. template<
  522. class T,
  523. class Ctx,
  524. class Sup = supported_context<Ctx, T, value_to_conversion>
  525. >
  526. mp11::mp_if<
  527. mp11::mp_valid< has_context_conversion_to_impl, typename Sup::type, T>, T >
  528. value_to_impl(
  529. context_conversion_tag,
  530. value_to_tag<T> tag,
  531. value const& jv,
  532. Ctx const& ctx )
  533. {
  534. return tag_invoke( tag, jv, Sup::get(ctx) );
  535. }
  536. template<
  537. class T,
  538. class Ctx,
  539. class Sup = supported_context<Ctx, T, value_to_conversion>
  540. >
  541. mp11::mp_if<
  542. mp11::mp_valid<
  543. has_full_context_conversion_to_impl, typename Sup::type, T>,
  544. T>
  545. value_to_impl(
  546. full_context_conversion_tag,
  547. value_to_tag<T> tag,
  548. value const& jv,
  549. Ctx const& ctx )
  550. {
  551. return tag_invoke( tag, jv, Sup::get(ctx), ctx );
  552. }
  553. //----------------------------------------------------------
  554. // User-provided conversions; throwing -> nonthrowing
  555. template< class T, class Ctx >
  556. mp11::mp_if_c< !mp11::mp_valid<has_user_conversion_to_impl, T>::value, T>
  557. value_to_impl(
  558. user_conversion_tag, value_to_tag<T>, value const& jv, Ctx const& )
  559. {
  560. auto res = tag_invoke(try_value_to_tag<T>(), jv);
  561. if( res.has_error() )
  562. throw_system_error( res.error() );
  563. return std::move(*res);
  564. }
  565. template<
  566. class T,
  567. class Ctx,
  568. class Sup = supported_context<Ctx, T, value_to_conversion>
  569. >
  570. mp11::mp_if_c<
  571. !mp11::mp_valid<
  572. has_context_conversion_to_impl, typename Sup::type, T>::value,
  573. T>
  574. value_to_impl(
  575. context_conversion_tag, value_to_tag<T>, value const& jv, Ctx const& ctx )
  576. {
  577. auto res = tag_invoke( try_value_to_tag<T>(), jv, Sup::get(ctx) );
  578. if( res.has_error() )
  579. throw_system_error( res.error() );
  580. return std::move(*res);
  581. }
  582. template<
  583. class T,
  584. class Ctx,
  585. class Sup = supported_context<Ctx, T, value_to_conversion>
  586. >
  587. mp11::mp_if_c<
  588. !mp11::mp_valid<
  589. has_full_context_conversion_to_impl, typename Sup::type, T>::value,
  590. T>
  591. value_to_impl(
  592. full_context_conversion_tag,
  593. value_to_tag<T>,
  594. value const& jv,
  595. Ctx const& ctx )
  596. {
  597. auto res = tag_invoke(try_value_to_tag<T>(), jv, Sup::get(ctx), ctx);
  598. if( res.has_error() )
  599. throw_system_error( res.error() );
  600. return std::move(*res);
  601. }
  602. //----------------------------------------------------------
  603. // User-provided conversions; nonthrowing -> nonthrowing
  604. template< class T, class Ctx >
  605. mp11::mp_if<
  606. mp11::mp_valid<
  607. has_nonthrowing_user_conversion_to_impl, T>, system::result<T> >
  608. value_to_impl(
  609. user_conversion_tag, try_value_to_tag<T>, value const& jv, Ctx const& )
  610. {
  611. return tag_invoke(try_value_to_tag<T>(), jv);
  612. }
  613. template<
  614. class T,
  615. class Ctx,
  616. class Sup = supported_context<Ctx, T, value_to_conversion>
  617. >
  618. mp11::mp_if<
  619. mp11::mp_valid<
  620. has_nonthrowing_context_conversion_to_impl, typename Sup::type, T>,
  621. system::result<T> >
  622. value_to_impl(
  623. context_conversion_tag,
  624. try_value_to_tag<T> tag,
  625. value const& jv,
  626. Ctx const& ctx )
  627. {
  628. return tag_invoke( tag, jv, Sup::get(ctx) );
  629. }
  630. template<
  631. class T,
  632. class Ctx,
  633. class Sup = supported_context<Ctx, T, value_to_conversion>
  634. >
  635. mp11::mp_if<
  636. mp11::mp_valid<
  637. has_nonthrowing_full_context_conversion_to_impl,
  638. typename Sup::type,
  639. T>,
  640. system::result<T> >
  641. value_to_impl(
  642. full_context_conversion_tag,
  643. try_value_to_tag<T> tag,
  644. value const& jv,
  645. Ctx const& ctx )
  646. {
  647. return tag_invoke( tag, jv, Sup::get(ctx), ctx );
  648. }
  649. //----------------------------------------------------------
  650. // User-provided conversions; nonthrowing -> throwing
  651. template< class T, class... Args >
  652. system::result<T>
  653. wrap_conversion_exceptions( value_to_tag<T>, Args&& ... args )
  654. {
  655. #ifndef BOOST_NO_EXCEPTIONS
  656. try
  657. {
  658. #endif
  659. return {
  660. boost::system::in_place_value,
  661. tag_invoke( value_to_tag<T>(), static_cast<Args&&>(args)... )};
  662. #ifndef BOOST_NO_EXCEPTIONS
  663. }
  664. catch( std::bad_alloc const&)
  665. {
  666. throw;
  667. }
  668. catch( system::system_error const& e)
  669. {
  670. return {boost::system::in_place_error, e.code()};
  671. }
  672. catch( ... )
  673. {
  674. system::error_code ec;
  675. BOOST_JSON_FAIL(ec, error::exception);
  676. return {boost::system::in_place_error, ec};
  677. }
  678. #endif
  679. }
  680. template< class T, class Ctx >
  681. mp11::mp_if_c<
  682. !mp11::mp_valid<has_nonthrowing_user_conversion_to_impl, T>::value,
  683. system::result<T> >
  684. value_to_impl(
  685. user_conversion_tag, try_value_to_tag<T>, value const& jv, Ctx const& )
  686. {
  687. return wrap_conversion_exceptions(value_to_tag<T>(), jv);
  688. }
  689. template<
  690. class T,
  691. class Ctx,
  692. class Sup = supported_context<Ctx, T, value_to_conversion>
  693. >
  694. mp11::mp_if_c<
  695. !mp11::mp_valid<
  696. has_nonthrowing_context_conversion_to_impl,
  697. typename Sup::type,
  698. T>::value,
  699. system::result<T> >
  700. value_to_impl(
  701. context_conversion_tag,
  702. try_value_to_tag<T>,
  703. value const& jv,
  704. Ctx const& ctx )
  705. {
  706. return wrap_conversion_exceptions( value_to_tag<T>(), jv, Sup::get(ctx) );
  707. }
  708. template<
  709. class T,
  710. class Ctx,
  711. class Sup = supported_context<Ctx, T, value_to_conversion>
  712. >
  713. mp11::mp_if_c<
  714. !mp11::mp_valid<
  715. has_nonthrowing_full_context_conversion_to_impl,
  716. typename Sup::type,
  717. T>::value,
  718. system::result<T> >
  719. value_to_impl(
  720. full_context_conversion_tag,
  721. try_value_to_tag<T>,
  722. value const& jv,
  723. Ctx const& ctx )
  724. {
  725. return wrap_conversion_exceptions(
  726. value_to_tag<T>(), jv, Sup::get(ctx), ctx);
  727. }
  728. // no suitable conversion implementation
  729. template< class T, class Ctx >
  730. T
  731. value_to_impl( no_conversion_tag, value_to_tag<T>, value const&, Ctx const& )
  732. {
  733. static_assert(
  734. !std::is_same<T, T>::value,
  735. "No suitable tag_invoke overload found for the type");
  736. }
  737. // generic wrapper over non-throwing implementations
  738. template< class Impl, class T, class Ctx >
  739. T
  740. value_to_impl( Impl impl, value_to_tag<T>, value const& jv, Ctx const& ctx )
  741. {
  742. return value_to_impl(impl, try_value_to_tag<T>(), jv, ctx).value();
  743. }
  744. template< class Ctx, class T >
  745. using value_to_category = conversion_category<
  746. Ctx, T, value_to_conversion >;
  747. } // detail
  748. #ifndef BOOST_NO_CXX17_HDR_OPTIONAL
  749. inline
  750. system::result<std::nullopt_t>
  751. tag_invoke(
  752. try_value_to_tag<std::nullopt_t>,
  753. value const& jv)
  754. {
  755. if( jv.is_null() )
  756. return std::nullopt;
  757. system::error_code ec;
  758. BOOST_JSON_FAIL(ec, error::not_null);
  759. return ec;
  760. }
  761. #endif
  762. } // namespace json
  763. } // namespace boost
  764. #endif