parse_into.hpp 44 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809
  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( error_code& ec ) { BOOST_JSON_FAIL( ec, E ); return false; }
  83. bool on_array_begin( error_code& ec ) { BOOST_JSON_FAIL( ec, E ); return false; }
  84. bool on_array_end( error_code& ec ) { BOOST_JSON_FAIL( ec, E ); return false; }
  85. bool on_string_part( error_code& ec, string_view ) { BOOST_JSON_FAIL( ec, E ); return false; }
  86. bool on_string( error_code& ec, string_view ) { BOOST_JSON_FAIL( ec, E ); return false; }
  87. bool on_number_part( error_code& ec ) { BOOST_JSON_FAIL( ec, E ); return false; }
  88. bool on_int64( error_code& ec, std::int64_t ) { BOOST_JSON_FAIL( ec, E ); return false; }
  89. bool on_uint64( error_code& ec, std::uint64_t ) { BOOST_JSON_FAIL( ec, E ); return false; }
  90. bool on_double( error_code& ec, double ) { BOOST_JSON_FAIL( ec, E ); return false; }
  91. bool on_bool( error_code& ec, bool ) { BOOST_JSON_FAIL( ec, E ); return false; }
  92. bool on_null( 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( error_code& ) { BOOST_ASSERT( false ); return false; }
  96. bool on_key_part( error_code& ec, string_view ) { BOOST_JSON_FAIL( ec, E ); return false; }
  97. bool on_key( error_code& ec, string_view ) { BOOST_JSON_FAIL( ec, E ); return false; }
  98. // LCOV_EXCL_START
  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( 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(error_code&)
  139. {
  140. inner_active_ = false;
  141. parent_->signal_value();
  142. return true;
  143. }
  144. #define BOOST_JSON_INVOKE_INNER(f) \
  145. if( !inner_active_ ) { \
  146. BOOST_JSON_FAIL(ec, E); \
  147. return false; \
  148. } \
  149. else \
  150. return inner_.f
  151. bool on_object_begin( error_code& ec )
  152. {
  153. BOOST_JSON_INVOKE_INNER( on_object_begin(ec) );
  154. }
  155. bool on_object_end( error_code& ec )
  156. {
  157. BOOST_JSON_INVOKE_INNER( on_object_end(ec) );
  158. }
  159. bool on_array_begin( error_code& ec )
  160. {
  161. BOOST_JSON_INVOKE_INNER( on_array_begin(ec) );
  162. }
  163. bool on_array_end( error_code& ec )
  164. {
  165. BOOST_JSON_INVOKE_INNER( on_array_end(ec) );
  166. }
  167. bool on_key_part( error_code& ec, string_view sv )
  168. {
  169. BOOST_JSON_INVOKE_INNER( on_key_part(ec, sv) );
  170. }
  171. bool on_key( error_code& ec, string_view sv )
  172. {
  173. BOOST_JSON_INVOKE_INNER( on_key(ec, sv) );
  174. }
  175. bool on_string_part( error_code& ec, string_view sv )
  176. {
  177. BOOST_JSON_INVOKE_INNER( on_string_part(ec, sv) );
  178. }
  179. bool on_string( error_code& ec, string_view sv )
  180. {
  181. BOOST_JSON_INVOKE_INNER( on_string(ec, sv) );
  182. }
  183. bool on_number_part( error_code& ec )
  184. {
  185. BOOST_JSON_INVOKE_INNER( on_number_part(ec) );
  186. }
  187. bool on_int64( error_code& ec, std::int64_t v )
  188. {
  189. BOOST_JSON_INVOKE_INNER( on_int64(ec, v) );
  190. }
  191. bool on_uint64( error_code& ec, std::uint64_t v )
  192. {
  193. BOOST_JSON_INVOKE_INNER( on_uint64(ec, v) );
  194. }
  195. bool on_double( error_code& ec, double v )
  196. {
  197. BOOST_JSON_INVOKE_INNER( on_double(ec, v) );
  198. }
  199. bool on_bool( error_code& ec, bool v )
  200. {
  201. BOOST_JSON_INVOKE_INNER( on_bool(ec, v) );
  202. }
  203. bool on_null( error_code& ec )
  204. {
  205. BOOST_JSON_INVOKE_INNER( on_null(ec) );
  206. }
  207. #undef BOOST_JSON_INVOKE_INNER
  208. };
  209. // integral handler
  210. template<class V,
  211. typename std::enable_if<std::is_signed<V>::value, int>::type = 0>
  212. bool integral_in_range( std::int64_t v )
  213. {
  214. return v >= (std::numeric_limits<V>::min)() && v <= (std::numeric_limits<V>::max)();
  215. }
  216. template<class V,
  217. typename std::enable_if<!std::is_signed<V>::value, int>::type = 0>
  218. bool integral_in_range( std::int64_t v )
  219. {
  220. return v >= 0 && static_cast<std::uint64_t>( v ) <= (std::numeric_limits<V>::max)();
  221. }
  222. template<class V>
  223. bool integral_in_range( std::uint64_t v )
  224. {
  225. return v <= static_cast<typename std::make_unsigned<V>::type>( (std::numeric_limits<V>::max)() );
  226. }
  227. template< class V, class P >
  228. class converting_handler<integral_conversion_tag, V, P>
  229. : public scalar_handler<P, error::not_integer>
  230. {
  231. private:
  232. V* value_;
  233. public:
  234. converting_handler( V* v, P* p )
  235. : converting_handler::scalar_handler(p)
  236. , value_(v)
  237. {}
  238. bool on_number_part( error_code& )
  239. {
  240. return true;
  241. }
  242. bool on_int64( error_code& ec, std::int64_t v )
  243. {
  244. if( !integral_in_range<V>( v ) )
  245. {
  246. BOOST_JSON_FAIL( ec, error::not_exact );
  247. return false;
  248. }
  249. *value_ = static_cast<V>( v );
  250. this->parent_->signal_value();
  251. return true;
  252. }
  253. bool on_uint64( error_code& ec, std::uint64_t v )
  254. {
  255. if( !integral_in_range<V>( v ) )
  256. {
  257. BOOST_JSON_FAIL( ec, error::not_exact );
  258. return false;
  259. }
  260. *value_ = static_cast<V>( v );
  261. this->parent_->signal_value();
  262. return true;
  263. }
  264. };
  265. // floating point handler
  266. template< class V, class P>
  267. class converting_handler<floating_point_conversion_tag, V, P>
  268. : public scalar_handler<P, error::not_double>
  269. {
  270. private:
  271. V* value_;
  272. public:
  273. converting_handler( V* v, P* p )
  274. : converting_handler::scalar_handler(p)
  275. , value_(v)
  276. {}
  277. bool on_number_part( error_code& )
  278. {
  279. return true;
  280. }
  281. bool on_int64( error_code&, std::int64_t v )
  282. {
  283. *value_ = static_cast<V>( v );
  284. this->parent_->signal_value();
  285. return true;
  286. }
  287. bool on_uint64( error_code&, std::uint64_t v )
  288. {
  289. *value_ = static_cast<V>( v );
  290. this->parent_->signal_value();
  291. return true;
  292. }
  293. bool on_double( error_code&, double v )
  294. {
  295. *value_ = static_cast<V>( v );
  296. this->parent_->signal_value();
  297. return true;
  298. }
  299. };
  300. // string handler
  301. template< class V, class P >
  302. class converting_handler<string_like_conversion_tag, V, P>
  303. : public scalar_handler<P, error::not_string>
  304. {
  305. private:
  306. V* value_;
  307. bool cleared_ = false;
  308. public:
  309. converting_handler( V* v, P* p )
  310. : converting_handler::scalar_handler(p)
  311. , value_(v)
  312. {}
  313. bool on_string_part( error_code&, string_view sv )
  314. {
  315. if( !cleared_ )
  316. {
  317. cleared_ = true;
  318. value_->clear();
  319. }
  320. value_->append( sv.begin(), sv.end() );
  321. return true;
  322. }
  323. bool on_string( error_code&, string_view sv )
  324. {
  325. if( !cleared_ )
  326. value_->clear();
  327. else
  328. cleared_ = false;
  329. value_->append( sv.begin(), sv.end() );
  330. this->parent_->signal_value();
  331. return true;
  332. }
  333. };
  334. // bool handler
  335. template< class V, class P >
  336. class converting_handler<bool_conversion_tag, V, P>
  337. : public scalar_handler<P, error::not_bool>
  338. {
  339. private:
  340. V* value_;
  341. public:
  342. converting_handler( V* v, P* p )
  343. : converting_handler::scalar_handler(p)
  344. , value_(v)
  345. {}
  346. bool on_bool( error_code&, bool v )
  347. {
  348. *value_ = v;
  349. this->parent_->signal_value();
  350. return true;
  351. }
  352. };
  353. // null handler
  354. template< class V, class P >
  355. class converting_handler<null_like_conversion_tag, V, P>
  356. : public scalar_handler<P, error::not_null>
  357. {
  358. private:
  359. V* value_;
  360. public:
  361. converting_handler( V* v, P* p )
  362. : converting_handler::scalar_handler(p)
  363. , value_(v)
  364. {}
  365. bool on_null( error_code& )
  366. {
  367. *value_ = {};
  368. this->parent_->signal_value();
  369. return true;
  370. }
  371. };
  372. // described enum handler
  373. template< class V, class P >
  374. class converting_handler<described_enum_conversion_tag, V, P>
  375. : public scalar_handler<P, error::not_string>
  376. {
  377. #ifndef BOOST_DESCRIBE_CXX14
  378. static_assert(
  379. sizeof(V) == 0, "Enum support for parse_into requires C++14" );
  380. #else
  381. private:
  382. V* value_;
  383. std::string name_;
  384. public:
  385. converting_handler( V* v, P* p )
  386. : converting_handler::scalar_handler(p)
  387. , value_(v)
  388. {}
  389. bool on_string_part( error_code&, string_view sv )
  390. {
  391. name_.append( sv.begin(), sv.end() );
  392. return true;
  393. }
  394. bool on_string( error_code& ec, string_view sv )
  395. {
  396. string_view name = sv;
  397. if( !name_.empty() )
  398. {
  399. name_.append( sv.begin(), sv.end() );
  400. name = name_;
  401. }
  402. if( !describe::enum_from_string(name, *value_) )
  403. {
  404. BOOST_JSON_FAIL(ec, error::unknown_name);
  405. return false;
  406. }
  407. this->parent_->signal_value();
  408. return true;
  409. }
  410. #endif // BOOST_DESCRIBE_CXX14
  411. };
  412. template< class V, class P >
  413. class converting_handler<no_conversion_tag, V, P>
  414. {
  415. static_assert( sizeof(V) == 0, "This type is not supported" );
  416. };
  417. // sequence handler
  418. template< class It >
  419. bool check_inserter( It l, It r )
  420. {
  421. return l == r;
  422. }
  423. template< class It1, class It2 >
  424. std::true_type check_inserter( 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. void signal_value()
  471. {
  472. *inserter++ = std::move(this->next_value_);
  473. #if defined(__GNUC__) && __GNUC__ < 5 && !defined(__clang__)
  474. # pragma GCC diagnostic push
  475. # pragma GCC diagnostic ignored "-Wmissing-field-initializers"
  476. #endif
  477. this->next_value_ = {};
  478. #if defined(__GNUC__) && __GNUC__ < 5 && !defined(__clang__)
  479. # pragma GCC diagnostic pop
  480. #endif
  481. }
  482. bool signal_end(error_code& ec)
  483. {
  484. if( !check_inserter( inserter, value_->end() ) )
  485. {
  486. BOOST_JSON_FAIL( ec, error::size_mismatch );
  487. return false;
  488. }
  489. inserter = detail::inserter(*value_, inserter_implementation<V>());
  490. return converting_handler::composite_handler::signal_end(ec);
  491. }
  492. bool on_array_begin( error_code& ec )
  493. {
  494. if( this->inner_active_ )
  495. return this->inner_.on_array_begin( ec );
  496. this->inner_active_ = true;
  497. clear_container( *value_, inserter_implementation<V>() );
  498. return true;
  499. }
  500. bool on_array_end( error_code& ec )
  501. {
  502. if( this->inner_active_ )
  503. return this->inner_.on_array_end( ec );
  504. return this->parent_->signal_end(ec);
  505. }
  506. };
  507. // map handler
  508. template< class V, class P >
  509. class converting_handler<map_like_conversion_tag, V, P>
  510. : public composite_handler<
  511. converting_handler<map_like_conversion_tag, V, P>,
  512. detail::mapped_type<V>,
  513. P,
  514. error::not_object>
  515. {
  516. private:
  517. V* value_;
  518. std::string key_;
  519. public:
  520. converting_handler( V* v, P* p )
  521. : converting_handler::composite_handler(p), value_(v)
  522. {}
  523. void signal_value()
  524. {
  525. value_->emplace( std::move(key_), std::move(this->next_value_) );
  526. key_ = {};
  527. this->next_value_ = {};
  528. this->inner_active_ = false;
  529. }
  530. bool on_object_begin( error_code& ec )
  531. {
  532. if( this->inner_active_ )
  533. return this->inner_.on_object_begin(ec);
  534. clear_container( *value_, inserter_implementation<V>() );
  535. return true;
  536. }
  537. bool on_object_end( error_code& ec )
  538. {
  539. if( this->inner_active_ )
  540. return this->inner_.on_object_end(ec);
  541. this->parent_->signal_value();
  542. return true;
  543. }
  544. bool on_array_end( error_code& ec )
  545. {
  546. if( this->inner_active_ )
  547. return this->inner_.on_array_end(ec);
  548. return this->parent_->signal_end(ec);
  549. }
  550. bool on_key_part( error_code& ec, string_view sv )
  551. {
  552. if( this->inner_active_ )
  553. return this->inner_.on_key_part(ec, sv);
  554. key_.append( sv.data(), sv.size() );
  555. return true;
  556. }
  557. bool on_key( error_code& ec, string_view sv )
  558. {
  559. if( this->inner_active_ )
  560. return this->inner_.on_key(ec, sv);
  561. key_.append( sv.data(), sv.size() );
  562. this->inner_active_ = true;
  563. return true;
  564. }
  565. };
  566. // tuple handler
  567. template<std::size_t I, class T>
  568. struct handler_tuple_element
  569. {
  570. template< class... Args >
  571. handler_tuple_element( Args&& ... args )
  572. : t_( static_cast<Args&&>(args)... )
  573. {}
  574. T t_;
  575. };
  576. template<std::size_t I, class T>
  577. T&
  578. get( handler_tuple_element<I, T>& e )
  579. {
  580. return e.t_;
  581. }
  582. template<
  583. class P,
  584. class LV,
  585. class S = mp11::make_index_sequence<mp11::mp_size<LV>::value> >
  586. struct handler_tuple;
  587. template< class P, template<class...> class L, class... V, std::size_t... I >
  588. struct handler_tuple< P, L<V...>, mp11::index_sequence<I...> >
  589. : handler_tuple_element< I, get_handler<V, P> >
  590. ...
  591. {
  592. handler_tuple( handler_tuple const& ) = delete;
  593. handler_tuple& operator=( handler_tuple const& ) = delete;
  594. template< class Access, class T >
  595. handler_tuple( Access access, T* pv, P* pp )
  596. : handler_tuple_element< I, get_handler<V, P> >(
  597. access( pv, mp11::mp_int<I>() ),
  598. pp )
  599. ...
  600. { }
  601. };
  602. #if defined(BOOST_MSVC) && BOOST_MSVC < 1910
  603. template< class T >
  604. struct tuple_element_list_impl
  605. {
  606. template< class I >
  607. using tuple_element_helper = tuple_element_t<I::value, T>;
  608. using type = mp11::mp_transform<
  609. tuple_element_helper,
  610. mp11::mp_iota< std::tuple_size<T> > >;
  611. };
  612. template< class T >
  613. using tuple_element_list = typename tuple_element_list_impl<T>::type;
  614. #else
  615. template< class I, class T >
  616. using tuple_element_helper = tuple_element_t<I::value, T>;
  617. template< class T >
  618. using tuple_element_list = mp11::mp_transform_q<
  619. mp11::mp_bind_back< tuple_element_helper, T>,
  620. mp11::mp_iota< std::tuple_size<T> > >;
  621. #endif
  622. template< class Op, class... Args>
  623. struct handler_op_invoker
  624. {
  625. public:
  626. std::tuple<Args&...> args;
  627. template< class Handler >
  628. bool
  629. operator()( Handler& handler ) const
  630. {
  631. return (*this)( handler, mp11::index_sequence_for<Args...>() );
  632. }
  633. private:
  634. template< class Handler, std::size_t... I >
  635. bool
  636. operator()( Handler& handler, mp11::index_sequence<I...> ) const
  637. {
  638. return Op()( handler, std::get<I>(args)... );
  639. }
  640. };
  641. template< class Handlers, class F >
  642. struct tuple_handler_op_invoker
  643. {
  644. Handlers& handlers;
  645. F fn;
  646. template< class I >
  647. bool
  648. operator()( I ) const
  649. {
  650. return fn( get<I::value>(handlers) );
  651. }
  652. };
  653. struct tuple_accessor
  654. {
  655. template< class T, class I >
  656. auto operator()( T* t, I ) const -> tuple_element_t<I::value, T>*
  657. {
  658. using std::get;
  659. return &get<I::value>(*t);
  660. }
  661. };
  662. template< class T, class P >
  663. class converting_handler<tuple_conversion_tag, T, P>
  664. {
  665. private:
  666. T* value_;
  667. P* parent_;
  668. handler_tuple< converting_handler, tuple_element_list<T> > handlers_;
  669. int inner_active_ = -1;
  670. public:
  671. converting_handler( converting_handler const& ) = delete;
  672. converting_handler& operator=( converting_handler const& ) = delete;
  673. converting_handler( T* v, P* p )
  674. : value_(v) , parent_(p) , handlers_(tuple_accessor(), v, this)
  675. {}
  676. void signal_value()
  677. {
  678. ++inner_active_;
  679. }
  680. bool signal_end(error_code& ec)
  681. {
  682. constexpr int N = std::tuple_size<T>::value;
  683. if( inner_active_ < N )
  684. {
  685. BOOST_JSON_FAIL( ec, error::size_mismatch );
  686. return true;
  687. }
  688. inner_active_ = -1;
  689. parent_->signal_value();
  690. return true;
  691. }
  692. #define BOOST_JSON_HANDLE_EVENT(fn) \
  693. struct do_ ## fn \
  694. { \
  695. template< class H, class... Args > \
  696. bool operator()( H& h, Args& ... args ) const \
  697. { \
  698. return h. fn (args...); \
  699. } \
  700. }; \
  701. \
  702. template< class... Args > \
  703. bool fn( error_code& ec, Args&& ... args ) \
  704. { \
  705. if( inner_active_ < 0 ) \
  706. { \
  707. BOOST_JSON_FAIL( ec, error::not_array ); \
  708. return false; \
  709. } \
  710. constexpr int N = std::tuple_size<T>::value; \
  711. if( inner_active_ >= N ) \
  712. { \
  713. BOOST_JSON_FAIL( ec, error::size_mismatch ); \
  714. return false; \
  715. } \
  716. using F = handler_op_invoker< do_ ## fn, error_code, Args...>; \
  717. using H = decltype(handlers_); \
  718. return mp11::mp_with_index<N>( \
  719. inner_active_, \
  720. tuple_handler_op_invoker<H, F>{ \
  721. handlers_, \
  722. F{ std::forward_as_tuple(ec, args...) } } ); \
  723. }
  724. BOOST_JSON_HANDLE_EVENT( on_object_begin );
  725. BOOST_JSON_HANDLE_EVENT( on_object_end );
  726. struct do_on_array_begin
  727. {
  728. handler_tuple< converting_handler, tuple_element_list<T> >& handlers;
  729. error_code& ec;
  730. template< class I >
  731. bool operator()( I ) const
  732. {
  733. return get<I::value>(handlers).on_array_begin(ec);
  734. };
  735. };
  736. bool on_array_begin( error_code& ec )
  737. {
  738. if( inner_active_ < 0 )
  739. {
  740. inner_active_ = 0;
  741. return true;
  742. }
  743. constexpr int N = std::tuple_size<T>::value;
  744. if( inner_active_ >= N )
  745. {
  746. inner_active_ = 0;
  747. return true;
  748. }
  749. return mp11::mp_with_index<N>(
  750. inner_active_, do_on_array_begin{handlers_, ec} );
  751. }
  752. struct do_on_array_end
  753. {
  754. handler_tuple< converting_handler, tuple_element_list<T> >& handlers;
  755. error_code& ec;
  756. template< class I >
  757. bool operator()( I ) const
  758. {
  759. return get<I::value>(handlers).on_array_end(ec);
  760. };
  761. };
  762. bool on_array_end( error_code& ec )
  763. {
  764. if( inner_active_ < 0 )
  765. return parent_->signal_end(ec);
  766. constexpr int N = std::tuple_size<T>::value;
  767. if( inner_active_ >= N )
  768. return signal_end(ec);
  769. return mp11::mp_with_index<N>(
  770. inner_active_, do_on_array_end{handlers_, ec} );
  771. }
  772. BOOST_JSON_HANDLE_EVENT( on_key_part );
  773. BOOST_JSON_HANDLE_EVENT( on_key );
  774. BOOST_JSON_HANDLE_EVENT( on_string_part );
  775. BOOST_JSON_HANDLE_EVENT( on_string );
  776. BOOST_JSON_HANDLE_EVENT( on_number_part );
  777. BOOST_JSON_HANDLE_EVENT( on_int64 );
  778. BOOST_JSON_HANDLE_EVENT( on_uint64 );
  779. BOOST_JSON_HANDLE_EVENT( on_double );
  780. BOOST_JSON_HANDLE_EVENT( on_bool );
  781. BOOST_JSON_HANDLE_EVENT( on_null );
  782. #undef BOOST_JSON_HANDLE_EVENT
  783. };
  784. // described struct handler
  785. #if defined(BOOST_MSVC) && BOOST_MSVC < 1910
  786. template< class T >
  787. struct struct_element_list_impl
  788. {
  789. template< class D >
  790. using helper = described_member_t<T, D>;
  791. using type = mp11::mp_transform<
  792. helper,
  793. describe::describe_members<T, describe::mod_public> >;
  794. };
  795. template< class T >
  796. using struct_element_list = typename struct_element_list_impl<T>::type;
  797. #else
  798. template< class T >
  799. using struct_element_list = mp11::mp_transform_q<
  800. mp11::mp_bind_front< described_member_t, T >,
  801. describe::describe_members<T, describe::mod_public> >;
  802. #endif
  803. struct struct_accessor
  804. {
  805. template< class T, class I >
  806. auto operator()( T* t, I ) const
  807. -> described_member_t<T,
  808. mp11::mp_at<
  809. describe::describe_members<T, describe::mod_public>, I> >*
  810. {
  811. using Ds = describe::describe_members<T, describe::mod_public>;
  812. using D = mp11::mp_at<Ds, I>;
  813. return &(t->*D::pointer);
  814. }
  815. };
  816. template< class F >
  817. struct struct_key_searcher
  818. {
  819. F fn;
  820. template< class D >
  821. void
  822. operator()( D ) const
  823. {
  824. fn( D::name ) ;
  825. }
  826. };
  827. template<class V, class P>
  828. class converting_handler<described_class_conversion_tag, V, P>
  829. {
  830. #if !defined(BOOST_DESCRIBE_CXX14)
  831. static_assert(
  832. sizeof(V) == 0, "Struct support for parse_into requires C++14" );
  833. #else
  834. private:
  835. V* value_;
  836. P* parent_;
  837. std::string key_;
  838. using Dm = describe::describe_members<V, describe::mod_public>;
  839. handler_tuple< converting_handler, struct_element_list<V> > handlers_;
  840. int inner_active_ = -1;
  841. std::size_t activated_ = 0;
  842. public:
  843. converting_handler( converting_handler const& ) = delete;
  844. converting_handler& operator=( converting_handler const& ) = delete;
  845. converting_handler( V* v, P* p )
  846. : value_(v), parent_(p), handlers_(struct_accessor(), v, this)
  847. {}
  848. struct is_optional_checker
  849. {
  850. template< class I >
  851. bool operator()( I ) const noexcept
  852. {
  853. using L = struct_element_list<V>;
  854. using T = mp11::mp_at<L, I>;
  855. return !is_optional_like<T>::value;
  856. }
  857. };
  858. void signal_value()
  859. {
  860. BOOST_ASSERT( inner_active_ >= 0 );
  861. bool required_member = mp11::mp_with_index< mp11::mp_size<Dm> >(
  862. inner_active_,
  863. is_optional_checker{});
  864. if( required_member )
  865. ++activated_;
  866. key_ = {};
  867. inner_active_ = -1;
  868. }
  869. bool signal_end(error_code&)
  870. {
  871. key_ = {};
  872. inner_active_ = -1;
  873. parent_->signal_value();
  874. return true;
  875. }
  876. #define BOOST_JSON_INVOKE_INNER(fn) \
  877. if( inner_active_ < 0 ) \
  878. { \
  879. BOOST_JSON_FAIL( ec, error::not_object ); \
  880. return false; \
  881. } \
  882. auto f = [&](auto& handler) { return handler.fn ; }; \
  883. using F = decltype(f); \
  884. using H = decltype(handlers_); \
  885. return mp11::mp_with_index< mp11::mp_size<Dm> >( \
  886. inner_active_, \
  887. tuple_handler_op_invoker<H, F>{handlers_, f} );
  888. bool on_object_begin( error_code& ec )
  889. {
  890. if( inner_active_ < 0 )
  891. return true;
  892. BOOST_JSON_INVOKE_INNER( on_object_begin(ec) );
  893. }
  894. bool on_object_end( error_code& ec )
  895. {
  896. if( inner_active_ < 0 )
  897. {
  898. using L = struct_element_list<V>;
  899. using C = mp11::mp_count_if<L, is_optional_like>;
  900. constexpr int N = mp11::mp_size<L>::value - C::value;
  901. if( activated_ < N )
  902. {
  903. BOOST_JSON_FAIL( ec, error::size_mismatch );
  904. return false;
  905. }
  906. parent_->signal_value();
  907. return true;
  908. }
  909. BOOST_JSON_INVOKE_INNER( on_object_end(ec) );
  910. }
  911. bool on_array_begin( error_code& ec )
  912. {
  913. BOOST_JSON_INVOKE_INNER( on_array_begin(ec) );
  914. }
  915. bool on_array_end( error_code& ec )
  916. {
  917. if( inner_active_ < 0 )
  918. return parent_->signal_end(ec);
  919. BOOST_JSON_INVOKE_INNER( on_array_end(ec) );
  920. }
  921. bool on_key_part( error_code& ec, string_view sv )
  922. {
  923. if( inner_active_ < 0 )
  924. {
  925. key_.append( sv.data(), sv.size() );
  926. return true;
  927. }
  928. BOOST_JSON_INVOKE_INNER( on_key_part(ec, sv) );
  929. }
  930. bool on_key( error_code& ec, string_view sv )
  931. {
  932. if( inner_active_ >= 0 )
  933. {
  934. BOOST_JSON_INVOKE_INNER( on_key(ec, sv) );
  935. }
  936. string_view key = sv;
  937. if( !key_.empty() )
  938. {
  939. key_.append( sv.data(), sv.size() );
  940. key = key_;
  941. }
  942. int i = 0;
  943. auto f = [&](char const* name)
  944. {
  945. if( key == name )
  946. inner_active_ = i;
  947. ++i;
  948. };
  949. mp11::mp_for_each<Dm>(
  950. struct_key_searcher<decltype(f)>{f} );
  951. if( inner_active_ < 0 )
  952. {
  953. BOOST_JSON_FAIL(ec, error::unknown_name);
  954. return false;
  955. }
  956. return true;
  957. }
  958. bool on_string_part( error_code& ec, string_view sv )
  959. {
  960. BOOST_JSON_INVOKE_INNER( on_string_part(ec, sv) );
  961. }
  962. bool on_string( error_code& ec, string_view sv )
  963. {
  964. BOOST_JSON_INVOKE_INNER( on_string(ec, sv) );
  965. }
  966. bool on_number_part( error_code& ec )
  967. {
  968. BOOST_JSON_INVOKE_INNER( on_number_part(ec) );
  969. }
  970. bool on_int64( error_code& ec, std::int64_t v )
  971. {
  972. BOOST_JSON_INVOKE_INNER( on_int64(ec, v) );
  973. }
  974. bool on_uint64( error_code& ec, std::uint64_t v )
  975. {
  976. BOOST_JSON_INVOKE_INNER( on_uint64(ec, v) );
  977. }
  978. bool on_double( error_code& ec, double v )
  979. {
  980. BOOST_JSON_INVOKE_INNER( on_double(ec, v) );
  981. }
  982. bool on_bool( error_code& ec, bool v )
  983. {
  984. BOOST_JSON_INVOKE_INNER( on_bool(ec, v) );
  985. }
  986. bool on_null( error_code& ec )
  987. {
  988. BOOST_JSON_INVOKE_INNER( on_null(ec) );
  989. }
  990. #undef BOOST_JSON_INVOKE_INNER
  991. #endif
  992. };
  993. // variant handler
  994. struct object_begin_handler_event
  995. { };
  996. struct object_end_handler_event
  997. { };
  998. struct array_begin_handler_event
  999. { };
  1000. struct array_end_handler_event
  1001. { };
  1002. struct key_handler_event
  1003. {
  1004. std::string value;
  1005. };
  1006. struct string_handler_event
  1007. {
  1008. std::string value;
  1009. };
  1010. struct int64_handler_event
  1011. {
  1012. std::int64_t value;
  1013. };
  1014. struct uint64_handler_event
  1015. {
  1016. std::uint64_t value;
  1017. };
  1018. struct double_handler_event
  1019. {
  1020. double value;
  1021. };
  1022. struct bool_handler_event
  1023. {
  1024. bool value;
  1025. };
  1026. struct null_handler_event
  1027. { };
  1028. using parse_event = variant2::variant<
  1029. object_begin_handler_event,
  1030. object_end_handler_event,
  1031. array_begin_handler_event,
  1032. array_end_handler_event,
  1033. key_handler_event,
  1034. string_handler_event,
  1035. int64_handler_event,
  1036. uint64_handler_event,
  1037. double_handler_event,
  1038. bool_handler_event,
  1039. null_handler_event>;
  1040. template< class H >
  1041. struct event_visitor
  1042. {
  1043. H& handler;
  1044. error_code& ec;
  1045. bool
  1046. operator()(object_begin_handler_event&) const
  1047. {
  1048. return handler.on_object_begin(ec);
  1049. }
  1050. bool
  1051. operator()(object_end_handler_event&) const
  1052. {
  1053. return handler.on_object_end(ec);
  1054. }
  1055. bool
  1056. operator()(array_begin_handler_event&) const
  1057. {
  1058. return handler.on_array_begin(ec);
  1059. }
  1060. bool
  1061. operator()(array_end_handler_event&) const
  1062. {
  1063. return handler.on_array_end(ec);
  1064. }
  1065. bool
  1066. operator()(key_handler_event& ev) const
  1067. {
  1068. return handler.on_key(ec, ev.value);
  1069. }
  1070. bool
  1071. operator()(string_handler_event& ev) const
  1072. {
  1073. return handler.on_string(ec, ev.value);
  1074. }
  1075. bool
  1076. operator()(int64_handler_event& ev) const
  1077. {
  1078. return handler.on_int64(ec, ev.value);
  1079. }
  1080. bool
  1081. operator()(uint64_handler_event& ev) const
  1082. {
  1083. return handler.on_uint64(ec, ev.value);
  1084. }
  1085. bool
  1086. operator()(double_handler_event& ev) const
  1087. {
  1088. return handler.on_double(ec, ev.value);
  1089. }
  1090. bool
  1091. operator()(bool_handler_event& ev) const
  1092. {
  1093. return handler.on_bool(ec, ev.value);
  1094. }
  1095. bool
  1096. operator()(null_handler_event&) const
  1097. {
  1098. return handler.on_null(ec);
  1099. }
  1100. };
  1101. // L<T...> -> variant< monostate, get_handler<T, P>... >
  1102. template< class P, class L >
  1103. using inner_handler_variant = mp11::mp_push_front<
  1104. mp11::mp_transform_q<
  1105. mp11::mp_bind_back<get_handler, P>,
  1106. mp11::mp_apply<variant2::variant, L>>,
  1107. variant2::monostate>;
  1108. template< class T, class P >
  1109. class converting_handler<variant_conversion_tag, T, P>
  1110. {
  1111. private:
  1112. using variant_size = mp11::mp_size<T>;
  1113. T* value_;
  1114. P* parent_;
  1115. std::string string_;
  1116. std::vector< parse_event > events_;
  1117. inner_handler_variant<converting_handler, T> inner_;
  1118. int inner_active_ = -1;
  1119. public:
  1120. converting_handler( converting_handler const& ) = delete;
  1121. converting_handler& operator=( converting_handler const& ) = delete;
  1122. converting_handler( T* v, P* p )
  1123. : value_( v )
  1124. , parent_( p )
  1125. {}
  1126. void signal_value()
  1127. {
  1128. inner_.template emplace<0>();
  1129. inner_active_ = -1;
  1130. events_.clear();
  1131. parent_->signal_value();
  1132. }
  1133. bool signal_end(error_code& ec)
  1134. {
  1135. return parent_->signal_end(ec);
  1136. }
  1137. struct alternative_selector
  1138. {
  1139. converting_handler* self;
  1140. template< class I >
  1141. void
  1142. operator()( I ) const
  1143. {
  1144. using V = mp11::mp_at<T, I>;
  1145. auto& v = self->value_->template emplace<I::value>( V{} );
  1146. self->inner_.template emplace<I::value + 1>(&v, self);
  1147. }
  1148. };
  1149. void
  1150. next_alternative()
  1151. {
  1152. if( ++inner_active_ >= static_cast<int>(variant_size::value) )
  1153. return;
  1154. mp11::mp_with_index< variant_size::value >(
  1155. inner_active_, alternative_selector{this} );
  1156. }
  1157. struct event_processor
  1158. {
  1159. converting_handler* self;
  1160. error_code& ec;
  1161. parse_event& event;
  1162. template< class I >
  1163. bool operator()( I ) const
  1164. {
  1165. auto& handler = variant2::get<I::value + 1>(self->inner_);
  1166. using Handler = remove_cvref<decltype(handler)>;
  1167. return variant2::visit(
  1168. event_visitor<Handler>{handler, ec}, event );
  1169. }
  1170. };
  1171. bool process_events(error_code& ec)
  1172. {
  1173. constexpr std::size_t N = variant_size::value;
  1174. // should be pointers not iterators, otherwise MSVC crashes
  1175. auto const last = events_.data() + events_.size();
  1176. auto first = last - 1;
  1177. bool ok = false;
  1178. if( inner_active_ < 0 )
  1179. next_alternative();
  1180. do
  1181. {
  1182. if( static_cast<std::size_t>(inner_active_) >= N )
  1183. {
  1184. BOOST_JSON_FAIL( ec, error::exhausted_variants );
  1185. return false;
  1186. }
  1187. for ( ; first != last; ++first )
  1188. {
  1189. ok = mp11::mp_with_index< N >(
  1190. inner_active_, event_processor{this, ec, *first} );
  1191. if( !ok )
  1192. {
  1193. first = events_.data();
  1194. next_alternative();
  1195. ec.clear();
  1196. break;
  1197. }
  1198. }
  1199. }
  1200. while( !ok );
  1201. return true;
  1202. }
  1203. #define BOOST_JSON_INVOKE_INNER(ev, ec) \
  1204. events_.emplace_back( ev ); \
  1205. return process_events(ec);
  1206. bool on_object_begin( error_code& ec )
  1207. {
  1208. BOOST_JSON_INVOKE_INNER( object_begin_handler_event{}, ec );
  1209. }
  1210. bool on_object_end( error_code& ec )
  1211. {
  1212. BOOST_JSON_INVOKE_INNER( object_end_handler_event{}, ec );
  1213. }
  1214. bool on_array_begin( error_code& ec )
  1215. {
  1216. BOOST_JSON_INVOKE_INNER( array_begin_handler_event{}, ec );
  1217. }
  1218. bool on_array_end( error_code& ec )
  1219. {
  1220. if( !inner_active_ )
  1221. return signal_end(ec);
  1222. BOOST_JSON_INVOKE_INNER( array_end_handler_event{}, ec );
  1223. }
  1224. bool on_key_part( error_code&, string_view sv )
  1225. {
  1226. string_.append(sv);
  1227. return true;
  1228. }
  1229. bool on_key( error_code& ec, string_view sv )
  1230. {
  1231. string_.append(sv);
  1232. BOOST_JSON_INVOKE_INNER( key_handler_event{ std::move(string_) }, ec );
  1233. }
  1234. bool on_string_part( error_code&, string_view sv )
  1235. {
  1236. string_.append(sv);
  1237. return true;
  1238. }
  1239. bool on_string( error_code& ec, string_view sv )
  1240. {
  1241. string_.append(sv);
  1242. BOOST_JSON_INVOKE_INNER(
  1243. string_handler_event{ std::move(string_) }, ec );
  1244. }
  1245. bool on_number_part( error_code& )
  1246. {
  1247. return true;
  1248. }
  1249. bool on_int64( error_code& ec, std::int64_t v )
  1250. {
  1251. BOOST_JSON_INVOKE_INNER( int64_handler_event{v}, ec );
  1252. }
  1253. bool on_uint64( error_code& ec, std::uint64_t v )
  1254. {
  1255. BOOST_JSON_INVOKE_INNER( uint64_handler_event{v}, ec );
  1256. }
  1257. bool on_double( error_code& ec, double v )
  1258. {
  1259. BOOST_JSON_INVOKE_INNER( double_handler_event{v}, ec );
  1260. }
  1261. bool on_bool( error_code& ec, bool v )
  1262. {
  1263. BOOST_JSON_INVOKE_INNER( bool_handler_event{v}, ec );
  1264. }
  1265. bool on_null( error_code& ec )
  1266. {
  1267. BOOST_JSON_INVOKE_INNER( null_handler_event{}, ec );
  1268. }
  1269. #undef BOOST_JSON_INVOKE_INNER
  1270. };
  1271. // optional handler
  1272. template<class V, class P>
  1273. class converting_handler<optional_conversion_tag, V, P>
  1274. {
  1275. private:
  1276. using inner_type = value_result_type<V>;
  1277. using inner_handler_type = get_handler<inner_type, converting_handler>;
  1278. V* value_;
  1279. P* parent_;
  1280. inner_type inner_value_ = {};
  1281. inner_handler_type inner_;
  1282. bool inner_active_ = false;
  1283. public:
  1284. converting_handler( converting_handler const& ) = delete;
  1285. converting_handler& operator=( converting_handler const& ) = delete;
  1286. converting_handler( V* v, P* p )
  1287. : value_(v), parent_(p), inner_(&inner_value_, this)
  1288. {}
  1289. void signal_value()
  1290. {
  1291. *value_ = std::move(inner_value_);
  1292. inner_active_ = false;
  1293. parent_->signal_value();
  1294. }
  1295. bool signal_end(error_code& ec)
  1296. {
  1297. return parent_->signal_end(ec);
  1298. }
  1299. #define BOOST_JSON_INVOKE_INNER(fn) \
  1300. if( !inner_active_ ) \
  1301. inner_active_ = true; \
  1302. return inner_.fn;
  1303. bool on_object_begin( error_code& ec )
  1304. {
  1305. BOOST_JSON_INVOKE_INNER( on_object_begin(ec) );
  1306. }
  1307. bool on_object_end( error_code& ec )
  1308. {
  1309. BOOST_JSON_INVOKE_INNER( on_object_end(ec) );
  1310. }
  1311. bool on_array_begin( error_code& ec )
  1312. {
  1313. BOOST_JSON_INVOKE_INNER( on_array_begin(ec) );
  1314. }
  1315. bool on_array_end( error_code& ec )
  1316. {
  1317. if( !inner_active_ )
  1318. return signal_end(ec);
  1319. BOOST_JSON_INVOKE_INNER( on_array_end(ec) );
  1320. }
  1321. bool on_key_part( error_code& ec, string_view sv )
  1322. {
  1323. BOOST_JSON_INVOKE_INNER( on_key_part(ec, sv) );
  1324. }
  1325. bool on_key( error_code& ec, string_view sv )
  1326. {
  1327. BOOST_JSON_INVOKE_INNER( on_key(ec, sv) );
  1328. }
  1329. bool on_string_part( error_code& ec, string_view sv )
  1330. {
  1331. BOOST_JSON_INVOKE_INNER( on_string_part(ec, sv) );
  1332. }
  1333. bool on_string( error_code& ec, string_view sv )
  1334. {
  1335. BOOST_JSON_INVOKE_INNER( on_string(ec, sv) );
  1336. }
  1337. bool on_number_part( error_code& ec )
  1338. {
  1339. BOOST_JSON_INVOKE_INNER( on_number_part(ec) );
  1340. }
  1341. bool on_int64( error_code& ec, std::int64_t v )
  1342. {
  1343. BOOST_JSON_INVOKE_INNER( on_int64(ec, v) );
  1344. }
  1345. bool on_uint64( error_code& ec, std::uint64_t v )
  1346. {
  1347. BOOST_JSON_INVOKE_INNER( on_uint64(ec, v) );
  1348. }
  1349. bool on_double( error_code& ec, double v )
  1350. {
  1351. BOOST_JSON_INVOKE_INNER( on_double(ec, v) );
  1352. }
  1353. bool on_bool( error_code& ec, bool v )
  1354. {
  1355. BOOST_JSON_INVOKE_INNER( on_bool(ec, v) );
  1356. }
  1357. bool on_null( error_code& ec )
  1358. {
  1359. if( !inner_active_ )
  1360. {
  1361. *value_ = {};
  1362. this->parent_->signal_value();
  1363. return true;
  1364. }
  1365. else
  1366. {
  1367. return inner_.on_null(ec);
  1368. }
  1369. }
  1370. #undef BOOST_JSON_INVOKE_INNER
  1371. };
  1372. // into_handler
  1373. template< class V >
  1374. class into_handler
  1375. {
  1376. private:
  1377. using inner_handler_type = get_handler<V, into_handler>;
  1378. inner_handler_type inner_;
  1379. bool inner_active_ = true;
  1380. public:
  1381. into_handler( into_handler const& ) = delete;
  1382. into_handler& operator=( into_handler const& ) = delete;
  1383. public:
  1384. static constexpr std::size_t max_object_size = object::max_size();
  1385. static constexpr std::size_t max_array_size = array::max_size();
  1386. static constexpr std::size_t max_key_size = string::max_size();
  1387. static constexpr std::size_t max_string_size = string::max_size();
  1388. public:
  1389. explicit into_handler( V* v ): inner_( v, this )
  1390. {
  1391. }
  1392. void signal_value()
  1393. {
  1394. }
  1395. bool signal_end(error_code&)
  1396. {
  1397. return true;
  1398. }
  1399. bool on_document_begin( error_code& )
  1400. {
  1401. return true;
  1402. }
  1403. bool on_document_end( error_code& )
  1404. {
  1405. inner_active_ = false;
  1406. return true;
  1407. }
  1408. #define BOOST_JSON_INVOKE_INNER(f) \
  1409. if( !inner_active_ ) \
  1410. { \
  1411. BOOST_JSON_FAIL( ec, error::extra_data ); \
  1412. return false; \
  1413. } \
  1414. else \
  1415. return inner_.f
  1416. bool on_object_begin( error_code& ec )
  1417. {
  1418. BOOST_JSON_INVOKE_INNER( on_object_begin(ec) );
  1419. }
  1420. bool on_object_end( std::size_t, error_code& ec )
  1421. {
  1422. BOOST_JSON_INVOKE_INNER( on_object_end(ec) );
  1423. }
  1424. bool on_array_begin( error_code& ec )
  1425. {
  1426. BOOST_JSON_INVOKE_INNER( on_array_begin(ec) );
  1427. }
  1428. bool on_array_end( std::size_t, error_code& ec )
  1429. {
  1430. BOOST_JSON_INVOKE_INNER( on_array_end(ec) );
  1431. }
  1432. bool on_key_part( string_view sv, std::size_t, error_code& ec )
  1433. {
  1434. BOOST_JSON_INVOKE_INNER( on_key_part(ec, sv) );
  1435. }
  1436. bool on_key( string_view sv, std::size_t, error_code& ec )
  1437. {
  1438. BOOST_JSON_INVOKE_INNER( on_key(ec, sv) );
  1439. }
  1440. bool on_string_part( string_view sv, std::size_t, error_code& ec )
  1441. {
  1442. BOOST_JSON_INVOKE_INNER( on_string_part(ec, sv) );
  1443. }
  1444. bool on_string( string_view sv, std::size_t, error_code& ec )
  1445. {
  1446. BOOST_JSON_INVOKE_INNER( on_string(ec, sv) );
  1447. }
  1448. bool on_number_part( string_view, error_code& ec )
  1449. {
  1450. BOOST_JSON_INVOKE_INNER( on_number_part(ec) );
  1451. }
  1452. bool on_int64( std::int64_t v, string_view, error_code& ec )
  1453. {
  1454. BOOST_JSON_INVOKE_INNER( on_int64(ec, v) );
  1455. }
  1456. bool on_uint64( std::uint64_t v, string_view, error_code& ec )
  1457. {
  1458. BOOST_JSON_INVOKE_INNER( on_uint64(ec, v) );
  1459. }
  1460. bool on_double( double v, string_view, error_code& ec )
  1461. {
  1462. BOOST_JSON_INVOKE_INNER( on_double(ec, v) );
  1463. }
  1464. bool on_bool( bool v, error_code& ec )
  1465. {
  1466. BOOST_JSON_INVOKE_INNER( on_bool(ec, v) );
  1467. }
  1468. bool on_null( error_code& ec )
  1469. {
  1470. BOOST_JSON_INVOKE_INNER( on_null(ec) );
  1471. }
  1472. bool on_comment_part(string_view, error_code&)
  1473. {
  1474. return true;
  1475. }
  1476. bool on_comment(string_view, error_code&)
  1477. {
  1478. return true;
  1479. }
  1480. #undef BOOST_JSON_INVOKE_INNER
  1481. };
  1482. } // namespace detail
  1483. } // namespace boost
  1484. } // namespace json
  1485. #endif