parse_into.hpp 47 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975
  1. //
  2. // Copyright (c) 2019 Vinnie Falco (vinnie.falco@gmail.com)
  3. // Copyright (c) 2020 Krystian Stasiowski (sdkrystian@gmail.com)
  4. //
  5. // Distributed under the Boost Software License, Version 1.0. (See accompanying
  6. // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  7. //
  8. // Official repository: https://github.com/boostorg/json
  9. //
  10. #ifndef BOOST_JSON_DETAIL_PARSE_INTO_HPP
  11. #define BOOST_JSON_DETAIL_PARSE_INTO_HPP
  12. #include <boost/json/detail/config.hpp>
  13. #include <boost/json/error.hpp>
  14. #include <boost/json/conversion.hpp>
  15. #include <boost/describe/enum_from_string.hpp>
  16. #include <vector>
  17. /*
  18. * This file contains the majority of parse_into functionality, specifically
  19. * the implementation of dedicated handlers for different generic categories of
  20. * types.
  21. *
  22. * At the core of parse_into is the specialisation basic_parser<
  23. * detail::into_handler<T> >. detail::into_handler<T> is a handler for
  24. * basic_parser. It directly handles events on_comment_part and on_comment (by
  25. * ignoring them), on_document_begin (by enabling the nested dedicated
  26. * handler), and on_document_end (by disabling the nested handler).
  27. *
  28. * Every other event is handled by the nested handler, which has the type
  29. * get_handler< T, into_handler<T> >. The second parameter is the parent
  30. * handler (in this case, it's the top handler, into_handler<T>). The type is
  31. * actually an alias to class template converting_handler, which has a separate
  32. * specialisation for every conversion category from the list of generic
  33. * conversion categories (e.g. sequence_conversion_tag, tuple_conversion_tag,
  34. * etc.) Instantiations of the template store a pointer to the parent handler
  35. * and a pointer to the value T.
  36. *
  37. * The nested handler handles specific parser events by setting error_code to
  38. * an appropriate value, if it receives an event it isn't supposed to handle
  39. * (e.g. a number handler getting an on_string event), and also updates the
  40. * value when appropriate. Note that they never need to handle on_comment_part,
  41. * on_comment, on_document_begin, and on_document_end events, as those are
  42. * always handled by the top handler into_handler<T>.
  43. *
  44. * When the nested handler receives an event that completes the current value,
  45. * it is supposed to call its parent's signal_value member function. This is
  46. * necessary for correct handling of composite types (e.g. sequences).
  47. *
  48. * Finally, nested handlers should always call parent's signal_end member
  49. * function if they don't handle on_array_end themselves. This is necessary
  50. * to correctly handle nested composites (e.g. sequences inside sequences).
  51. * signal_end can return false and set error state when the containing parser
  52. * requires more elements.
  53. *
  54. * converting_handler instantiations for composite categories of types have
  55. * their own nested handlers, to which they themselves delegate events. For
  56. * complex types you will get a tree of handlers with into_handler<T> as the
  57. * root and handlers for scalars as leaves.
  58. *
  59. * To reiterate, only into_handler has to handle on_comment_part, on_comment,
  60. * on_document_begin, and on_document_end; only handlers for composites and
  61. * into_handler has to provide signal_value and signal_end; all handlers
  62. * except for into_handler have to call their parent's signal_end from
  63. * their on_array_begin, if they don't handle it themselves; once a handler
  64. * receives an event that finishes its current value, it should call its
  65. * parent's signal_value.
  66. */
  67. namespace boost {
  68. namespace json {
  69. namespace detail {
  70. template< class Impl, class T, class Parent >
  71. class converting_handler;
  72. // get_handler
  73. template< class V, class P >
  74. using get_handler = converting_handler< generic_conversion_category<V>, V, P >;
  75. template<error E> class handler_error_base
  76. {
  77. public:
  78. handler_error_base() = default;
  79. handler_error_base( handler_error_base const& ) = delete;
  80. handler_error_base& operator=( handler_error_base const& ) = delete;
  81. public:
  82. bool on_object_begin( system::error_code& ec ) { BOOST_JSON_FAIL( ec, E ); return false; }
  83. bool on_array_begin( system::error_code& ec ) { BOOST_JSON_FAIL( ec, E ); return false; }
  84. bool on_array_end( system::error_code& ec ) { BOOST_JSON_FAIL( ec, E ); return false; }
  85. bool on_string_part( system::error_code& ec, string_view ) { BOOST_JSON_FAIL( ec, E ); return false; }
  86. bool on_string( system::error_code& ec, string_view ) { BOOST_JSON_FAIL( ec, E ); return false; }
  87. bool on_number_part( system::error_code& ec ) { BOOST_JSON_FAIL( ec, E ); return false; }
  88. bool on_int64( system::error_code& ec, std::int64_t ) { BOOST_JSON_FAIL( ec, E ); return false; }
  89. bool on_uint64( system::error_code& ec, std::uint64_t ) { BOOST_JSON_FAIL( ec, E ); return false; }
  90. bool on_double( system::error_code& ec, double ) { BOOST_JSON_FAIL( ec, E ); return false; }
  91. bool on_bool( system::error_code& ec, bool ) { BOOST_JSON_FAIL( ec, E ); return false; }
  92. bool on_null( system::error_code& ec ) { BOOST_JSON_FAIL( ec, E ); return false; }
  93. // LCOV_EXCL_START
  94. // parses that can't handle this would fail at on_object_begin
  95. bool on_object_end( system::error_code& ) { BOOST_ASSERT( false ); return false; }
  96. bool on_key_part( system::error_code& ec, string_view ) { BOOST_JSON_FAIL( ec, E ); return false; }
  97. bool on_key( system::error_code& ec, string_view ) { BOOST_JSON_FAIL( ec, E ); return false; }
  98. // LCOV_EXCL_STOP
  99. };
  100. template< class P, error E >
  101. class scalar_handler
  102. : public handler_error_base<E>
  103. {
  104. protected:
  105. P* parent_;
  106. public:
  107. scalar_handler(scalar_handler const&) = delete;
  108. scalar_handler& operator=(scalar_handler const&) = delete;
  109. scalar_handler(P* p): parent_( p )
  110. {}
  111. bool on_array_end( system::error_code& ec )
  112. {
  113. return parent_->signal_end(ec);
  114. }
  115. };
  116. template< class D, class V, class P, error E >
  117. class composite_handler
  118. {
  119. protected:
  120. using inner_handler_type = get_handler<V, D>;
  121. P* parent_;
  122. #if defined(__GNUC__) && __GNUC__ < 5 && !defined(__clang__)
  123. # pragma GCC diagnostic push
  124. # pragma GCC diagnostic ignored "-Wmissing-field-initializers"
  125. #endif
  126. V next_value_ = {};
  127. inner_handler_type inner_;
  128. bool inner_active_ = false;
  129. public:
  130. composite_handler( composite_handler const& ) = delete;
  131. composite_handler& operator=( composite_handler const& ) = delete;
  132. composite_handler( P* p )
  133. : parent_(p), inner_( &next_value_, static_cast<D*>(this) )
  134. {}
  135. #if defined(__GNUC__) && __GNUC__ < 5 && !defined(__clang__)
  136. # pragma GCC diagnostic pop
  137. #endif
  138. bool signal_end(system::error_code& ec)
  139. {
  140. inner_active_ = false;
  141. return parent_->signal_value(ec);
  142. }
  143. #define BOOST_JSON_INVOKE_INNER(f) \
  144. if( !inner_active_ ) { \
  145. BOOST_JSON_FAIL(ec, E); \
  146. return false; \
  147. } \
  148. else \
  149. return inner_.f
  150. bool on_object_begin( system::error_code& ec )
  151. {
  152. BOOST_JSON_INVOKE_INNER( on_object_begin(ec) );
  153. }
  154. bool on_object_end( system::error_code& ec )
  155. {
  156. BOOST_JSON_INVOKE_INNER( on_object_end(ec) );
  157. }
  158. bool on_array_begin( system::error_code& ec )
  159. {
  160. BOOST_JSON_INVOKE_INNER( on_array_begin(ec) );
  161. }
  162. bool on_array_end( system::error_code& ec )
  163. {
  164. BOOST_JSON_INVOKE_INNER( on_array_end(ec) );
  165. }
  166. bool on_key_part( system::error_code& ec, string_view sv )
  167. {
  168. BOOST_JSON_INVOKE_INNER( on_key_part(ec, sv) );
  169. }
  170. bool on_key( system::error_code& ec, string_view sv )
  171. {
  172. BOOST_JSON_INVOKE_INNER( on_key(ec, sv) );
  173. }
  174. bool on_string_part( system::error_code& ec, string_view sv )
  175. {
  176. BOOST_JSON_INVOKE_INNER( on_string_part(ec, sv) );
  177. }
  178. bool on_string( system::error_code& ec, string_view sv )
  179. {
  180. BOOST_JSON_INVOKE_INNER( on_string(ec, sv) );
  181. }
  182. bool on_number_part( system::error_code& ec )
  183. {
  184. BOOST_JSON_INVOKE_INNER( on_number_part(ec) );
  185. }
  186. bool on_int64( system::error_code& ec, std::int64_t v )
  187. {
  188. BOOST_JSON_INVOKE_INNER( on_int64(ec, v) );
  189. }
  190. bool on_uint64( system::error_code& ec, std::uint64_t v )
  191. {
  192. BOOST_JSON_INVOKE_INNER( on_uint64(ec, v) );
  193. }
  194. bool on_double( system::error_code& ec, double v )
  195. {
  196. BOOST_JSON_INVOKE_INNER( on_double(ec, v) );
  197. }
  198. bool on_bool( system::error_code& ec, bool v )
  199. {
  200. BOOST_JSON_INVOKE_INNER( on_bool(ec, v) );
  201. }
  202. bool on_null( system::error_code& ec )
  203. {
  204. BOOST_JSON_INVOKE_INNER( on_null(ec) );
  205. }
  206. #undef BOOST_JSON_INVOKE_INNER
  207. };
  208. // integral handler
  209. template<class V,
  210. typename std::enable_if<std::is_signed<V>::value, int>::type = 0>
  211. bool integral_in_range( std::int64_t v )
  212. {
  213. return v >= (std::numeric_limits<V>::min)() && v <= (std::numeric_limits<V>::max)();
  214. }
  215. template<class V,
  216. typename std::enable_if<!std::is_signed<V>::value, int>::type = 0>
  217. bool integral_in_range( std::int64_t v )
  218. {
  219. return v >= 0 && static_cast<std::uint64_t>( v ) <= (std::numeric_limits<V>::max)();
  220. }
  221. template<class V>
  222. bool integral_in_range( std::uint64_t v )
  223. {
  224. return v <= static_cast<typename std::make_unsigned<V>::type>( (std::numeric_limits<V>::max)() );
  225. }
  226. template< class V, class P >
  227. class converting_handler<integral_conversion_tag, V, P>
  228. : public scalar_handler<P, error::not_integer>
  229. {
  230. private:
  231. V* value_;
  232. public:
  233. converting_handler( V* v, P* p )
  234. : converting_handler::scalar_handler(p)
  235. , value_(v)
  236. {}
  237. bool on_number_part( system::error_code& )
  238. {
  239. return true;
  240. }
  241. bool on_int64(system::error_code& ec, std::int64_t v)
  242. {
  243. if( !integral_in_range<V>( v ) )
  244. {
  245. BOOST_JSON_FAIL( ec, error::not_exact );
  246. return false;
  247. }
  248. *value_ = static_cast<V>( v );
  249. return this->parent_->signal_value(ec);
  250. }
  251. bool on_uint64(system::error_code& ec, std::uint64_t v)
  252. {
  253. if( !integral_in_range<V>(v) )
  254. {
  255. BOOST_JSON_FAIL( ec, error::not_exact );
  256. return false;
  257. }
  258. *value_ = static_cast<V>(v);
  259. return this->parent_->signal_value(ec);
  260. }
  261. };
  262. // floating point handler
  263. template< class V, class P>
  264. class converting_handler<floating_point_conversion_tag, V, P>
  265. : public scalar_handler<P, error::not_double>
  266. {
  267. private:
  268. V* value_;
  269. public:
  270. converting_handler( V* v, P* p )
  271. : converting_handler::scalar_handler(p)
  272. , value_(v)
  273. {}
  274. bool on_number_part( system::error_code& )
  275. {
  276. return true;
  277. }
  278. bool on_int64(system::error_code& ec, std::int64_t v)
  279. {
  280. *value_ = static_cast<V>(v);
  281. return this->parent_->signal_value(ec);
  282. }
  283. bool on_uint64(system::error_code& ec, std::uint64_t v)
  284. {
  285. *value_ = static_cast<V>(v);
  286. return this->parent_->signal_value(ec);
  287. }
  288. bool on_double(system::error_code& ec, double v)
  289. {
  290. *value_ = static_cast<V>(v);
  291. return this->parent_->signal_value(ec);
  292. }
  293. };
  294. // string handler
  295. template< class V, class P >
  296. class converting_handler<string_like_conversion_tag, V, P>
  297. : public scalar_handler<P, error::not_string>
  298. {
  299. private:
  300. V* value_;
  301. bool cleared_ = false;
  302. public:
  303. converting_handler( V* v, P* p )
  304. : converting_handler::scalar_handler(p)
  305. , value_(v)
  306. {}
  307. bool on_string_part( system::error_code&, string_view sv )
  308. {
  309. if( !cleared_ )
  310. {
  311. cleared_ = true;
  312. value_->clear();
  313. }
  314. value_->append( sv.begin(), sv.end() );
  315. return true;
  316. }
  317. bool on_string(system::error_code& ec, string_view sv)
  318. {
  319. if( !cleared_ )
  320. value_->clear();
  321. else
  322. cleared_ = false;
  323. value_->append( sv.begin(), sv.end() );
  324. return this->parent_->signal_value(ec);
  325. }
  326. };
  327. // bool handler
  328. template< class V, class P >
  329. class converting_handler<bool_conversion_tag, V, P>
  330. : public scalar_handler<P, error::not_bool>
  331. {
  332. private:
  333. V* value_;
  334. public:
  335. converting_handler( V* v, P* p )
  336. : converting_handler::scalar_handler(p)
  337. , value_(v)
  338. {}
  339. bool on_bool(system::error_code& ec, bool v)
  340. {
  341. *value_ = v;
  342. return this->parent_->signal_value(ec);
  343. }
  344. };
  345. // null handler
  346. template< class V, class P >
  347. class converting_handler<null_like_conversion_tag, V, P>
  348. : public scalar_handler<P, error::not_null>
  349. {
  350. private:
  351. V* value_;
  352. public:
  353. converting_handler( V* v, P* p )
  354. : converting_handler::scalar_handler(p)
  355. , value_(v)
  356. {}
  357. bool on_null(system::error_code& ec)
  358. {
  359. *value_ = {};
  360. return this->parent_->signal_value(ec);
  361. }
  362. };
  363. // described enum handler
  364. template< class V, class P >
  365. class converting_handler<described_enum_conversion_tag, V, P>
  366. : public scalar_handler<P, error::not_string>
  367. {
  368. #ifndef BOOST_DESCRIBE_CXX14
  369. static_assert(
  370. sizeof(V) == 0, "Enum support for parse_into requires C++14" );
  371. #else
  372. private:
  373. V* value_;
  374. std::string name_;
  375. public:
  376. converting_handler( V* v, P* p )
  377. : converting_handler::scalar_handler(p)
  378. , value_(v)
  379. {}
  380. bool on_string_part( system::error_code&, string_view sv )
  381. {
  382. name_.append( sv.begin(), sv.end() );
  383. return true;
  384. }
  385. bool on_string(system::error_code& ec, string_view sv)
  386. {
  387. string_view name = sv;
  388. if( !name_.empty() )
  389. {
  390. name_.append( sv.begin(), sv.end() );
  391. name = name_;
  392. }
  393. if( !describe::enum_from_string(name, *value_) )
  394. {
  395. BOOST_JSON_FAIL(ec, error::unknown_name);
  396. return false;
  397. }
  398. return this->parent_->signal_value(ec);
  399. }
  400. #endif // BOOST_DESCRIBE_CXX14
  401. };
  402. template< class V, class P >
  403. class converting_handler<no_conversion_tag, V, P>
  404. {
  405. static_assert( sizeof(V) == 0, "This type is not supported" );
  406. };
  407. // sequence handler
  408. template< class It >
  409. bool cannot_insert(It i, It e)
  410. {
  411. return i == e;
  412. }
  413. template< class It1, class It2 >
  414. std::false_type cannot_insert(It1, It2)
  415. {
  416. return {};
  417. }
  418. template< class It >
  419. bool needs_more_elements(It i, It e)
  420. {
  421. return i != e;
  422. }
  423. template< class It1, class It2 >
  424. std::false_type needs_more_elements(It1, It2)
  425. {
  426. return {};
  427. }
  428. template<class T>
  429. void
  430. clear_container(
  431. T&,
  432. mp11::mp_int<2>)
  433. {
  434. }
  435. template<class T>
  436. void
  437. clear_container(
  438. T& target,
  439. mp11::mp_int<1>)
  440. {
  441. target.clear();
  442. }
  443. template<class T>
  444. void
  445. clear_container(
  446. T& target,
  447. mp11::mp_int<0>)
  448. {
  449. target.clear();
  450. }
  451. template< class V, class P >
  452. class converting_handler<sequence_conversion_tag, V, P>
  453. : public composite_handler<
  454. converting_handler<sequence_conversion_tag, V, P>,
  455. detail::value_type<V>,
  456. P,
  457. error::not_array>
  458. {
  459. private:
  460. V* value_;
  461. using Inserter = decltype(
  462. detail::inserter(*value_, inserter_implementation<V>()) );
  463. Inserter inserter;
  464. public:
  465. converting_handler( V* v, P* p )
  466. : converting_handler::composite_handler(p)
  467. , value_(v)
  468. , inserter( detail::inserter(*value_, inserter_implementation<V>()) )
  469. {}
  470. bool signal_value(system::error_code& ec)
  471. {
  472. if(cannot_insert( inserter, value_->end() ))
  473. {
  474. BOOST_JSON_FAIL( ec, error::size_mismatch );
  475. return false;
  476. }
  477. *inserter++ = std::move(this->next_value_);
  478. #if defined(__GNUC__) && __GNUC__ < 5 && !defined(__clang__)
  479. # pragma GCC diagnostic push
  480. # pragma GCC diagnostic ignored "-Wmissing-field-initializers"
  481. #endif
  482. this->next_value_ = {};
  483. #if defined(__GNUC__) && __GNUC__ < 5 && !defined(__clang__)
  484. # pragma GCC diagnostic pop
  485. #endif
  486. return true;
  487. }
  488. bool signal_end(system::error_code& ec)
  489. {
  490. if(needs_more_elements( inserter, value_->end() ))
  491. {
  492. BOOST_JSON_FAIL( ec, error::size_mismatch );
  493. return false;
  494. }
  495. inserter = detail::inserter(*value_, inserter_implementation<V>());
  496. return converting_handler::composite_handler::signal_end(ec);
  497. }
  498. bool on_array_begin( system::error_code& ec )
  499. {
  500. if( this->inner_active_ )
  501. return this->inner_.on_array_begin( ec );
  502. this->inner_active_ = true;
  503. clear_container( *value_, inserter_implementation<V>() );
  504. return true;
  505. }
  506. bool on_array_end( system::error_code& ec )
  507. {
  508. if( this->inner_active_ )
  509. return this->inner_.on_array_end( ec );
  510. return this->parent_->signal_end(ec);
  511. }
  512. };
  513. // map handler
  514. template< class V, class P >
  515. class converting_handler<map_like_conversion_tag, V, P>
  516. : public composite_handler<
  517. converting_handler<map_like_conversion_tag, V, P>,
  518. detail::mapped_type<V>,
  519. P,
  520. error::not_object>
  521. {
  522. private:
  523. V* value_;
  524. std::string key_;
  525. public:
  526. converting_handler( V* v, P* p )
  527. : converting_handler::composite_handler(p), value_(v)
  528. {}
  529. bool signal_value(system::error_code&)
  530. {
  531. value_->emplace( std::move(key_), std::move(this->next_value_) );
  532. key_ = {};
  533. this->next_value_ = {};
  534. this->inner_active_ = false;
  535. return true;
  536. }
  537. bool on_object_begin( system::error_code& ec )
  538. {
  539. if( this->inner_active_ )
  540. return this->inner_.on_object_begin(ec);
  541. clear_container( *value_, inserter_implementation<V>() );
  542. return true;
  543. }
  544. bool on_object_end(system::error_code& ec)
  545. {
  546. if( this->inner_active_ )
  547. return this->inner_.on_object_end(ec);
  548. return this->parent_->signal_value(ec);
  549. }
  550. bool on_array_end( system::error_code& ec )
  551. {
  552. if( this->inner_active_ )
  553. return this->inner_.on_array_end(ec);
  554. return this->parent_->signal_end(ec);
  555. }
  556. bool on_key_part( system::error_code& ec, string_view sv )
  557. {
  558. if( this->inner_active_ )
  559. return this->inner_.on_key_part(ec, sv);
  560. key_.append( sv.data(), sv.size() );
  561. return true;
  562. }
  563. bool on_key( system::error_code& ec, string_view sv )
  564. {
  565. if( this->inner_active_ )
  566. return this->inner_.on_key(ec, sv);
  567. key_.append( sv.data(), sv.size() );
  568. this->inner_active_ = true;
  569. return true;
  570. }
  571. };
  572. // tuple handler
  573. template<std::size_t I, class T>
  574. struct handler_tuple_element
  575. {
  576. template< class... Args >
  577. handler_tuple_element( Args&& ... args )
  578. : t_( static_cast<Args&&>(args)... )
  579. {}
  580. T t_;
  581. };
  582. template<std::size_t I, class T>
  583. T&
  584. get( handler_tuple_element<I, T>& e )
  585. {
  586. return e.t_;
  587. }
  588. template<
  589. class P,
  590. class LV,
  591. class S = mp11::make_index_sequence<mp11::mp_size<LV>::value> >
  592. struct handler_tuple;
  593. template< class P, template<class...> class L, class... V, std::size_t... I >
  594. struct handler_tuple< P, L<V...>, mp11::index_sequence<I...> >
  595. : handler_tuple_element<I, V>
  596. ...
  597. {
  598. handler_tuple( handler_tuple const& ) = delete;
  599. handler_tuple& operator=( handler_tuple const& ) = delete;
  600. template< class Access, class T >
  601. handler_tuple( Access access, T* pv, P* pp )
  602. : handler_tuple_element<I, V>(
  603. access( pv, mp11::mp_size_t<I>() ),
  604. pp )
  605. ...
  606. {}
  607. };
  608. #if defined(BOOST_MSVC) && BOOST_MSVC < 1910
  609. template< class T >
  610. struct tuple_element_list_impl
  611. {
  612. template< class I >
  613. using tuple_element_helper = tuple_element_t<I::value, T>;
  614. using type = mp11::mp_transform<
  615. tuple_element_helper,
  616. mp11::mp_iota< std::tuple_size<T> > >;
  617. };
  618. template< class T >
  619. using tuple_element_list = typename tuple_element_list_impl<T>::type;
  620. #else
  621. template< class I, class T >
  622. using tuple_element_helper = tuple_element_t<I::value, T>;
  623. template< class T >
  624. using tuple_element_list = mp11::mp_transform_q<
  625. mp11::mp_bind_back< tuple_element_helper, T>,
  626. mp11::mp_iota< std::tuple_size<T> > >;
  627. #endif
  628. template< class Op, class... Args>
  629. struct handler_op_invoker
  630. {
  631. public:
  632. std::tuple<Args&...> args;
  633. template< class Handler >
  634. bool
  635. operator()( Handler& handler ) const
  636. {
  637. return (*this)( handler, mp11::index_sequence_for<Args...>() );
  638. }
  639. private:
  640. template< class Handler, std::size_t... I >
  641. bool
  642. operator()( Handler& handler, mp11::index_sequence<I...> ) const
  643. {
  644. return Op()( handler, std::get<I>(args)... );
  645. }
  646. };
  647. template< class Handlers, class F >
  648. struct tuple_handler_op_invoker
  649. {
  650. Handlers& handlers;
  651. F fn;
  652. template< class I >
  653. bool
  654. operator()( I ) const
  655. {
  656. return fn( get<I::value>(handlers) );
  657. }
  658. };
  659. struct tuple_accessor
  660. {
  661. template< class T, class I >
  662. auto operator()( T* t, I ) const -> tuple_element_t<I::value, T>*
  663. {
  664. using std::get;
  665. return &get<I::value>(*t);
  666. }
  667. };
  668. template< class T, class P >
  669. class converting_handler<tuple_conversion_tag, T, P>
  670. {
  671. private:
  672. using ElementTypes = tuple_element_list<T>;
  673. template<class V>
  674. using ElementHandler = get_handler<V, converting_handler>;
  675. using InnerHandlers = mp11::mp_transform<ElementHandler, ElementTypes>;
  676. using HandlerTuple = handler_tuple<converting_handler, InnerHandlers>;
  677. T* value_;
  678. P* parent_;
  679. HandlerTuple handlers_;
  680. int inner_active_ = -1;
  681. public:
  682. converting_handler( converting_handler const& ) = delete;
  683. converting_handler& operator=( converting_handler const& ) = delete;
  684. converting_handler( T* v, P* p )
  685. : value_(v) , parent_(p) , handlers_(tuple_accessor(), v, this)
  686. {}
  687. bool signal_value(system::error_code&)
  688. {
  689. ++inner_active_;
  690. return true;
  691. }
  692. bool signal_end(system::error_code& ec)
  693. {
  694. constexpr int N = std::tuple_size<T>::value;
  695. if( inner_active_ < N )
  696. {
  697. BOOST_JSON_FAIL( ec, error::size_mismatch );
  698. return false;
  699. }
  700. inner_active_ = -1;
  701. return parent_->signal_value(ec);
  702. }
  703. #define BOOST_JSON_HANDLE_EVENT(fn) \
  704. struct do_ ## fn \
  705. { \
  706. template< class H, class... Args > \
  707. bool operator()( H& h, Args& ... args ) const \
  708. { \
  709. return h. fn (args...); \
  710. } \
  711. }; \
  712. \
  713. template< class... Args > \
  714. bool fn( system::error_code& ec, Args&& ... args ) \
  715. { \
  716. if( inner_active_ < 0 ) \
  717. { \
  718. BOOST_JSON_FAIL( ec, error::not_array ); \
  719. return false; \
  720. } \
  721. constexpr int N = std::tuple_size<T>::value; \
  722. if( inner_active_ >= N ) \
  723. { \
  724. BOOST_JSON_FAIL( ec, error::size_mismatch ); \
  725. return false; \
  726. } \
  727. using F = handler_op_invoker< do_ ## fn, system::error_code, Args...>; \
  728. using H = decltype(handlers_); \
  729. return mp11::mp_with_index<N>( \
  730. inner_active_, \
  731. tuple_handler_op_invoker<H, F>{ \
  732. handlers_, \
  733. F{ std::forward_as_tuple(ec, args...) } } ); \
  734. }
  735. BOOST_JSON_HANDLE_EVENT( on_object_begin )
  736. BOOST_JSON_HANDLE_EVENT( on_object_end )
  737. struct do_on_array_begin
  738. {
  739. HandlerTuple& handlers;
  740. system::error_code& ec;
  741. template< class I >
  742. bool operator()( I ) const
  743. {
  744. return get<I::value>(handlers).on_array_begin(ec);
  745. }
  746. };
  747. bool on_array_begin( system::error_code& ec )
  748. {
  749. if( inner_active_ < 0 )
  750. {
  751. inner_active_ = 0;
  752. return true;
  753. }
  754. constexpr int N = std::tuple_size<T>::value;
  755. if( inner_active_ >= N )
  756. {
  757. BOOST_JSON_FAIL( ec, error::size_mismatch );
  758. return false;
  759. }
  760. return mp11::mp_with_index<N>(
  761. inner_active_, do_on_array_begin{handlers_, ec} );
  762. }
  763. struct do_on_array_end
  764. {
  765. HandlerTuple& handlers;
  766. system::error_code& ec;
  767. template< class I >
  768. bool operator()( I ) const
  769. {
  770. return get<I::value>(handlers).on_array_end(ec);
  771. }
  772. };
  773. bool on_array_end( system::error_code& ec )
  774. {
  775. if( inner_active_ < 0 )
  776. return parent_->signal_end(ec);
  777. constexpr int N = std::tuple_size<T>::value;
  778. if( inner_active_ >= N )
  779. return signal_end(ec);
  780. return mp11::mp_with_index<N>(
  781. inner_active_, do_on_array_end{handlers_, ec} );
  782. }
  783. BOOST_JSON_HANDLE_EVENT( on_key_part )
  784. BOOST_JSON_HANDLE_EVENT( on_key )
  785. BOOST_JSON_HANDLE_EVENT( on_string_part )
  786. BOOST_JSON_HANDLE_EVENT( on_string )
  787. BOOST_JSON_HANDLE_EVENT( on_number_part )
  788. BOOST_JSON_HANDLE_EVENT( on_int64 )
  789. BOOST_JSON_HANDLE_EVENT( on_uint64 )
  790. BOOST_JSON_HANDLE_EVENT( on_double )
  791. BOOST_JSON_HANDLE_EVENT( on_bool )
  792. BOOST_JSON_HANDLE_EVENT( on_null )
  793. #undef BOOST_JSON_HANDLE_EVENT
  794. };
  795. // described struct handler
  796. #if defined(BOOST_MSVC) && BOOST_MSVC < 1910
  797. template< class T >
  798. struct struct_element_list_impl
  799. {
  800. template< class D >
  801. using helper = described_member_t<T, D>;
  802. using type = mp11::mp_transform< helper, described_members<T> >;
  803. };
  804. template< class T >
  805. using struct_element_list = typename struct_element_list_impl<T>::type;
  806. #else
  807. template< class T >
  808. using struct_element_list = mp11::mp_transform_q<
  809. mp11::mp_bind_front< described_member_t, T >, described_members<T> >;
  810. #endif
  811. struct struct_accessor
  812. {
  813. template< class T >
  814. auto operator()( T*, mp11::mp_size< described_members<T> > ) const
  815. -> void*
  816. {
  817. return nullptr;
  818. }
  819. template< class T, class I >
  820. auto operator()( T* t, I ) const
  821. -> described_member_t<T, mp11::mp_at< described_members<T>, I> >*
  822. {
  823. using Ds = described_members<T>;
  824. using D = mp11::mp_at<Ds, I>;
  825. return &(t->*D::pointer);
  826. }
  827. };
  828. struct struct_key_searcher
  829. {
  830. string_view key;
  831. int& found;
  832. int index = 0;
  833. struct_key_searcher(string_view key, int& found) noexcept
  834. : key(key), found(found)
  835. {}
  836. template< class D >
  837. void
  838. operator()( D )
  839. {
  840. if( key == D::name )
  841. found = index;
  842. ++index;
  843. }
  844. };
  845. template<class P>
  846. struct ignoring_handler
  847. {
  848. P* parent_;
  849. std::size_t array_depth_ = 0;
  850. std::size_t object_depth_ = 0;
  851. ignoring_handler(ignoring_handler const&) = delete;
  852. ignoring_handler& operator=(ignoring_handler const&) = delete;
  853. ignoring_handler(void*, P* p) noexcept
  854. : parent_(p)
  855. {}
  856. bool on_object_begin(system::error_code&)
  857. {
  858. ++object_depth_;
  859. return true;
  860. }
  861. bool on_object_end(system::error_code& ec)
  862. {
  863. BOOST_ASSERT( object_depth_ > 0 );
  864. --object_depth_;
  865. if( (array_depth_ + object_depth_) == 0 )
  866. return parent_->signal_value(ec);
  867. return true;
  868. }
  869. bool on_array_begin(system::error_code&)
  870. {
  871. ++array_depth_;
  872. return true;
  873. }
  874. bool on_array_end(system::error_code& ec)
  875. {
  876. BOOST_ASSERT( array_depth_ > 0 );
  877. --array_depth_;
  878. if( (array_depth_ + object_depth_) == 0 )
  879. return parent_->signal_end(ec);
  880. return true;
  881. }
  882. bool on_key_part(system::error_code&, string_view)
  883. {
  884. return true;
  885. }
  886. bool on_key(system::error_code&, string_view)
  887. {
  888. return true;
  889. }
  890. bool on_string_part(system::error_code&, string_view)
  891. {
  892. return true;
  893. }
  894. bool on_string(system::error_code& ec, string_view)
  895. {
  896. if( (array_depth_ + object_depth_) == 0 )
  897. return parent_->signal_value(ec);
  898. return true;
  899. }
  900. bool on_number_part(system::error_code&)
  901. {
  902. return true;
  903. }
  904. bool on_int64(system::error_code& ec, std::int64_t)
  905. {
  906. if( (array_depth_ + object_depth_) == 0 )
  907. return parent_->signal_value(ec);
  908. return true;
  909. }
  910. bool on_uint64(system::error_code& ec, std::uint64_t)
  911. {
  912. if( (array_depth_ + object_depth_) == 0 )
  913. return parent_->signal_value(ec);
  914. return true;
  915. }
  916. bool on_double(system::error_code& ec, double)
  917. {
  918. if( (array_depth_ + object_depth_) == 0 )
  919. return parent_->signal_value(ec);
  920. return true;
  921. }
  922. bool on_bool(system::error_code& ec, bool)
  923. {
  924. if( (array_depth_ + object_depth_) == 0 )
  925. return parent_->signal_value(ec);
  926. return true;
  927. }
  928. bool on_null(system::error_code& ec)
  929. {
  930. if( (array_depth_ + object_depth_) == 0 )
  931. return parent_->signal_value(ec);
  932. return true;
  933. }
  934. };
  935. template<class V, class P>
  936. class converting_handler<described_class_conversion_tag, V, P>
  937. {
  938. #if !defined(BOOST_DESCRIBE_CXX14)
  939. static_assert(
  940. sizeof(V) == 0, "Struct support for parse_into requires C++14" );
  941. #else
  942. private:
  943. static_assert(
  944. uniquely_named_members<V>::value,
  945. "The type has several described members with the same name.");
  946. using Dm = described_members<V>;
  947. using Dt = struct_element_list<V>;
  948. template<class T>
  949. using MemberHandler = get_handler<T, converting_handler>;
  950. using InnerHandlers = mp11::mp_push_back<
  951. mp11::mp_transform<MemberHandler, Dt>,
  952. ignoring_handler<converting_handler> >;
  953. using InnerCount = mp11::mp_size<InnerHandlers>;
  954. V* value_;
  955. P* parent_;
  956. std::string key_;
  957. handler_tuple<converting_handler, InnerHandlers> handlers_;
  958. int inner_active_ = -1;
  959. std::size_t activated_ = 0;
  960. public:
  961. converting_handler( converting_handler const& ) = delete;
  962. converting_handler& operator=( converting_handler const& ) = delete;
  963. converting_handler( V* v, P* p )
  964. : value_(v), parent_(p), handlers_(struct_accessor(), v, this)
  965. {}
  966. struct is_required_checker
  967. {
  968. bool operator()( mp11::mp_size<Dt> ) const noexcept
  969. {
  970. return false;
  971. }
  972. template< class I >
  973. auto operator()( I ) const noexcept
  974. {
  975. using T = mp11::mp_at<Dt, I>;
  976. return !is_optional_like<T>::value;
  977. }
  978. };
  979. bool signal_value(system::error_code&)
  980. {
  981. BOOST_ASSERT( inner_active_ >= 0 );
  982. bool required_member = mp11::mp_with_index<InnerCount>(
  983. inner_active_,
  984. is_required_checker{});
  985. if( required_member )
  986. ++activated_;
  987. key_ = {};
  988. inner_active_ = -1;
  989. return true;
  990. }
  991. bool signal_end(system::error_code& ec)
  992. {
  993. key_ = {};
  994. inner_active_ = -1;
  995. return parent_->signal_value(ec);
  996. }
  997. #define BOOST_JSON_INVOKE_INNER(fn) \
  998. if( inner_active_ < 0 ) \
  999. { \
  1000. BOOST_JSON_FAIL( ec, error::not_object ); \
  1001. return false; \
  1002. } \
  1003. auto f = [&](auto& handler) { return handler.fn ; }; \
  1004. using F = decltype(f); \
  1005. using H = decltype(handlers_); \
  1006. return mp11::mp_with_index<InnerCount>( \
  1007. inner_active_, \
  1008. tuple_handler_op_invoker<H, F>{handlers_, f} );
  1009. bool on_object_begin( system::error_code& ec )
  1010. {
  1011. if( inner_active_ < 0 )
  1012. return true;
  1013. BOOST_JSON_INVOKE_INNER( on_object_begin(ec) );
  1014. }
  1015. bool on_object_end( system::error_code& ec )
  1016. {
  1017. if( inner_active_ < 0 )
  1018. {
  1019. using C = mp11::mp_count_if<Dt, is_optional_like>;
  1020. constexpr int N = mp11::mp_size<Dt>::value - C::value;
  1021. if( activated_ < N )
  1022. {
  1023. BOOST_JSON_FAIL( ec, error::size_mismatch );
  1024. return false;
  1025. }
  1026. return parent_->signal_value(ec);
  1027. }
  1028. BOOST_JSON_INVOKE_INNER( on_object_end(ec) );
  1029. }
  1030. bool on_array_begin( system::error_code& ec )
  1031. {
  1032. BOOST_JSON_INVOKE_INNER( on_array_begin(ec) );
  1033. }
  1034. bool on_array_end( system::error_code& ec )
  1035. {
  1036. if( inner_active_ < 0 )
  1037. return parent_->signal_end(ec);
  1038. BOOST_JSON_INVOKE_INNER( on_array_end(ec) );
  1039. }
  1040. bool on_key_part( system::error_code& ec, string_view sv )
  1041. {
  1042. if( inner_active_ < 0 )
  1043. {
  1044. key_.append( sv.data(), sv.size() );
  1045. return true;
  1046. }
  1047. BOOST_JSON_INVOKE_INNER( on_key_part(ec, sv) );
  1048. }
  1049. bool on_key( system::error_code& ec, string_view sv )
  1050. {
  1051. if( inner_active_ >= 0 )
  1052. {
  1053. BOOST_JSON_INVOKE_INNER( on_key(ec, sv) );
  1054. }
  1055. string_view key = sv;
  1056. if( !key_.empty() )
  1057. {
  1058. key_.append( sv.data(), sv.size() );
  1059. key = key_;
  1060. }
  1061. inner_active_ = InnerCount::value - 1;
  1062. mp11::mp_for_each<Dm>( struct_key_searcher(key, inner_active_) );
  1063. return true;
  1064. }
  1065. bool on_string_part( system::error_code& ec, string_view sv )
  1066. {
  1067. BOOST_JSON_INVOKE_INNER( on_string_part(ec, sv) );
  1068. }
  1069. bool on_string( system::error_code& ec, string_view sv )
  1070. {
  1071. BOOST_JSON_INVOKE_INNER( on_string(ec, sv) );
  1072. }
  1073. bool on_number_part( system::error_code& ec )
  1074. {
  1075. BOOST_JSON_INVOKE_INNER( on_number_part(ec) );
  1076. }
  1077. bool on_int64( system::error_code& ec, std::int64_t v )
  1078. {
  1079. BOOST_JSON_INVOKE_INNER( on_int64(ec, v) );
  1080. }
  1081. bool on_uint64( system::error_code& ec, std::uint64_t v )
  1082. {
  1083. BOOST_JSON_INVOKE_INNER( on_uint64(ec, v) );
  1084. }
  1085. bool on_double( system::error_code& ec, double v )
  1086. {
  1087. BOOST_JSON_INVOKE_INNER( on_double(ec, v) );
  1088. }
  1089. bool on_bool( system::error_code& ec, bool v )
  1090. {
  1091. BOOST_JSON_INVOKE_INNER( on_bool(ec, v) );
  1092. }
  1093. bool on_null( system::error_code& ec )
  1094. {
  1095. BOOST_JSON_INVOKE_INNER( on_null(ec) );
  1096. }
  1097. #undef BOOST_JSON_INVOKE_INNER
  1098. #endif
  1099. };
  1100. // variant handler
  1101. struct object_begin_handler_event
  1102. { };
  1103. struct object_end_handler_event
  1104. { };
  1105. struct array_begin_handler_event
  1106. { };
  1107. struct array_end_handler_event
  1108. { };
  1109. struct key_handler_event
  1110. {
  1111. std::string value;
  1112. };
  1113. struct string_handler_event
  1114. {
  1115. std::string value;
  1116. };
  1117. struct int64_handler_event
  1118. {
  1119. std::int64_t value;
  1120. };
  1121. struct uint64_handler_event
  1122. {
  1123. std::uint64_t value;
  1124. };
  1125. struct double_handler_event
  1126. {
  1127. double value;
  1128. };
  1129. struct bool_handler_event
  1130. {
  1131. bool value;
  1132. };
  1133. struct null_handler_event
  1134. { };
  1135. using parse_event = variant2::variant<
  1136. object_begin_handler_event,
  1137. object_end_handler_event,
  1138. array_begin_handler_event,
  1139. array_end_handler_event,
  1140. key_handler_event,
  1141. string_handler_event,
  1142. int64_handler_event,
  1143. uint64_handler_event,
  1144. double_handler_event,
  1145. bool_handler_event,
  1146. null_handler_event>;
  1147. template< class H >
  1148. struct event_visitor
  1149. {
  1150. H& handler;
  1151. system::error_code& ec;
  1152. bool
  1153. operator()(object_begin_handler_event&) const
  1154. {
  1155. return handler.on_object_begin(ec);
  1156. }
  1157. bool
  1158. operator()(object_end_handler_event&) const
  1159. {
  1160. return handler.on_object_end(ec);
  1161. }
  1162. bool
  1163. operator()(array_begin_handler_event&) const
  1164. {
  1165. return handler.on_array_begin(ec);
  1166. }
  1167. bool
  1168. operator()(array_end_handler_event&) const
  1169. {
  1170. return handler.on_array_end(ec);
  1171. }
  1172. bool
  1173. operator()(key_handler_event& ev) const
  1174. {
  1175. return handler.on_key(ec, ev.value);
  1176. }
  1177. bool
  1178. operator()(string_handler_event& ev) const
  1179. {
  1180. return handler.on_string(ec, ev.value);
  1181. }
  1182. bool
  1183. operator()(int64_handler_event& ev) const
  1184. {
  1185. return handler.on_int64(ec, ev.value);
  1186. }
  1187. bool
  1188. operator()(uint64_handler_event& ev) const
  1189. {
  1190. return handler.on_uint64(ec, ev.value);
  1191. }
  1192. bool
  1193. operator()(double_handler_event& ev) const
  1194. {
  1195. return handler.on_double(ec, ev.value);
  1196. }
  1197. bool
  1198. operator()(bool_handler_event& ev) const
  1199. {
  1200. return handler.on_bool(ec, ev.value);
  1201. }
  1202. bool
  1203. operator()(null_handler_event&) const
  1204. {
  1205. return handler.on_null(ec);
  1206. }
  1207. };
  1208. // L<T...> -> variant< monostate, get_handler<T, P>... >
  1209. template< class P, class L >
  1210. using inner_handler_variant = mp11::mp_push_front<
  1211. mp11::mp_transform_q<
  1212. mp11::mp_bind_back<get_handler, P>,
  1213. mp11::mp_apply<variant2::variant, L>>,
  1214. variant2::monostate>;
  1215. template< class T, class P >
  1216. class converting_handler<variant_conversion_tag, T, P>
  1217. {
  1218. private:
  1219. using variant_size = mp11::mp_size<T>;
  1220. T* value_;
  1221. P* parent_;
  1222. std::string string_;
  1223. std::vector< parse_event > events_;
  1224. inner_handler_variant<converting_handler, T> inner_;
  1225. int inner_active_ = -1;
  1226. public:
  1227. converting_handler( converting_handler const& ) = delete;
  1228. converting_handler& operator=( converting_handler const& ) = delete;
  1229. converting_handler( T* v, P* p )
  1230. : value_( v )
  1231. , parent_( p )
  1232. {}
  1233. bool signal_value(system::error_code& ec)
  1234. {
  1235. inner_.template emplace<0>();
  1236. inner_active_ = -1;
  1237. events_.clear();
  1238. return parent_->signal_value(ec);
  1239. }
  1240. bool signal_end(system::error_code& ec)
  1241. {
  1242. return parent_->signal_end(ec);
  1243. }
  1244. struct alternative_selector
  1245. {
  1246. converting_handler* self;
  1247. template< class I >
  1248. void
  1249. operator()( I ) const
  1250. {
  1251. using V = mp11::mp_at<T, I>;
  1252. auto& v = self->value_->template emplace<I::value>( V{} );
  1253. self->inner_.template emplace<I::value + 1>(&v, self);
  1254. }
  1255. };
  1256. void
  1257. next_alternative()
  1258. {
  1259. if( ++inner_active_ >= static_cast<int>(variant_size::value) )
  1260. return;
  1261. mp11::mp_with_index< variant_size::value >(
  1262. inner_active_, alternative_selector{this} );
  1263. }
  1264. struct event_processor
  1265. {
  1266. converting_handler* self;
  1267. system::error_code& ec;
  1268. parse_event& event;
  1269. template< class I >
  1270. bool operator()( I ) const
  1271. {
  1272. auto& handler = variant2::get<I::value + 1>(self->inner_);
  1273. using Handler = remove_cvref<decltype(handler)>;
  1274. return variant2::visit(
  1275. event_visitor<Handler>{handler, ec}, event );
  1276. }
  1277. };
  1278. bool process_events(system::error_code& ec)
  1279. {
  1280. constexpr std::size_t N = variant_size::value;
  1281. // should be pointers not iterators, otherwise MSVC crashes
  1282. auto const last = events_.data() + events_.size();
  1283. auto first = last - 1;
  1284. bool ok = false;
  1285. if( inner_active_ < 0 )
  1286. next_alternative();
  1287. do
  1288. {
  1289. if( static_cast<std::size_t>(inner_active_) >= N )
  1290. {
  1291. BOOST_JSON_FAIL( ec, error::exhausted_variants );
  1292. return false;
  1293. }
  1294. for ( ; first != last; ++first )
  1295. {
  1296. ok = mp11::mp_with_index< N >(
  1297. inner_active_, event_processor{this, ec, *first} );
  1298. if( !ok )
  1299. {
  1300. first = events_.data();
  1301. next_alternative();
  1302. ec.clear();
  1303. break;
  1304. }
  1305. }
  1306. }
  1307. while( !ok );
  1308. return true;
  1309. }
  1310. #define BOOST_JSON_INVOKE_INNER(ev, ec) \
  1311. events_.emplace_back( ev ); \
  1312. return process_events(ec);
  1313. bool on_object_begin( system::error_code& ec )
  1314. {
  1315. BOOST_JSON_INVOKE_INNER( object_begin_handler_event{}, ec );
  1316. }
  1317. bool on_object_end( system::error_code& ec )
  1318. {
  1319. BOOST_JSON_INVOKE_INNER( object_end_handler_event{}, ec );
  1320. }
  1321. bool on_array_begin( system::error_code& ec )
  1322. {
  1323. BOOST_JSON_INVOKE_INNER( array_begin_handler_event{}, ec );
  1324. }
  1325. bool on_array_end( system::error_code& ec )
  1326. {
  1327. if( !inner_active_ )
  1328. return signal_end(ec);
  1329. BOOST_JSON_INVOKE_INNER( array_end_handler_event{}, ec );
  1330. }
  1331. bool on_key_part( system::error_code&, string_view sv )
  1332. {
  1333. string_.append(sv);
  1334. return true;
  1335. }
  1336. bool on_key( system::error_code& ec, string_view sv )
  1337. {
  1338. string_.append(sv);
  1339. BOOST_JSON_INVOKE_INNER( key_handler_event{ std::move(string_) }, ec );
  1340. }
  1341. bool on_string_part( system::error_code&, string_view sv )
  1342. {
  1343. string_.append(sv);
  1344. return true;
  1345. }
  1346. bool on_string( system::error_code& ec, string_view sv )
  1347. {
  1348. string_.append(sv);
  1349. BOOST_JSON_INVOKE_INNER(
  1350. string_handler_event{ std::move(string_) }, ec );
  1351. }
  1352. bool on_number_part( system::error_code& )
  1353. {
  1354. return true;
  1355. }
  1356. bool on_int64( system::error_code& ec, std::int64_t v )
  1357. {
  1358. BOOST_JSON_INVOKE_INNER( int64_handler_event{v}, ec );
  1359. }
  1360. bool on_uint64( system::error_code& ec, std::uint64_t v )
  1361. {
  1362. BOOST_JSON_INVOKE_INNER( uint64_handler_event{v}, ec );
  1363. }
  1364. bool on_double( system::error_code& ec, double v )
  1365. {
  1366. BOOST_JSON_INVOKE_INNER( double_handler_event{v}, ec );
  1367. }
  1368. bool on_bool( system::error_code& ec, bool v )
  1369. {
  1370. BOOST_JSON_INVOKE_INNER( bool_handler_event{v}, ec );
  1371. }
  1372. bool on_null( system::error_code& ec )
  1373. {
  1374. BOOST_JSON_INVOKE_INNER( null_handler_event{}, ec );
  1375. }
  1376. #undef BOOST_JSON_INVOKE_INNER
  1377. };
  1378. // optional handler
  1379. template<class V, class P>
  1380. class converting_handler<optional_conversion_tag, V, P>
  1381. {
  1382. private:
  1383. using inner_type = value_result_type<V>;
  1384. using inner_handler_type = get_handler<inner_type, converting_handler>;
  1385. V* value_;
  1386. P* parent_;
  1387. inner_type inner_value_ = {};
  1388. inner_handler_type inner_;
  1389. bool inner_active_ = false;
  1390. public:
  1391. converting_handler( converting_handler const& ) = delete;
  1392. converting_handler& operator=( converting_handler const& ) = delete;
  1393. converting_handler( V* v, P* p )
  1394. : value_(v), parent_(p), inner_(&inner_value_, this)
  1395. {}
  1396. bool signal_value(system::error_code& ec)
  1397. {
  1398. *value_ = std::move(inner_value_);
  1399. inner_active_ = false;
  1400. return parent_->signal_value(ec);
  1401. }
  1402. bool signal_end(system::error_code& ec)
  1403. {
  1404. return parent_->signal_end(ec);
  1405. }
  1406. #define BOOST_JSON_INVOKE_INNER(fn) \
  1407. if( !inner_active_ ) \
  1408. inner_active_ = true; \
  1409. return inner_.fn;
  1410. bool on_object_begin( system::error_code& ec )
  1411. {
  1412. BOOST_JSON_INVOKE_INNER( on_object_begin(ec) );
  1413. }
  1414. bool on_object_end( system::error_code& ec )
  1415. {
  1416. BOOST_JSON_INVOKE_INNER( on_object_end(ec) );
  1417. }
  1418. bool on_array_begin( system::error_code& ec )
  1419. {
  1420. BOOST_JSON_INVOKE_INNER( on_array_begin(ec) );
  1421. }
  1422. bool on_array_end( system::error_code& ec )
  1423. {
  1424. if( !inner_active_ )
  1425. return signal_end(ec);
  1426. BOOST_JSON_INVOKE_INNER( on_array_end(ec) );
  1427. }
  1428. bool on_key_part( system::error_code& ec, string_view sv )
  1429. {
  1430. BOOST_JSON_INVOKE_INNER( on_key_part(ec, sv) );
  1431. }
  1432. bool on_key( system::error_code& ec, string_view sv )
  1433. {
  1434. BOOST_JSON_INVOKE_INNER( on_key(ec, sv) );
  1435. }
  1436. bool on_string_part( system::error_code& ec, string_view sv )
  1437. {
  1438. BOOST_JSON_INVOKE_INNER( on_string_part(ec, sv) );
  1439. }
  1440. bool on_string( system::error_code& ec, string_view sv )
  1441. {
  1442. BOOST_JSON_INVOKE_INNER( on_string(ec, sv) );
  1443. }
  1444. bool on_number_part( system::error_code& ec )
  1445. {
  1446. BOOST_JSON_INVOKE_INNER( on_number_part(ec) );
  1447. }
  1448. bool on_int64( system::error_code& ec, std::int64_t v )
  1449. {
  1450. BOOST_JSON_INVOKE_INNER( on_int64(ec, v) );
  1451. }
  1452. bool on_uint64( system::error_code& ec, std::uint64_t v )
  1453. {
  1454. BOOST_JSON_INVOKE_INNER( on_uint64(ec, v) );
  1455. }
  1456. bool on_double( system::error_code& ec, double v )
  1457. {
  1458. BOOST_JSON_INVOKE_INNER( on_double(ec, v) );
  1459. }
  1460. bool on_bool( system::error_code& ec, bool v )
  1461. {
  1462. BOOST_JSON_INVOKE_INNER( on_bool(ec, v) );
  1463. }
  1464. bool on_null(system::error_code& ec)
  1465. {
  1466. if( !inner_active_ )
  1467. {
  1468. *value_ = {};
  1469. return this->parent_->signal_value(ec);
  1470. }
  1471. else
  1472. {
  1473. return inner_.on_null(ec);
  1474. }
  1475. }
  1476. #undef BOOST_JSON_INVOKE_INNER
  1477. };
  1478. // path handler
  1479. template< class V, class P >
  1480. class converting_handler<path_conversion_tag, V, P>
  1481. : public scalar_handler<P, error::not_string>
  1482. {
  1483. private:
  1484. V* value_;
  1485. bool cleared_ = false;
  1486. public:
  1487. converting_handler( V* v, P* p )
  1488. : converting_handler::scalar_handler(p)
  1489. , value_(v)
  1490. {}
  1491. bool on_string_part( system::error_code&, string_view sv )
  1492. {
  1493. if( !cleared_ )
  1494. {
  1495. cleared_ = true;
  1496. value_->clear();
  1497. }
  1498. value_->concat( sv.begin(), sv.end() );
  1499. return true;
  1500. }
  1501. bool on_string(system::error_code& ec, string_view sv)
  1502. {
  1503. if( !cleared_ )
  1504. value_->clear();
  1505. else
  1506. cleared_ = false;
  1507. value_->concat( sv.begin(), sv.end() );
  1508. return this->parent_->signal_value(ec);
  1509. }
  1510. };
  1511. // into_handler
  1512. template< class V >
  1513. class into_handler
  1514. {
  1515. private:
  1516. using inner_handler_type = get_handler<V, into_handler>;
  1517. inner_handler_type inner_;
  1518. bool inner_active_ = true;
  1519. public:
  1520. into_handler( into_handler const& ) = delete;
  1521. into_handler& operator=( into_handler const& ) = delete;
  1522. public:
  1523. static constexpr std::size_t max_object_size = object::max_size();
  1524. static constexpr std::size_t max_array_size = array::max_size();
  1525. static constexpr std::size_t max_key_size = string::max_size();
  1526. static constexpr std::size_t max_string_size = string::max_size();
  1527. public:
  1528. explicit into_handler( V* v ): inner_( v, this )
  1529. {
  1530. }
  1531. bool signal_value(system::error_code&)
  1532. {
  1533. return true;
  1534. }
  1535. bool signal_end(system::error_code&)
  1536. {
  1537. return true;
  1538. }
  1539. bool on_document_begin( system::error_code& )
  1540. {
  1541. return true;
  1542. }
  1543. bool on_document_end( system::error_code& )
  1544. {
  1545. inner_active_ = false;
  1546. return true;
  1547. }
  1548. #define BOOST_JSON_INVOKE_INNER(f) \
  1549. if( !inner_active_ ) \
  1550. { \
  1551. BOOST_JSON_FAIL( ec, error::extra_data ); \
  1552. return false; \
  1553. } \
  1554. else \
  1555. return inner_.f
  1556. bool on_object_begin( system::error_code& ec )
  1557. {
  1558. BOOST_JSON_INVOKE_INNER( on_object_begin(ec) );
  1559. }
  1560. bool on_object_end( std::size_t, system::error_code& ec )
  1561. {
  1562. BOOST_JSON_INVOKE_INNER( on_object_end(ec) );
  1563. }
  1564. bool on_array_begin( system::error_code& ec )
  1565. {
  1566. BOOST_JSON_INVOKE_INNER( on_array_begin(ec) );
  1567. }
  1568. bool on_array_end( std::size_t, system::error_code& ec )
  1569. {
  1570. BOOST_JSON_INVOKE_INNER( on_array_end(ec) );
  1571. }
  1572. bool on_key_part( string_view sv, std::size_t, system::error_code& ec )
  1573. {
  1574. BOOST_JSON_INVOKE_INNER( on_key_part(ec, sv) );
  1575. }
  1576. bool on_key( string_view sv, std::size_t, system::error_code& ec )
  1577. {
  1578. BOOST_JSON_INVOKE_INNER( on_key(ec, sv) );
  1579. }
  1580. bool on_string_part( string_view sv, std::size_t, system::error_code& ec )
  1581. {
  1582. BOOST_JSON_INVOKE_INNER( on_string_part(ec, sv) );
  1583. }
  1584. bool on_string( string_view sv, std::size_t, system::error_code& ec )
  1585. {
  1586. BOOST_JSON_INVOKE_INNER( on_string(ec, sv) );
  1587. }
  1588. bool on_number_part( string_view, system::error_code& ec )
  1589. {
  1590. BOOST_JSON_INVOKE_INNER( on_number_part(ec) );
  1591. }
  1592. bool on_int64( std::int64_t v, string_view, system::error_code& ec )
  1593. {
  1594. BOOST_JSON_INVOKE_INNER( on_int64(ec, v) );
  1595. }
  1596. bool on_uint64( std::uint64_t v, string_view, system::error_code& ec )
  1597. {
  1598. BOOST_JSON_INVOKE_INNER( on_uint64(ec, v) );
  1599. }
  1600. bool on_double( double v, string_view, system::error_code& ec )
  1601. {
  1602. BOOST_JSON_INVOKE_INNER( on_double(ec, v) );
  1603. }
  1604. bool on_bool( bool v, system::error_code& ec )
  1605. {
  1606. BOOST_JSON_INVOKE_INNER( on_bool(ec, v) );
  1607. }
  1608. bool on_null( system::error_code& ec )
  1609. {
  1610. BOOST_JSON_INVOKE_INNER( on_null(ec) );
  1611. }
  1612. bool on_comment_part(string_view, system::error_code&)
  1613. {
  1614. return true;
  1615. }
  1616. bool on_comment(string_view, system::error_code&)
  1617. {
  1618. return true;
  1619. }
  1620. #undef BOOST_JSON_INVOKE_INNER
  1621. };
  1622. } // namespace detail
  1623. } // namespace boost
  1624. } // namespace json
  1625. #endif