/src/boost/boost/json/impl/parse.ipp
Line | Count | Source (jump to first uncovered line) |
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 | | parse( |
25 | | string_view s, |
26 | | system::error_code& ec, |
27 | | storage_ptr sp, |
28 | | const parse_options& opt) |
29 | 0 | { |
30 | 0 | unsigned char temp[ |
31 | 0 | BOOST_JSON_STACK_BUFFER_SIZE]; |
32 | 0 | parser p(storage_ptr(), opt, temp); |
33 | 0 | p.reset(std::move(sp)); |
34 | 0 | p.write(s, ec); |
35 | 0 | if(ec) |
36 | 0 | return nullptr; |
37 | 0 | return p.release(); |
38 | 0 | } |
39 | | |
40 | | value |
41 | | parse( |
42 | | string_view s, |
43 | | std::error_code& ec, |
44 | | storage_ptr sp, |
45 | | parse_options const& opt) |
46 | 0 | { |
47 | 0 | system::error_code jec; |
48 | 0 | value result = parse(s, jec, std::move(sp), opt); |
49 | 0 | ec = jec; |
50 | 0 | return result; |
51 | 0 | } |
52 | | |
53 | | value |
54 | | parse( |
55 | | string_view s, |
56 | | storage_ptr sp, |
57 | | const parse_options& opt) |
58 | 0 | { |
59 | 0 | system::error_code ec; |
60 | 0 | auto jv = parse( |
61 | 0 | s, ec, std::move(sp), opt); |
62 | 0 | if(ec) |
63 | 0 | detail::throw_system_error( ec ); |
64 | 0 | return jv; |
65 | 0 | } |
66 | | |
67 | | value |
68 | | parse( |
69 | | std::istream& is, |
70 | | system::error_code& ec, |
71 | | storage_ptr sp, |
72 | | parse_options const& opt) |
73 | 0 | { |
74 | 0 | unsigned char parser_buffer[BOOST_JSON_STACK_BUFFER_SIZE / 2]; |
75 | 0 | stream_parser p(storage_ptr(), opt, parser_buffer); |
76 | 0 | p.reset(std::move(sp)); |
77 | |
|
78 | 0 | char read_buffer[BOOST_JSON_STACK_BUFFER_SIZE / 2]; |
79 | 0 | do |
80 | 0 | { |
81 | 0 | if( is.eof() ) |
82 | 0 | { |
83 | 0 | p.finish(ec); |
84 | 0 | break; |
85 | 0 | } |
86 | | |
87 | 0 | if( !is ) |
88 | 0 | { |
89 | 0 | BOOST_JSON_FAIL( ec, error::input_error ); |
90 | 0 | break; |
91 | 0 | } |
92 | | |
93 | 0 | is.read(read_buffer, sizeof(read_buffer)); |
94 | 0 | auto const consumed = is.gcount(); |
95 | |
|
96 | 0 | p.write( read_buffer, static_cast<std::size_t>(consumed), ec ); |
97 | 0 | } |
98 | 0 | while( !ec.failed() ); |
99 | | |
100 | 0 | if( ec.failed() ) |
101 | 0 | return nullptr; |
102 | | |
103 | 0 | return p.release(); |
104 | 0 | } |
105 | | |
106 | | value |
107 | | parse( |
108 | | std::istream& is, |
109 | | std::error_code& ec, |
110 | | storage_ptr sp, |
111 | | parse_options const& opt) |
112 | 0 | { |
113 | 0 | system::error_code jec; |
114 | 0 | value result = parse(is, jec, std::move(sp), opt); |
115 | 0 | ec = jec; |
116 | 0 | return result; |
117 | 0 | } |
118 | | |
119 | | value |
120 | | parse( |
121 | | std::istream& is, |
122 | | storage_ptr sp, |
123 | | parse_options const& opt) |
124 | 0 | { |
125 | 0 | system::error_code ec; |
126 | 0 | auto jv = parse( |
127 | 0 | is, ec, std::move(sp), opt); |
128 | 0 | if(ec) |
129 | 0 | detail::throw_system_error( ec ); |
130 | 0 | return jv; |
131 | 0 | } |
132 | | |
133 | | } // namespace json |
134 | | } // namespace boost |
135 | | |
136 | | #endif |