value.ipp 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987
  1. //
  2. // Copyright (c) 2019 Vinnie Falco (vinnie.falco@gmail.com)
  3. //
  4. // Distributed under the Boost Software License, Version 1.0. (See accompanying
  5. // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  6. //
  7. // Official repository: https://github.com/boostorg/json
  8. //
  9. #ifndef BOOST_JSON_IMPL_VALUE_IPP
  10. #define BOOST_JSON_IMPL_VALUE_IPP
  11. #include <boost/container_hash/hash.hpp>
  12. #include <boost/json/value.hpp>
  13. #include <boost/json/parser.hpp>
  14. #include <cstring>
  15. #include <istream>
  16. #include <limits>
  17. #include <new>
  18. #include <utility>
  19. namespace boost {
  20. namespace json {
  21. namespace
  22. {
  23. int parse_depth_xalloc = std::ios::xalloc();
  24. int parse_flags_xalloc = std::ios::xalloc();
  25. struct value_hasher
  26. {
  27. std::size_t& seed;
  28. template< class T >
  29. void operator()( T&& t ) const noexcept
  30. {
  31. boost::hash_combine( seed, t );
  32. }
  33. };
  34. enum class stream_parse_flags
  35. {
  36. allow_comments = 1 << 0,
  37. allow_trailing_commas = 1 << 1,
  38. allow_invalid_utf8 = 1 << 2,
  39. };
  40. long
  41. to_bitmask( parse_options const& opts )
  42. {
  43. using E = stream_parse_flags;
  44. return
  45. (opts.allow_comments ?
  46. static_cast<long>(E::allow_comments) : 0) |
  47. (opts.allow_trailing_commas ?
  48. static_cast<long>(E::allow_trailing_commas) : 0) |
  49. (opts.allow_invalid_utf8 ?
  50. static_cast<long>(E::allow_invalid_utf8) : 0);
  51. }
  52. parse_options
  53. get_parse_options( std::istream& is )
  54. {
  55. long const flags = is.iword(parse_flags_xalloc);
  56. using E = stream_parse_flags;
  57. parse_options opts;
  58. opts.allow_comments =
  59. flags & static_cast<long>(E::allow_comments) ? true : false;
  60. opts.allow_trailing_commas =
  61. flags & static_cast<long>(E::allow_trailing_commas) ? true : false;
  62. opts.allow_invalid_utf8 =
  63. flags & static_cast<long>(E::allow_invalid_utf8) ? true : false;
  64. return opts;
  65. }
  66. } // namespace
  67. value::
  68. ~value() noexcept
  69. {
  70. switch(kind())
  71. {
  72. case json::kind::null:
  73. case json::kind::bool_:
  74. case json::kind::int64:
  75. case json::kind::uint64:
  76. case json::kind::double_:
  77. sca_.~scalar();
  78. break;
  79. case json::kind::string:
  80. str_.~string();
  81. break;
  82. case json::kind::array:
  83. arr_.~array();
  84. break;
  85. case json::kind::object:
  86. obj_.~object();
  87. break;
  88. }
  89. }
  90. value::
  91. value(
  92. value const& other,
  93. storage_ptr sp)
  94. {
  95. switch(other.kind())
  96. {
  97. case json::kind::null:
  98. ::new(&sca_) scalar(
  99. std::move(sp));
  100. break;
  101. case json::kind::bool_:
  102. ::new(&sca_) scalar(
  103. other.sca_.b,
  104. std::move(sp));
  105. break;
  106. case json::kind::int64:
  107. ::new(&sca_) scalar(
  108. other.sca_.i,
  109. std::move(sp));
  110. break;
  111. case json::kind::uint64:
  112. ::new(&sca_) scalar(
  113. other.sca_.u,
  114. std::move(sp));
  115. break;
  116. case json::kind::double_:
  117. ::new(&sca_) scalar(
  118. other.sca_.d,
  119. std::move(sp));
  120. break;
  121. case json::kind::string:
  122. ::new(&str_) string(
  123. other.str_,
  124. std::move(sp));
  125. break;
  126. case json::kind::array:
  127. ::new(&arr_) array(
  128. other.arr_,
  129. std::move(sp));
  130. break;
  131. case json::kind::object:
  132. ::new(&obj_) object(
  133. other.obj_,
  134. std::move(sp));
  135. break;
  136. }
  137. }
  138. value::
  139. value(value&& other) noexcept
  140. {
  141. relocate(this, other);
  142. ::new(&other.sca_) scalar(sp_);
  143. }
  144. value::
  145. value(
  146. value&& other,
  147. storage_ptr sp)
  148. {
  149. switch(other.kind())
  150. {
  151. case json::kind::null:
  152. ::new(&sca_) scalar(
  153. std::move(sp));
  154. break;
  155. case json::kind::bool_:
  156. ::new(&sca_) scalar(
  157. other.sca_.b, std::move(sp));
  158. break;
  159. case json::kind::int64:
  160. ::new(&sca_) scalar(
  161. other.sca_.i, std::move(sp));
  162. break;
  163. case json::kind::uint64:
  164. ::new(&sca_) scalar(
  165. other.sca_.u, std::move(sp));
  166. break;
  167. case json::kind::double_:
  168. ::new(&sca_) scalar(
  169. other.sca_.d, std::move(sp));
  170. break;
  171. case json::kind::string:
  172. ::new(&str_) string(
  173. std::move(other.str_),
  174. std::move(sp));
  175. break;
  176. case json::kind::array:
  177. ::new(&arr_) array(
  178. std::move(other.arr_),
  179. std::move(sp));
  180. break;
  181. case json::kind::object:
  182. ::new(&obj_) object(
  183. std::move(other.obj_),
  184. std::move(sp));
  185. break;
  186. }
  187. }
  188. //----------------------------------------------------------
  189. //
  190. // Conversion
  191. //
  192. //----------------------------------------------------------
  193. value::
  194. value(
  195. std::initializer_list<value_ref> init,
  196. storage_ptr sp)
  197. {
  198. if(value_ref::maybe_object(init))
  199. {
  200. ::new(&obj_) object(
  201. value_ref::make_object(
  202. init, std::move(sp)));
  203. }
  204. else
  205. {
  206. #ifndef BOOST_JSON_LEGACY_INIT_LIST_BEHAVIOR
  207. if( init.size() == 1 )
  208. {
  209. ::new(&sca_) scalar();
  210. value temp = init.begin()->make_value( std::move(sp) );
  211. swap(temp);
  212. }
  213. else
  214. #endif
  215. {
  216. ::new(&arr_) array(
  217. value_ref::make_array(
  218. init, std::move(sp)));
  219. }
  220. }
  221. }
  222. //----------------------------------------------------------
  223. //
  224. // Assignment
  225. //
  226. //----------------------------------------------------------
  227. value&
  228. value::
  229. operator=(value const& other)
  230. {
  231. value(other,
  232. storage()).swap(*this);
  233. return *this;
  234. }
  235. value&
  236. value::
  237. operator=(value&& other)
  238. {
  239. value(std::move(other),
  240. storage()).swap(*this);
  241. return *this;
  242. }
  243. value&
  244. value::
  245. operator=(
  246. std::initializer_list<value_ref> init)
  247. {
  248. value(init,
  249. storage()).swap(*this);
  250. return *this;
  251. }
  252. value&
  253. value::
  254. operator=(string_view s)
  255. {
  256. value(s, storage()).swap(*this);
  257. return *this;
  258. }
  259. value&
  260. value::
  261. operator=(char const* s)
  262. {
  263. value(s, storage()).swap(*this);
  264. return *this;
  265. }
  266. value&
  267. value::
  268. operator=(string const& str)
  269. {
  270. value(str, storage()).swap(*this);
  271. return *this;
  272. }
  273. value&
  274. value::
  275. operator=(string&& str)
  276. {
  277. value(std::move(str),
  278. storage()).swap(*this);
  279. return *this;
  280. }
  281. value&
  282. value::
  283. operator=(array const& arr)
  284. {
  285. value(arr, storage()).swap(*this);
  286. return *this;
  287. }
  288. value&
  289. value::
  290. operator=(array&& arr)
  291. {
  292. value(std::move(arr),
  293. storage()).swap(*this);
  294. return *this;
  295. }
  296. value&
  297. value::
  298. operator=(object const& obj)
  299. {
  300. value(obj, storage()).swap(*this);
  301. return *this;
  302. }
  303. value&
  304. value::
  305. operator=(object&& obj)
  306. {
  307. value(std::move(obj),
  308. storage()).swap(*this);
  309. return *this;
  310. }
  311. //----------------------------------------------------------
  312. //
  313. // Accessors
  314. //
  315. //----------------------------------------------------------
  316. system::result<array&>
  317. value::try_as_array() noexcept
  318. {
  319. if( is_array() )
  320. return arr_;
  321. system::error_code ec;
  322. BOOST_JSON_FAIL(ec, error::not_array);
  323. return ec;
  324. }
  325. system::result<array const&>
  326. value::try_as_array() const noexcept
  327. {
  328. if( is_array() )
  329. return arr_;
  330. system::error_code ec;
  331. BOOST_JSON_FAIL(ec, error::not_array);
  332. return ec;
  333. }
  334. system::result<object&>
  335. value::try_as_object() noexcept
  336. {
  337. if( is_object() )
  338. return obj_;
  339. system::error_code ec;
  340. BOOST_JSON_FAIL(ec, error::not_object);
  341. return ec;
  342. }
  343. system::result<object const&>
  344. value::try_as_object() const noexcept
  345. {
  346. if( is_object() )
  347. return obj_;
  348. system::error_code ec;
  349. BOOST_JSON_FAIL(ec, error::not_object);
  350. return ec;
  351. }
  352. system::result<string&>
  353. value::try_as_string() noexcept
  354. {
  355. if( is_string() )
  356. return str_;
  357. system::error_code ec;
  358. BOOST_JSON_FAIL(ec, error::not_string);
  359. return ec;
  360. }
  361. system::result<string const&>
  362. value::try_as_string() const noexcept
  363. {
  364. if( is_string() )
  365. return str_;
  366. system::error_code ec;
  367. BOOST_JSON_FAIL(ec, error::not_string);
  368. return ec;
  369. }
  370. system::result<std::int64_t&>
  371. value::try_as_int64() noexcept
  372. {
  373. if( is_int64() )
  374. return sca_.i;
  375. system::error_code ec;
  376. BOOST_JSON_FAIL(ec, error::not_int64);
  377. return ec;
  378. }
  379. system::result<std::int64_t>
  380. value::try_as_int64() const noexcept
  381. {
  382. if( is_int64() )
  383. return sca_.i;
  384. system::error_code ec;
  385. BOOST_JSON_FAIL(ec, error::not_int64);
  386. return ec;
  387. }
  388. system::result<std::uint64_t&>
  389. value::try_as_uint64() noexcept
  390. {
  391. if( is_uint64() )
  392. return sca_.u;
  393. system::error_code ec;
  394. BOOST_JSON_FAIL(ec, error::not_uint64);
  395. return ec;
  396. }
  397. system::result<std::uint64_t>
  398. value::try_as_uint64() const noexcept
  399. {
  400. if( is_uint64() )
  401. return sca_.u;
  402. system::error_code ec;
  403. BOOST_JSON_FAIL(ec, error::not_uint64);
  404. return ec;
  405. }
  406. system::result<double&>
  407. value::try_as_double() noexcept
  408. {
  409. if( is_double() )
  410. return sca_.d;
  411. system::error_code ec;
  412. BOOST_JSON_FAIL(ec, error::not_double);
  413. return ec;
  414. }
  415. system::result<double>
  416. value::try_as_double() const noexcept
  417. {
  418. if( is_double() )
  419. return sca_.d;
  420. system::error_code ec;
  421. BOOST_JSON_FAIL(ec, error::not_double);
  422. return ec;
  423. }
  424. system::result<bool&>
  425. value::try_as_bool() noexcept
  426. {
  427. if( is_bool() )
  428. return sca_.b;
  429. system::error_code ec;
  430. BOOST_JSON_FAIL(ec, error::not_bool);
  431. return ec;
  432. }
  433. system::result<bool>
  434. value::try_as_bool() const noexcept
  435. {
  436. if( is_bool() )
  437. return sca_.b;
  438. system::error_code ec;
  439. BOOST_JSON_FAIL(ec, error::not_bool);
  440. return ec;
  441. }
  442. system::result<std::nullptr_t>
  443. value::try_as_null() const noexcept
  444. {
  445. if( is_null() )
  446. return nullptr;
  447. system::error_code ec;
  448. BOOST_JSON_FAIL(ec, error::not_null);
  449. return ec;
  450. }
  451. boost::system::result<value&>
  452. value::try_at(string_view key) noexcept
  453. {
  454. auto r = try_as_object();
  455. if( !r )
  456. return r.error();
  457. return r->try_at(key);
  458. }
  459. boost::system::result<value const&>
  460. value::try_at(string_view key) const noexcept
  461. {
  462. auto r = try_as_object();
  463. if( !r )
  464. return r.error();
  465. return r->try_at(key);
  466. }
  467. boost::system::result<value&>
  468. value::try_at(std::size_t pos) noexcept
  469. {
  470. auto r = try_as_array();
  471. if( !r )
  472. return r.error();
  473. return r->try_at(pos);
  474. }
  475. boost::system::result<value const&>
  476. value::try_at(std::size_t pos) const noexcept
  477. {
  478. auto r = try_as_array();
  479. if( !r )
  480. return r.error();
  481. return r->try_at(pos);
  482. }
  483. object const&
  484. value::as_object(source_location const& loc) const&
  485. {
  486. return try_as_object().value(loc);
  487. }
  488. array const&
  489. value::as_array(source_location const& loc) const&
  490. {
  491. return try_as_array().value(loc);
  492. }
  493. string const&
  494. value::as_string(source_location const& loc) const&
  495. {
  496. return try_as_string().value(loc);
  497. }
  498. std::int64_t&
  499. value::as_int64(source_location const& loc)
  500. {
  501. return try_as_int64().value(loc);
  502. }
  503. std::int64_t
  504. value::as_int64(source_location const& loc) const
  505. {
  506. return try_as_int64().value(loc);
  507. }
  508. std::uint64_t&
  509. value::as_uint64(source_location const& loc)
  510. {
  511. return try_as_uint64().value(loc);
  512. }
  513. std::uint64_t
  514. value::as_uint64(source_location const& loc) const
  515. {
  516. return try_as_uint64().value(loc);
  517. }
  518. double&
  519. value::as_double(source_location const& loc)
  520. {
  521. return try_as_double().value(loc);
  522. }
  523. double
  524. value::as_double(source_location const& loc) const
  525. {
  526. return try_as_double().value(loc);
  527. }
  528. bool&
  529. value::as_bool(source_location const& loc)
  530. {
  531. return try_as_bool().value(loc);
  532. }
  533. bool
  534. value::as_bool(source_location const& loc) const
  535. {
  536. return try_as_bool().value(loc);
  537. }
  538. //----------------------------------------------------------
  539. //
  540. // Modifiers
  541. //
  542. //----------------------------------------------------------
  543. string&
  544. value::
  545. emplace_string() noexcept
  546. {
  547. return *::new(&str_) string(destroy());
  548. }
  549. array&
  550. value::
  551. emplace_array() noexcept
  552. {
  553. return *::new(&arr_) array(destroy());
  554. }
  555. object&
  556. value::
  557. emplace_object() noexcept
  558. {
  559. return *::new(&obj_) object(destroy());
  560. }
  561. void
  562. value::
  563. swap(value& other)
  564. {
  565. if(*storage() == *other.storage())
  566. {
  567. // fast path
  568. union U
  569. {
  570. value tmp;
  571. U(){}
  572. ~U(){}
  573. };
  574. U u;
  575. relocate(&u.tmp, *this);
  576. relocate(this, other);
  577. relocate(&other, u.tmp);
  578. return;
  579. }
  580. // copy
  581. value temp1(
  582. std::move(*this),
  583. other.storage());
  584. value temp2(
  585. std::move(other),
  586. this->storage());
  587. other.~value();
  588. ::new(&other) value(pilfer(temp1));
  589. this->~value();
  590. ::new(this) value(pilfer(temp2));
  591. }
  592. std::istream&
  593. operator>>(
  594. std::istream& is,
  595. value& jv)
  596. {
  597. using Traits = std::istream::traits_type;
  598. // sentry prepares the stream for reading and finalizes it in destructor
  599. std::istream::sentry sentry(is);
  600. if( !sentry )
  601. return is;
  602. parse_options opts = get_parse_options( is );
  603. if( auto depth = static_cast<std::size_t>( is.iword(parse_depth_xalloc) ) )
  604. opts.max_depth = depth;
  605. unsigned char parser_buf[BOOST_JSON_STACK_BUFFER_SIZE / 2];
  606. stream_parser p( {}, opts, parser_buf );
  607. p.reset( jv.storage() );
  608. char read_buf[BOOST_JSON_STACK_BUFFER_SIZE / 2];
  609. std::streambuf& buf = *is.rdbuf();
  610. std::ios::iostate err = std::ios::goodbit;
  611. #ifndef BOOST_NO_EXCEPTIONS
  612. try
  613. #endif
  614. {
  615. while( true )
  616. {
  617. system::error_code ec;
  618. // we peek the buffer; this either makes sure that there's no
  619. // more input, or makes sure there's something in the internal
  620. // buffer (so in_avail will return a positive number)
  621. std::istream::int_type c = is.rdbuf()->sgetc();
  622. // if we indeed reached EOF, we check if we parsed a full JSON
  623. // document; if not, we error out
  624. if( Traits::eq_int_type(c, Traits::eof()) )
  625. {
  626. err |= std::ios::eofbit;
  627. p.finish(ec);
  628. if( ec.failed() )
  629. break;
  630. }
  631. // regardless of reaching EOF, we might have parsed a full JSON
  632. // document; if so, we successfully finish
  633. if( p.done() )
  634. {
  635. jv = p.release();
  636. return is;
  637. }
  638. // at this point we definitely have more input, specifically in
  639. // buf's internal buffer; we also definitely haven't parsed a whole
  640. // document
  641. std::streamsize available = buf.in_avail();
  642. // if this assert fails, the streambuf is buggy
  643. BOOST_ASSERT( available > 0 );
  644. available = ( std::min )(
  645. static_cast<std::size_t>(available), sizeof(read_buf) );
  646. // we read from the internal buffer of buf into our buffer
  647. available = buf.sgetn( read_buf, available );
  648. std::size_t consumed = p.write_some(
  649. read_buf, static_cast<std::size_t>(available), ec );
  650. // if the parser hasn't consumed the entire input we've took from
  651. // buf, we put the remaining data back; this should succeed,
  652. // because we only read data from buf's internal buffer
  653. while( consumed++ < static_cast<std::size_t>(available) )
  654. {
  655. std::istream::int_type const status = buf.sungetc();
  656. BOOST_ASSERT( status != Traits::eof() );
  657. (void)status;
  658. }
  659. if( ec.failed() )
  660. break;
  661. }
  662. }
  663. #ifndef BOOST_NO_EXCEPTIONS
  664. catch(...)
  665. {
  666. try
  667. {
  668. is.setstate(std::ios::badbit);
  669. }
  670. // we ignore the exception, because we need to throw the original
  671. // exception instead
  672. catch( std::ios::failure const& ) { }
  673. if( is.exceptions() & std::ios::badbit )
  674. throw;
  675. }
  676. #endif
  677. is.setstate(err | std::ios::failbit);
  678. return is;
  679. }
  680. std::istream&
  681. operator>>(
  682. std::istream& is,
  683. parse_options const& opts)
  684. {
  685. is.iword(parse_flags_xalloc) = to_bitmask(opts);
  686. is.iword(parse_depth_xalloc) = static_cast<long>(opts.max_depth);
  687. return is;
  688. }
  689. //----------------------------------------------------------
  690. //
  691. // private
  692. //
  693. //----------------------------------------------------------
  694. storage_ptr
  695. value::
  696. destroy() noexcept
  697. {
  698. switch(kind())
  699. {
  700. case json::kind::null:
  701. case json::kind::bool_:
  702. case json::kind::int64:
  703. case json::kind::uint64:
  704. case json::kind::double_:
  705. break;
  706. case json::kind::string:
  707. {
  708. auto sp = str_.storage();
  709. str_.~string();
  710. return sp;
  711. }
  712. case json::kind::array:
  713. {
  714. auto sp = arr_.storage();
  715. arr_.~array();
  716. return sp;
  717. }
  718. case json::kind::object:
  719. {
  720. auto sp = obj_.storage();
  721. obj_.~object();
  722. return sp;
  723. }
  724. }
  725. return std::move(sp_);
  726. }
  727. bool
  728. value::
  729. equal(value const& other) const noexcept
  730. {
  731. switch(kind())
  732. {
  733. default: // unreachable()?
  734. case json::kind::null:
  735. return other.kind() == json::kind::null;
  736. case json::kind::bool_:
  737. return
  738. other.kind() == json::kind::bool_ &&
  739. get_bool() == other.get_bool();
  740. case json::kind::int64:
  741. switch(other.kind())
  742. {
  743. case json::kind::int64:
  744. return get_int64() == other.get_int64();
  745. case json::kind::uint64:
  746. if(get_int64() < 0)
  747. return false;
  748. return static_cast<std::uint64_t>(
  749. get_int64()) == other.get_uint64();
  750. default:
  751. return false;
  752. }
  753. case json::kind::uint64:
  754. switch(other.kind())
  755. {
  756. case json::kind::uint64:
  757. return get_uint64() == other.get_uint64();
  758. case json::kind::int64:
  759. if(other.get_int64() < 0)
  760. return false;
  761. return static_cast<std::uint64_t>(
  762. other.get_int64()) == get_uint64();
  763. default:
  764. return false;
  765. }
  766. case json::kind::double_:
  767. return
  768. other.kind() == json::kind::double_ &&
  769. get_double() == other.get_double();
  770. case json::kind::string:
  771. return
  772. other.kind() == json::kind::string &&
  773. get_string() == other.get_string();
  774. case json::kind::array:
  775. return
  776. other.kind() == json::kind::array &&
  777. get_array() == other.get_array();
  778. case json::kind::object:
  779. return
  780. other.kind() == json::kind::object &&
  781. get_object() == other.get_object();
  782. }
  783. }
  784. //----------------------------------------------------------
  785. //
  786. // key_value_pair
  787. //
  788. //----------------------------------------------------------
  789. // empty keys point here
  790. BOOST_JSON_REQUIRE_CONST_INIT
  791. char const
  792. key_value_pair::empty_[1] = { 0 };
  793. key_value_pair::
  794. key_value_pair(
  795. pilfered<json::value> key,
  796. pilfered<json::value> value) noexcept
  797. : value_(value)
  798. {
  799. std::size_t len;
  800. key_ = access::release_key(key.get(), len);
  801. len_ = static_cast<std::uint32_t>(len);
  802. }
  803. key_value_pair::
  804. key_value_pair(
  805. key_value_pair const& other,
  806. storage_ptr sp)
  807. : value_(other.value_, std::move(sp))
  808. {
  809. auto p = reinterpret_cast<
  810. char*>(value_.storage()->
  811. allocate(other.len_ + 1,
  812. alignof(char)));
  813. std::memcpy(
  814. p, other.key_, other.len_);
  815. len_ = other.len_;
  816. p[len_] = 0;
  817. key_ = p;
  818. }
  819. //----------------------------------------------------------
  820. namespace detail
  821. {
  822. std::size_t
  823. hash_value_impl( value const& jv ) noexcept
  824. {
  825. std::size_t seed = 0;
  826. kind const k = jv.kind();
  827. boost::hash_combine( seed, k != kind::int64 ? k : kind::uint64 );
  828. visit( value_hasher{seed}, jv );
  829. return seed;
  830. }
  831. } // namespace detail
  832. } // namespace json
  833. } // namespace boost
  834. //----------------------------------------------------------
  835. //
  836. // std::hash specialization
  837. //
  838. //----------------------------------------------------------
  839. std::size_t
  840. std::hash<::boost::json::value>::operator()(
  841. ::boost::json::value const& jv) const noexcept
  842. {
  843. return ::boost::hash< ::boost::json::value >()( jv );
  844. }
  845. //----------------------------------------------------------
  846. #endif