parse_into.hpp 44 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845
  1. //
  2. // Copyright (c) 2019 Vinnie Falco ([email protected])
  3. // Copyright (c) 2020 Krystian Stasiowski ([email protected])
  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_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( 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&)
  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( system::error_code& ec )
  152. {
  153. BOOST_JSON_INVOKE_INNER( on_object_begin(ec) );
  154. }
  155. bool on_object_end( system::error_code& ec )
  156. {
  157. BOOST_JSON_INVOKE_INNER( on_object_end(ec) );
  158. }
  159. bool on_array_begin( system::error_code& ec )
  160. {
  161. BOOST_JSON_INVOKE_INNER( on_array_begin(ec) );
  162. }
  163. bool on_array_end( system::error_code& ec )
  164. {
  165. BOOST_JSON_INVOKE_INNER( on_array_end(ec) );
  166. }
  167. bool on_key_part( system::error_code& ec, string_view sv )
  168. {
  169. BOOST_JSON_INVOKE_INNER( on_key_part(ec, sv) );
  170. }
  171. bool on_key( system::error_code& ec, string_view sv )
  172. {
  173. BOOST_JSON_INVOKE_INNER( on_key(ec, sv) );
  174. }
  175. bool on_string_part( system::error_code& ec, string_view sv )
  176. {
  177. BOOST_JSON_INVOKE_INNER( on_string_part(ec, sv) );
  178. }
  179. bool on_string( system::error_code& ec, string_view sv )
  180. {
  181. BOOST_JSON_INVOKE_INNER( on_string(ec, sv) );
  182. }
  183. bool on_number_part( system::error_code& ec )
  184. {
  185. BOOST_JSON_INVOKE_INNER( on_number_part(ec) );
  186. }
  187. bool on_int64( system::error_code& ec, std::int64_t v )
  188. {
  189. BOOST_JSON_INVOKE_INNER( on_int64(ec, v) );
  190. }
  191. bool on_uint64( system::error_code& ec, std::uint64_t v )
  192. {
  193. BOOST_JSON_INVOKE_INNER( on_uint64(ec, v) );
  194. }
  195. bool on_double( system::error_code& ec, double v )
  196. {
  197. BOOST_JSON_INVOKE_INNER( on_double(ec, v) );
  198. }
  199. bool on_bool( system::error_code& ec, bool v )
  200. {
  201. BOOST_JSON_INVOKE_INNER( on_bool(ec, v) );
  202. }
  203. bool on_null( system::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( system::error_code& )
  239. {
  240. return true;
  241. }
  242. bool on_int64( system::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( system::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( system::error_code& )
  278. {
  279. return true;
  280. }
  281. bool on_int64( system::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( system::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( system::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( system::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( system::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( system::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( system::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( system::error_code&, string_view sv )
  390. {
  391. name_.append( sv.begin(), sv.end() );
  392. return true;
  393. }
  394. bool on_string( system::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(system::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( system::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( system::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( system::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( system::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( system::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( system::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( system::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(system::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( system::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, system::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. system::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( system::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. system::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( system::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< helper, described_members<T> >;
  792. };
  793. template< class T >
  794. using struct_element_list = typename struct_element_list_impl<T>::type;
  795. #else
  796. template< class T >
  797. using struct_element_list = mp11::mp_transform_q<
  798. mp11::mp_bind_front< described_member_t, T >, described_members<T> >;
  799. #endif
  800. struct struct_accessor
  801. {
  802. template< class T, class I >
  803. auto operator()( T* t, I ) const
  804. -> described_member_t<T, mp11::mp_at< described_members<T>, I> >*
  805. {
  806. using Ds = described_members<T>;
  807. using D = mp11::mp_at<Ds, I>;
  808. return &(t->*D::pointer);
  809. }
  810. };
  811. template< class F >
  812. struct struct_key_searcher
  813. {
  814. F fn;
  815. template< class D >
  816. void
  817. operator()( D ) const
  818. {
  819. fn( D::name ) ;
  820. }
  821. };
  822. template<class V, class P>
  823. class converting_handler<described_class_conversion_tag, V, P>
  824. {
  825. #if !defined(BOOST_DESCRIBE_CXX14)
  826. static_assert(
  827. sizeof(V) == 0, "Struct support for parse_into requires C++14" );
  828. #else
  829. private:
  830. V* value_;
  831. P* parent_;
  832. std::string key_;
  833. using Dm = described_members<V>;
  834. handler_tuple< converting_handler, struct_element_list<V> > handlers_;
  835. int inner_active_ = -1;
  836. std::size_t activated_ = 0;
  837. public:
  838. converting_handler( converting_handler const& ) = delete;
  839. converting_handler& operator=( converting_handler const& ) = delete;
  840. converting_handler( V* v, P* p )
  841. : value_(v), parent_(p), handlers_(struct_accessor(), v, this)
  842. {}
  843. struct is_optional_checker
  844. {
  845. template< class I >
  846. bool operator()( I ) const noexcept
  847. {
  848. using L = struct_element_list<V>;
  849. using T = mp11::mp_at<L, I>;
  850. return !is_optional_like<T>::value;
  851. }
  852. };
  853. void signal_value()
  854. {
  855. BOOST_ASSERT( inner_active_ >= 0 );
  856. bool required_member = mp11::mp_with_index< mp11::mp_size<Dm> >(
  857. inner_active_,
  858. is_optional_checker{});
  859. if( required_member )
  860. ++activated_;
  861. key_ = {};
  862. inner_active_ = -1;
  863. }
  864. bool signal_end(system::error_code&)
  865. {
  866. key_ = {};
  867. inner_active_ = -1;
  868. parent_->signal_value();
  869. return true;
  870. }
  871. #define BOOST_JSON_INVOKE_INNER(fn) \
  872. if( inner_active_ < 0 ) \
  873. { \
  874. BOOST_JSON_FAIL( ec, error::not_object ); \
  875. return false; \
  876. } \
  877. auto f = [&](auto& handler) { return handler.fn ; }; \
  878. using F = decltype(f); \
  879. using H = decltype(handlers_); \
  880. return mp11::mp_with_index< mp11::mp_size<Dm> >( \
  881. inner_active_, \
  882. tuple_handler_op_invoker<H, F>{handlers_, f} );
  883. bool on_object_begin( system::error_code& ec )
  884. {
  885. if( inner_active_ < 0 )
  886. return true;
  887. BOOST_JSON_INVOKE_INNER( on_object_begin(ec) );
  888. }
  889. bool on_object_end( system::error_code& ec )
  890. {
  891. if( inner_active_ < 0 )
  892. {
  893. using L = struct_element_list<V>;
  894. using C = mp11::mp_count_if<L, is_optional_like>;
  895. constexpr int N = mp11::mp_size<L>::value - C::value;
  896. if( activated_ < N )
  897. {
  898. BOOST_JSON_FAIL( ec, error::size_mismatch );
  899. return false;
  900. }
  901. parent_->signal_value();
  902. return true;
  903. }
  904. BOOST_JSON_INVOKE_INNER( on_object_end(ec) );
  905. }
  906. bool on_array_begin( system::error_code& ec )
  907. {
  908. BOOST_JSON_INVOKE_INNER( on_array_begin(ec) );
  909. }
  910. bool on_array_end( system::error_code& ec )
  911. {
  912. if( inner_active_ < 0 )
  913. return parent_->signal_end(ec);
  914. BOOST_JSON_INVOKE_INNER( on_array_end(ec) );
  915. }
  916. bool on_key_part( system::error_code& ec, string_view sv )
  917. {
  918. if( inner_active_ < 0 )
  919. {
  920. key_.append( sv.data(), sv.size() );
  921. return true;
  922. }
  923. BOOST_JSON_INVOKE_INNER( on_key_part(ec, sv) );
  924. }
  925. bool on_key( system::error_code& ec, string_view sv )
  926. {
  927. if( inner_active_ >= 0 )
  928. {
  929. BOOST_JSON_INVOKE_INNER( on_key(ec, sv) );
  930. }
  931. string_view key = sv;
  932. if( !key_.empty() )
  933. {
  934. key_.append( sv.data(), sv.size() );
  935. key = key_;
  936. }
  937. int i = 0;
  938. auto f = [&](char const* name)
  939. {
  940. if( key == name )
  941. inner_active_ = i;
  942. ++i;
  943. };
  944. mp11::mp_for_each<Dm>(
  945. struct_key_searcher<decltype(f)>{f} );
  946. if( inner_active_ < 0 )
  947. {
  948. BOOST_JSON_FAIL(ec, error::unknown_name);
  949. return false;
  950. }
  951. return true;
  952. }
  953. bool on_string_part( system::error_code& ec, string_view sv )
  954. {
  955. BOOST_JSON_INVOKE_INNER( on_string_part(ec, sv) );
  956. }
  957. bool on_string( system::error_code& ec, string_view sv )
  958. {
  959. BOOST_JSON_INVOKE_INNER( on_string(ec, sv) );
  960. }
  961. bool on_number_part( system::error_code& ec )
  962. {
  963. BOOST_JSON_INVOKE_INNER( on_number_part(ec) );
  964. }
  965. bool on_int64( system::error_code& ec, std::int64_t v )
  966. {
  967. BOOST_JSON_INVOKE_INNER( on_int64(ec, v) );
  968. }
  969. bool on_uint64( system::error_code& ec, std::uint64_t v )
  970. {
  971. BOOST_JSON_INVOKE_INNER( on_uint64(ec, v) );
  972. }
  973. bool on_double( system::error_code& ec, double v )
  974. {
  975. BOOST_JSON_INVOKE_INNER( on_double(ec, v) );
  976. }
  977. bool on_bool( system::error_code& ec, bool v )
  978. {
  979. BOOST_JSON_INVOKE_INNER( on_bool(ec, v) );
  980. }
  981. bool on_null( system::error_code& ec )
  982. {
  983. BOOST_JSON_INVOKE_INNER( on_null(ec) );
  984. }
  985. #undef BOOST_JSON_INVOKE_INNER
  986. #endif
  987. };
  988. // variant handler
  989. struct object_begin_handler_event
  990. { };
  991. struct object_end_handler_event
  992. { };
  993. struct array_begin_handler_event
  994. { };
  995. struct array_end_handler_event
  996. { };
  997. struct key_handler_event
  998. {
  999. std::string value;
  1000. };
  1001. struct string_handler_event
  1002. {
  1003. std::string value;
  1004. };
  1005. struct int64_handler_event
  1006. {
  1007. std::int64_t value;
  1008. };
  1009. struct uint64_handler_event
  1010. {
  1011. std::uint64_t value;
  1012. };
  1013. struct double_handler_event
  1014. {
  1015. double value;
  1016. };
  1017. struct bool_handler_event
  1018. {
  1019. bool value;
  1020. };
  1021. struct null_handler_event
  1022. { };
  1023. using parse_event = variant2::variant<
  1024. object_begin_handler_event,
  1025. object_end_handler_event,
  1026. array_begin_handler_event,
  1027. array_end_handler_event,
  1028. key_handler_event,
  1029. string_handler_event,
  1030. int64_handler_event,
  1031. uint64_handler_event,
  1032. double_handler_event,
  1033. bool_handler_event,
  1034. null_handler_event>;
  1035. template< class H >
  1036. struct event_visitor
  1037. {
  1038. H& handler;
  1039. system::error_code& ec;
  1040. bool
  1041. operator()(object_begin_handler_event&) const
  1042. {
  1043. return handler.on_object_begin(ec);
  1044. }
  1045. bool
  1046. operator()(object_end_handler_event&) const
  1047. {
  1048. return handler.on_object_end(ec);
  1049. }
  1050. bool
  1051. operator()(array_begin_handler_event&) const
  1052. {
  1053. return handler.on_array_begin(ec);
  1054. }
  1055. bool
  1056. operator()(array_end_handler_event&) const
  1057. {
  1058. return handler.on_array_end(ec);
  1059. }
  1060. bool
  1061. operator()(key_handler_event& ev) const
  1062. {
  1063. return handler.on_key(ec, ev.value);
  1064. }
  1065. bool
  1066. operator()(string_handler_event& ev) const
  1067. {
  1068. return handler.on_string(ec, ev.value);
  1069. }
  1070. bool
  1071. operator()(int64_handler_event& ev) const
  1072. {
  1073. return handler.on_int64(ec, ev.value);
  1074. }
  1075. bool
  1076. operator()(uint64_handler_event& ev) const
  1077. {
  1078. return handler.on_uint64(ec, ev.value);
  1079. }
  1080. bool
  1081. operator()(double_handler_event& ev) const
  1082. {
  1083. return handler.on_double(ec, ev.value);
  1084. }
  1085. bool
  1086. operator()(bool_handler_event& ev) const
  1087. {
  1088. return handler.on_bool(ec, ev.value);
  1089. }
  1090. bool
  1091. operator()(null_handler_event&) const
  1092. {
  1093. return handler.on_null(ec);
  1094. }
  1095. };
  1096. // L<T...> -> variant< monostate, get_handler<T, P>... >
  1097. template< class P, class L >
  1098. using inner_handler_variant = mp11::mp_push_front<
  1099. mp11::mp_transform_q<
  1100. mp11::mp_bind_back<get_handler, P>,
  1101. mp11::mp_apply<variant2::variant, L>>,
  1102. variant2::monostate>;
  1103. template< class T, class P >
  1104. class converting_handler<variant_conversion_tag, T, P>
  1105. {
  1106. private:
  1107. using variant_size = mp11::mp_size<T>;
  1108. T* value_;
  1109. P* parent_;
  1110. std::string string_;
  1111. std::vector< parse_event > events_;
  1112. inner_handler_variant<converting_handler, T> inner_;
  1113. int inner_active_ = -1;
  1114. public:
  1115. converting_handler( converting_handler const& ) = delete;
  1116. converting_handler& operator=( converting_handler const& ) = delete;
  1117. converting_handler( T* v, P* p )
  1118. : value_( v )
  1119. , parent_( p )
  1120. {}
  1121. void signal_value()
  1122. {
  1123. inner_.template emplace<0>();
  1124. inner_active_ = -1;
  1125. events_.clear();
  1126. parent_->signal_value();
  1127. }
  1128. bool signal_end(system::error_code& ec)
  1129. {
  1130. return parent_->signal_end(ec);
  1131. }
  1132. struct alternative_selector
  1133. {
  1134. converting_handler* self;
  1135. template< class I >
  1136. void
  1137. operator()( I ) const
  1138. {
  1139. using V = mp11::mp_at<T, I>;
  1140. auto& v = self->value_->template emplace<I::value>( V{} );
  1141. self->inner_.template emplace<I::value + 1>(&v, self);
  1142. }
  1143. };
  1144. void
  1145. next_alternative()
  1146. {
  1147. if( ++inner_active_ >= static_cast<int>(variant_size::value) )
  1148. return;
  1149. mp11::mp_with_index< variant_size::value >(
  1150. inner_active_, alternative_selector{this} );
  1151. }
  1152. struct event_processor
  1153. {
  1154. converting_handler* self;
  1155. system::error_code& ec;
  1156. parse_event& event;
  1157. template< class I >
  1158. bool operator()( I ) const
  1159. {
  1160. auto& handler = variant2::get<I::value + 1>(self->inner_);
  1161. using Handler = remove_cvref<decltype(handler)>;
  1162. return variant2::visit(
  1163. event_visitor<Handler>{handler, ec}, event );
  1164. }
  1165. };
  1166. bool process_events(system::error_code& ec)
  1167. {
  1168. constexpr std::size_t N = variant_size::value;
  1169. // should be pointers not iterators, otherwise MSVC crashes
  1170. auto const last = events_.data() + events_.size();
  1171. auto first = last - 1;
  1172. bool ok = false;
  1173. if( inner_active_ < 0 )
  1174. next_alternative();
  1175. do
  1176. {
  1177. if( static_cast<std::size_t>(inner_active_) >= N )
  1178. {
  1179. BOOST_JSON_FAIL( ec, error::exhausted_variants );
  1180. return false;
  1181. }
  1182. for ( ; first != last; ++first )
  1183. {
  1184. ok = mp11::mp_with_index< N >(
  1185. inner_active_, event_processor{this, ec, *first} );
  1186. if( !ok )
  1187. {
  1188. first = events_.data();
  1189. next_alternative();
  1190. ec.clear();
  1191. break;
  1192. }
  1193. }
  1194. }
  1195. while( !ok );
  1196. return true;
  1197. }
  1198. #define BOOST_JSON_INVOKE_INNER(ev, ec) \
  1199. events_.emplace_back( ev ); \
  1200. return process_events(ec);
  1201. bool on_object_begin( system::error_code& ec )
  1202. {
  1203. BOOST_JSON_INVOKE_INNER( object_begin_handler_event{}, ec );
  1204. }
  1205. bool on_object_end( system::error_code& ec )
  1206. {
  1207. BOOST_JSON_INVOKE_INNER( object_end_handler_event{}, ec );
  1208. }
  1209. bool on_array_begin( system::error_code& ec )
  1210. {
  1211. BOOST_JSON_INVOKE_INNER( array_begin_handler_event{}, ec );
  1212. }
  1213. bool on_array_end( system::error_code& ec )
  1214. {
  1215. if( !inner_active_ )
  1216. return signal_end(ec);
  1217. BOOST_JSON_INVOKE_INNER( array_end_handler_event{}, ec );
  1218. }
  1219. bool on_key_part( system::error_code&, string_view sv )
  1220. {
  1221. string_.append(sv);
  1222. return true;
  1223. }
  1224. bool on_key( system::error_code& ec, string_view sv )
  1225. {
  1226. string_.append(sv);
  1227. BOOST_JSON_INVOKE_INNER( key_handler_event{ std::move(string_) }, ec );
  1228. }
  1229. bool on_string_part( system::error_code&, string_view sv )
  1230. {
  1231. string_.append(sv);
  1232. return true;
  1233. }
  1234. bool on_string( system::error_code& ec, string_view sv )
  1235. {
  1236. string_.append(sv);
  1237. BOOST_JSON_INVOKE_INNER(
  1238. string_handler_event{ std::move(string_) }, ec );
  1239. }
  1240. bool on_number_part( system::error_code& )
  1241. {
  1242. return true;
  1243. }
  1244. bool on_int64( system::error_code& ec, std::int64_t v )
  1245. {
  1246. BOOST_JSON_INVOKE_INNER( int64_handler_event{v}, ec );
  1247. }
  1248. bool on_uint64( system::error_code& ec, std::uint64_t v )
  1249. {
  1250. BOOST_JSON_INVOKE_INNER( uint64_handler_event{v}, ec );
  1251. }
  1252. bool on_double( system::error_code& ec, double v )
  1253. {
  1254. BOOST_JSON_INVOKE_INNER( double_handler_event{v}, ec );
  1255. }
  1256. bool on_bool( system::error_code& ec, bool v )
  1257. {
  1258. BOOST_JSON_INVOKE_INNER( bool_handler_event{v}, ec );
  1259. }
  1260. bool on_null( system::error_code& ec )
  1261. {
  1262. BOOST_JSON_INVOKE_INNER( null_handler_event{}, ec );
  1263. }
  1264. #undef BOOST_JSON_INVOKE_INNER
  1265. };
  1266. // optional handler
  1267. template<class V, class P>
  1268. class converting_handler<optional_conversion_tag, V, P>
  1269. {
  1270. private:
  1271. using inner_type = value_result_type<V>;
  1272. using inner_handler_type = get_handler<inner_type, converting_handler>;
  1273. V* value_;
  1274. P* parent_;
  1275. inner_type inner_value_ = {};
  1276. inner_handler_type inner_;
  1277. bool inner_active_ = false;
  1278. public:
  1279. converting_handler( converting_handler const& ) = delete;
  1280. converting_handler& operator=( converting_handler const& ) = delete;
  1281. converting_handler( V* v, P* p )
  1282. : value_(v), parent_(p), inner_(&inner_value_, this)
  1283. {}
  1284. void signal_value()
  1285. {
  1286. *value_ = std::move(inner_value_);
  1287. inner_active_ = false;
  1288. parent_->signal_value();
  1289. }
  1290. bool signal_end(system::error_code& ec)
  1291. {
  1292. return parent_->signal_end(ec);
  1293. }
  1294. #define BOOST_JSON_INVOKE_INNER(fn) \
  1295. if( !inner_active_ ) \
  1296. inner_active_ = true; \
  1297. return inner_.fn;
  1298. bool on_object_begin( system::error_code& ec )
  1299. {
  1300. BOOST_JSON_INVOKE_INNER( on_object_begin(ec) );
  1301. }
  1302. bool on_object_end( system::error_code& ec )
  1303. {
  1304. BOOST_JSON_INVOKE_INNER( on_object_end(ec) );
  1305. }
  1306. bool on_array_begin( system::error_code& ec )
  1307. {
  1308. BOOST_JSON_INVOKE_INNER( on_array_begin(ec) );
  1309. }
  1310. bool on_array_end( system::error_code& ec )
  1311. {
  1312. if( !inner_active_ )
  1313. return signal_end(ec);
  1314. BOOST_JSON_INVOKE_INNER( on_array_end(ec) );
  1315. }
  1316. bool on_key_part( system::error_code& ec, string_view sv )
  1317. {
  1318. BOOST_JSON_INVOKE_INNER( on_key_part(ec, sv) );
  1319. }
  1320. bool on_key( system::error_code& ec, string_view sv )
  1321. {
  1322. BOOST_JSON_INVOKE_INNER( on_key(ec, sv) );
  1323. }
  1324. bool on_string_part( system::error_code& ec, string_view sv )
  1325. {
  1326. BOOST_JSON_INVOKE_INNER( on_string_part(ec, sv) );
  1327. }
  1328. bool on_string( system::error_code& ec, string_view sv )
  1329. {
  1330. BOOST_JSON_INVOKE_INNER( on_string(ec, sv) );
  1331. }
  1332. bool on_number_part( system::error_code& ec )
  1333. {
  1334. BOOST_JSON_INVOKE_INNER( on_number_part(ec) );
  1335. }
  1336. bool on_int64( system::error_code& ec, std::int64_t v )
  1337. {
  1338. BOOST_JSON_INVOKE_INNER( on_int64(ec, v) );
  1339. }
  1340. bool on_uint64( system::error_code& ec, std::uint64_t v )
  1341. {
  1342. BOOST_JSON_INVOKE_INNER( on_uint64(ec, v) );
  1343. }
  1344. bool on_double( system::error_code& ec, double v )
  1345. {
  1346. BOOST_JSON_INVOKE_INNER( on_double(ec, v) );
  1347. }
  1348. bool on_bool( system::error_code& ec, bool v )
  1349. {
  1350. BOOST_JSON_INVOKE_INNER( on_bool(ec, v) );
  1351. }
  1352. bool on_null( system::error_code& ec )
  1353. {
  1354. if( !inner_active_ )
  1355. {
  1356. *value_ = {};
  1357. this->parent_->signal_value();
  1358. return true;
  1359. }
  1360. else
  1361. {
  1362. return inner_.on_null(ec);
  1363. }
  1364. }
  1365. #undef BOOST_JSON_INVOKE_INNER
  1366. };
  1367. // path handler
  1368. template< class V, class P >
  1369. class converting_handler<path_conversion_tag, V, P>
  1370. : public scalar_handler<P, error::not_string>
  1371. {
  1372. private:
  1373. V* value_;
  1374. bool cleared_ = false;
  1375. public:
  1376. converting_handler( V* v, P* p )
  1377. : converting_handler::scalar_handler(p)
  1378. , value_(v)
  1379. {}
  1380. bool on_string_part( system::error_code&, string_view sv )
  1381. {
  1382. if( !cleared_ )
  1383. {
  1384. cleared_ = true;
  1385. value_->clear();
  1386. }
  1387. value_->concat( sv.begin(), sv.end() );
  1388. return true;
  1389. }
  1390. bool on_string( system::error_code&, string_view sv )
  1391. {
  1392. if( !cleared_ )
  1393. value_->clear();
  1394. else
  1395. cleared_ = false;
  1396. value_->concat( sv.begin(), sv.end() );
  1397. this->parent_->signal_value();
  1398. return true;
  1399. }
  1400. };
  1401. // into_handler
  1402. template< class V >
  1403. class into_handler
  1404. {
  1405. private:
  1406. using inner_handler_type = get_handler<V, into_handler>;
  1407. inner_handler_type inner_;
  1408. bool inner_active_ = true;
  1409. public:
  1410. into_handler( into_handler const& ) = delete;
  1411. into_handler& operator=( into_handler const& ) = delete;
  1412. public:
  1413. static constexpr std::size_t max_object_size = object::max_size();
  1414. static constexpr std::size_t max_array_size = array::max_size();
  1415. static constexpr std::size_t max_key_size = string::max_size();
  1416. static constexpr std::size_t max_string_size = string::max_size();
  1417. public:
  1418. explicit into_handler( V* v ): inner_( v, this )
  1419. {
  1420. }
  1421. void signal_value()
  1422. {
  1423. }
  1424. bool signal_end(system::error_code&)
  1425. {
  1426. return true;
  1427. }
  1428. bool on_document_begin( system::error_code& )
  1429. {
  1430. return true;
  1431. }
  1432. bool on_document_end( system::error_code& )
  1433. {
  1434. inner_active_ = false;
  1435. return true;
  1436. }
  1437. #define BOOST_JSON_INVOKE_INNER(f) \
  1438. if( !inner_active_ ) \
  1439. { \
  1440. BOOST_JSON_FAIL( ec, error::extra_data ); \
  1441. return false; \
  1442. } \
  1443. else \
  1444. return inner_.f
  1445. bool on_object_begin( system::error_code& ec )
  1446. {
  1447. BOOST_JSON_INVOKE_INNER( on_object_begin(ec) );
  1448. }
  1449. bool on_object_end( std::size_t, system::error_code& ec )
  1450. {
  1451. BOOST_JSON_INVOKE_INNER( on_object_end(ec) );
  1452. }
  1453. bool on_array_begin( system::error_code& ec )
  1454. {
  1455. BOOST_JSON_INVOKE_INNER( on_array_begin(ec) );
  1456. }
  1457. bool on_array_end( std::size_t, system::error_code& ec )
  1458. {
  1459. BOOST_JSON_INVOKE_INNER( on_array_end(ec) );
  1460. }
  1461. bool on_key_part( string_view sv, std::size_t, system::error_code& ec )
  1462. {
  1463. BOOST_JSON_INVOKE_INNER( on_key_part(ec, sv) );
  1464. }
  1465. bool on_key( string_view sv, std::size_t, system::error_code& ec )
  1466. {
  1467. BOOST_JSON_INVOKE_INNER( on_key(ec, sv) );
  1468. }
  1469. bool on_string_part( string_view sv, std::size_t, system::error_code& ec )
  1470. {
  1471. BOOST_JSON_INVOKE_INNER( on_string_part(ec, sv) );
  1472. }
  1473. bool on_string( string_view sv, std::size_t, system::error_code& ec )
  1474. {
  1475. BOOST_JSON_INVOKE_INNER( on_string(ec, sv) );
  1476. }
  1477. bool on_number_part( string_view, system::error_code& ec )
  1478. {
  1479. BOOST_JSON_INVOKE_INNER( on_number_part(ec) );
  1480. }
  1481. bool on_int64( std::int64_t v, string_view, system::error_code& ec )
  1482. {
  1483. BOOST_JSON_INVOKE_INNER( on_int64(ec, v) );
  1484. }
  1485. bool on_uint64( std::uint64_t v, string_view, system::error_code& ec )
  1486. {
  1487. BOOST_JSON_INVOKE_INNER( on_uint64(ec, v) );
  1488. }
  1489. bool on_double( double v, string_view, system::error_code& ec )
  1490. {
  1491. BOOST_JSON_INVOKE_INNER( on_double(ec, v) );
  1492. }
  1493. bool on_bool( bool v, system::error_code& ec )
  1494. {
  1495. BOOST_JSON_INVOKE_INNER( on_bool(ec, v) );
  1496. }
  1497. bool on_null( system::error_code& ec )
  1498. {
  1499. BOOST_JSON_INVOKE_INNER( on_null(ec) );
  1500. }
  1501. bool on_comment_part(string_view, system::error_code&)
  1502. {
  1503. return true;
  1504. }
  1505. bool on_comment(string_view, system::error_code&)
  1506. {
  1507. return true;
  1508. }
  1509. #undef BOOST_JSON_INVOKE_INNER
  1510. };
  1511. } // namespace detail
  1512. } // namespace boost
  1513. } // namespace json
  1514. #endif