impl/parse.ipp

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