parse.ipp 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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_IPP
  11. #define BOOST_JSON_IMPL_PARSE_IPP
  12. #include <boost/json/parse.hpp>
  13. #include <boost/json/parser.hpp>
  14. #include <boost/json/detail/except.hpp>
  15. #include <istream>
  16. namespace boost {
  17. namespace json {
  18. value
  19. parse(
  20. string_view s,
  21. system::error_code& ec,
  22. storage_ptr sp,
  23. const parse_options& opt)
  24. {
  25. unsigned char temp[
  26. BOOST_JSON_STACK_BUFFER_SIZE];
  27. parser p(storage_ptr(), opt, temp);
  28. p.reset(std::move(sp));
  29. p.write(s, ec);
  30. if(ec)
  31. return nullptr;
  32. return p.release();
  33. }
  34. value
  35. parse(
  36. string_view s,
  37. std::error_code& ec,
  38. storage_ptr sp,
  39. parse_options const& opt)
  40. {
  41. system::error_code jec;
  42. value result = parse(s, jec, std::move(sp), opt);
  43. ec = jec;
  44. return result;
  45. }
  46. value
  47. parse(
  48. string_view s,
  49. storage_ptr sp,
  50. const parse_options& opt)
  51. {
  52. system::error_code ec;
  53. auto jv = parse(
  54. s, ec, std::move(sp), opt);
  55. if(ec)
  56. detail::throw_system_error( ec );
  57. return jv;
  58. }
  59. value
  60. parse(
  61. std::istream& is,
  62. system::error_code& ec,
  63. storage_ptr sp,
  64. parse_options const& opt)
  65. {
  66. unsigned char parser_buffer[BOOST_JSON_STACK_BUFFER_SIZE / 2];
  67. stream_parser p(storage_ptr(), opt, parser_buffer);
  68. p.reset(std::move(sp));
  69. char read_buffer[BOOST_JSON_STACK_BUFFER_SIZE / 2];
  70. do
  71. {
  72. if( is.eof() )
  73. {
  74. p.finish(ec);
  75. break;
  76. }
  77. if( !is )
  78. {
  79. BOOST_JSON_FAIL( ec, error::input_error );
  80. break;
  81. }
  82. is.read(read_buffer, sizeof(read_buffer));
  83. auto const consumed = is.gcount();
  84. p.write( read_buffer, static_cast<std::size_t>(consumed), ec );
  85. }
  86. while( !ec.failed() );
  87. if( ec.failed() )
  88. return nullptr;
  89. return p.release();
  90. }
  91. value
  92. parse(
  93. std::istream& is,
  94. std::error_code& ec,
  95. storage_ptr sp,
  96. parse_options const& opt)
  97. {
  98. system::error_code jec;
  99. value result = parse(is, jec, std::move(sp), opt);
  100. ec = jec;
  101. return result;
  102. }
  103. value
  104. parse(
  105. std::istream& is,
  106. storage_ptr sp,
  107. parse_options const& opt)
  108. {
  109. system::error_code ec;
  110. auto jv = parse(
  111. is, ec, std::move(sp), opt);
  112. if(ec)
  113. detail::throw_system_error( ec );
  114. return jv;
  115. }
  116. } // namespace json
  117. } // namespace boost
  118. #endif