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