parse_into.hpp 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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_IMPL_PARSE_INTO_HPP
  11. #define BOOST_JSON_IMPL_PARSE_INTO_HPP
  12. #include <boost/json/basic_parser_impl.hpp>
  13. #include <boost/json/error.hpp>
  14. #include <istream>
  15. namespace boost {
  16. namespace json {
  17. template<class V>
  18. void
  19. parse_into(
  20. V& v,
  21. string_view sv,
  22. system::error_code& ec,
  23. parse_options const& opt )
  24. {
  25. parser_for<V> p( opt, &v );
  26. std::size_t n = p.write_some( false, sv.data(), sv.size(), ec );
  27. if( !ec && n < sv.size() )
  28. {
  29. BOOST_JSON_FAIL( ec, error::extra_data );
  30. }
  31. }
  32. template<class V>
  33. void
  34. parse_into(
  35. V& v,
  36. string_view sv,
  37. std::error_code& ec,
  38. parse_options const& opt )
  39. {
  40. system::error_code jec;
  41. parse_into(v, sv, jec, opt);
  42. ec = jec;
  43. }
  44. template<class V>
  45. void
  46. parse_into(
  47. V& v,
  48. string_view sv,
  49. parse_options const& opt )
  50. {
  51. system::error_code ec;
  52. parse_into(v, sv, ec, opt);
  53. if( ec.failed() )
  54. detail::throw_system_error( ec );
  55. }
  56. template<class V>
  57. void
  58. parse_into(
  59. V& v,
  60. std::istream& is,
  61. system::error_code& ec,
  62. parse_options const& opt )
  63. {
  64. parser_for<V> p( opt, &v );
  65. char read_buffer[BOOST_JSON_STACK_BUFFER_SIZE];
  66. do
  67. {
  68. if( is.eof() )
  69. {
  70. p.write_some(false, nullptr, 0, ec);
  71. break;
  72. }
  73. if( !is )
  74. {
  75. BOOST_JSON_FAIL( ec, error::input_error );
  76. break;
  77. }
  78. is.read(read_buffer, sizeof(read_buffer));
  79. std::size_t const consumed = static_cast<std::size_t>( is.gcount() );
  80. std::size_t const n = p.write_some( true, read_buffer, consumed, ec );
  81. if( !ec.failed() && n < consumed )
  82. {
  83. BOOST_JSON_FAIL( ec, error::extra_data );
  84. }
  85. }
  86. while( !ec.failed() );
  87. }
  88. template<class V>
  89. void
  90. parse_into(
  91. V& v,
  92. std::istream& is,
  93. std::error_code& ec,
  94. parse_options const& opt )
  95. {
  96. system::error_code jec;
  97. parse_into(v, is, jec, opt);
  98. ec = jec;
  99. }
  100. template<class V>
  101. void
  102. parse_into(
  103. V& v,
  104. std::istream& is,
  105. parse_options const& opt )
  106. {
  107. system::error_code ec;
  108. parse_into(v, is, ec, opt);
  109. if( ec.failed() )
  110. detail::throw_system_error( ec );
  111. }
  112. } // namespace boost
  113. } // namespace json
  114. #endif