impl/parse.ipp

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