1  
//
1  
//
2  
// Copyright (c) 2019 Vinnie Falco (vinnie.falco@gmail.com)
2  
// Copyright (c) 2019 Vinnie Falco (vinnie.falco@gmail.com)
3  
// Copyright (c) 2020 Krystian Stasiowski (sdkrystian@gmail.com)
3  
// Copyright (c) 2020 Krystian Stasiowski (sdkrystian@gmail.com)
4  
//
4  
//
5  
// Distributed under the Boost Software License, Version 1.0. (See accompanying
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)
6  
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
7  
//
7  
//
8  
// Official repository: https://github.com/boostorg/json
8  
// Official repository: https://github.com/boostorg/json
9  
//
9  
//
10  

10  

11  
#ifndef BOOST_JSON_IMPL_PARSE_IPP
11  
#ifndef BOOST_JSON_IMPL_PARSE_IPP
12  
#define BOOST_JSON_IMPL_PARSE_IPP
12  
#define BOOST_JSON_IMPL_PARSE_IPP
13  

13  

14  
#include <boost/json/parse.hpp>
14  
#include <boost/json/parse.hpp>
15  
#include <boost/json/parser.hpp>
15  
#include <boost/json/parser.hpp>
16  
#include <boost/json/detail/except.hpp>
16  
#include <boost/json/detail/except.hpp>
17  

17  

18  
#include <istream>
18  
#include <istream>
19  

19  

20  
namespace boost {
20  
namespace boost {
21  
namespace json {
21  
namespace json {
22  

22  

23  
value
23  
value
24  
parse(
24  
parse(
25  
    string_view s,
25  
    string_view s,
26  
    system::error_code& ec,
26  
    system::error_code& ec,
27  
    storage_ptr sp,
27  
    storage_ptr sp,
28  
    const parse_options& opt)
28  
    const parse_options& opt)
29  
{
29  
{
30  
    unsigned char temp[
30  
    unsigned char temp[
31  
        BOOST_JSON_STACK_BUFFER_SIZE];
31  
        BOOST_JSON_STACK_BUFFER_SIZE];
32  
    parser p(storage_ptr(), opt, temp);
32  
    parser p(storage_ptr(), opt, temp);
33  
    p.reset(std::move(sp));
33  
    p.reset(std::move(sp));
34  
    p.write(s, ec);
34  
    p.write(s, ec);
35  
    if(ec)
35  
    if(ec)
36  
        return nullptr;
36  
        return nullptr;
37  
    return p.release();
37  
    return p.release();
38  
}
38  
}
39  

39  

40  
value
40  
value
41  
parse(
41  
parse(
42  
    string_view s,
42  
    string_view s,
43  
    std::error_code& ec,
43  
    std::error_code& ec,
44  
    storage_ptr sp,
44  
    storage_ptr sp,
45  
    parse_options const& opt)
45  
    parse_options const& opt)
46  
{
46  
{
47  
    system::error_code jec;
47  
    system::error_code jec;
48  
    value result = parse(s, jec, std::move(sp), opt);
48  
    value result = parse(s, jec, std::move(sp), opt);
49  
    ec = jec;
49  
    ec = jec;
50  
    return result;
50  
    return result;
51  
}
51  
}
52  

52  

53  
value
53  
value
54  
parse(
54  
parse(
55  
    string_view s,
55  
    string_view s,
56  
    storage_ptr sp,
56  
    storage_ptr sp,
57  
    const parse_options& opt)
57  
    const parse_options& opt)
58  
{
58  
{
59  
    system::error_code ec;
59  
    system::error_code ec;
60  
    auto jv = parse(
60  
    auto jv = parse(
61  
        s, ec, std::move(sp), opt);
61  
        s, ec, std::move(sp), opt);
62  
    if(ec)
62  
    if(ec)
63  
        detail::throw_system_error( ec );
63  
        detail::throw_system_error( ec );
64  
    return jv;
64  
    return jv;
65  
}
65  
}
66  

66  

67  
value
67  
value
68  
parse(
68  
parse(
69  
    std::istream& is,
69  
    std::istream& is,
70  
    system::error_code& ec,
70  
    system::error_code& ec,
71  
    storage_ptr sp,
71  
    storage_ptr sp,
72  
    parse_options const& opt)
72  
    parse_options const& opt)
73  
{
73  
{
74  
    unsigned char parser_buffer[BOOST_JSON_STACK_BUFFER_SIZE / 2];
74  
    unsigned char parser_buffer[BOOST_JSON_STACK_BUFFER_SIZE / 2];
75  
    stream_parser p(storage_ptr(), opt, parser_buffer);
75  
    stream_parser p(storage_ptr(), opt, parser_buffer);
76  
    p.reset(std::move(sp));
76  
    p.reset(std::move(sp));
77  

77  

78  
    char read_buffer[BOOST_JSON_STACK_BUFFER_SIZE / 2];
78  
    char read_buffer[BOOST_JSON_STACK_BUFFER_SIZE / 2];
79  
    do
79  
    do
80  
    {
80  
    {
81  
        if( is.eof() )
81  
        if( is.eof() )
82  
        {
82  
        {
83  
            p.finish(ec);
83  
            p.finish(ec);
84  
            break;
84  
            break;
85  
        }
85  
        }
86  

86  

87  
        if( !is )
87  
        if( !is )
88  
        {
88  
        {
89  
            BOOST_JSON_FAIL( ec, error::input_error );
89  
            BOOST_JSON_FAIL( ec, error::input_error );
90  
            break;
90  
            break;
91  
        }
91  
        }
92  

92  

93  
        is.read(read_buffer, sizeof(read_buffer));
93  
        is.read(read_buffer, sizeof(read_buffer));
94  
        auto const consumed = is.gcount();
94  
        auto const consumed = is.gcount();
95  

95  

96  
        p.write( read_buffer, static_cast<std::size_t>(consumed), ec );
96  
        p.write( read_buffer, static_cast<std::size_t>(consumed), ec );
97  
    }
97  
    }
98  
    while( !ec.failed() );
98  
    while( !ec.failed() );
99  

99  

100  
    if( ec.failed() )
100  
    if( ec.failed() )
101  
        return nullptr;
101  
        return nullptr;
102  

102  

103  
    return p.release();
103  
    return p.release();
104  
}
104  
}
105  

105  

106  
value
106  
value
107  
parse(
107  
parse(
108  
    std::istream& is,
108  
    std::istream& is,
109  
    std::error_code& ec,
109  
    std::error_code& ec,
110  
    storage_ptr sp,
110  
    storage_ptr sp,
111  
    parse_options const& opt)
111  
    parse_options const& opt)
112  
{
112  
{
113  
    system::error_code jec;
113  
    system::error_code jec;
114  
    value result = parse(is, jec, std::move(sp), opt);
114  
    value result = parse(is, jec, std::move(sp), opt);
115  
    ec = jec;
115  
    ec = jec;
116  
    return result;
116  
    return result;
117  
}
117  
}
118  

118  

119  
value
119  
value
120  
parse(
120  
parse(
121  
    std::istream& is,
121  
    std::istream& is,
122  
    storage_ptr sp,
122  
    storage_ptr sp,
123  
    parse_options const& opt)
123  
    parse_options const& opt)
124  
{
124  
{
125  
    system::error_code ec;
125  
    system::error_code ec;
126  
    auto jv = parse(
126  
    auto jv = parse(
127  
        is, ec, std::move(sp), opt);
127  
        is, ec, std::move(sp), opt);
128  
    if(ec)
128  
    if(ec)
129  
        detail::throw_system_error( ec );
129  
        detail::throw_system_error( ec );
130  
    return jv;
130  
    return jv;
131  
}
131  
}
132  

132  

133  
} // namespace json
133  
} // namespace json
134  
} // namespace boost
134  
} // namespace boost
135  

135  

136  
#endif
136  
#endif