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_INTO_HPP
11  
#ifndef BOOST_JSON_IMPL_PARSE_INTO_HPP
12  
#define BOOST_JSON_IMPL_PARSE_INTO_HPP
12  
#define BOOST_JSON_IMPL_PARSE_INTO_HPP
13  

13  

14  
#include <boost/json/basic_parser_impl.hpp>
14  
#include <boost/json/basic_parser_impl.hpp>
15  
#include <boost/json/error.hpp>
15  
#include <boost/json/error.hpp>
16  
#include <istream>
16  
#include <istream>
17  

17  

18  
namespace boost {
18  
namespace boost {
19  
namespace json {
19  
namespace json {
20  

20  

21  
template<class V>
21  
template<class V>
22  
void
22  
void
23  
parse_into(
23  
parse_into(
24  
    V& v,
24  
    V& v,
25  
    string_view sv,
25  
    string_view sv,
26  
    system::error_code& ec,
26  
    system::error_code& ec,
27  
    parse_options const& opt )
27  
    parse_options const& opt )
28  
{
28  
{
29  
    parser_for<V> p( opt, &v );
29  
    parser_for<V> p( opt, &v );
30  

30  

31  
    std::size_t n = p.write_some( false, sv.data(), sv.size(), ec );
31  
    std::size_t n = p.write_some( false, sv.data(), sv.size(), ec );
32  

32  

33  
    if( !ec && n < sv.size() )
33  
    if( !ec && n < sv.size() )
34  
    {
34  
    {
35  
        BOOST_JSON_FAIL( ec, error::extra_data );
35  
        BOOST_JSON_FAIL( ec, error::extra_data );
36  
    }
36  
    }
37  
}
37  
}
38  

38  

39  
template<class V>
39  
template<class V>
40  
void
40  
void
41  
parse_into(
41  
parse_into(
42  
    V& v,
42  
    V& v,
43  
    string_view sv,
43  
    string_view sv,
44  
    std::error_code& ec,
44  
    std::error_code& ec,
45  
    parse_options const& opt )
45  
    parse_options const& opt )
46  
{
46  
{
47  
    system::error_code jec;
47  
    system::error_code jec;
48  
    parse_into(v, sv, jec, opt);
48  
    parse_into(v, sv, jec, opt);
49  
    ec = jec;
49  
    ec = jec;
50  
}
50  
}
51  

51  

52  
template<class V>
52  
template<class V>
53  
void
53  
void
54  
parse_into(
54  
parse_into(
55  
    V& v,
55  
    V& v,
56  
    string_view sv,
56  
    string_view sv,
57  
    parse_options const& opt )
57  
    parse_options const& opt )
58  
{
58  
{
59  
    system::error_code ec;
59  
    system::error_code ec;
60  
    parse_into(v, sv, ec, opt);
60  
    parse_into(v, sv, ec, opt);
61  
    if( ec.failed() )
61  
    if( ec.failed() )
62  
        detail::throw_system_error( ec );
62  
        detail::throw_system_error( ec );
63  
}
63  
}
64  

64  

65  
template<class V>
65  
template<class V>
66  
void
66  
void
67  
parse_into(
67  
parse_into(
68  
    V& v,
68  
    V& v,
69  
    std::istream& is,
69  
    std::istream& is,
70  
    system::error_code& ec,
70  
    system::error_code& ec,
71  
    parse_options const& opt )
71  
    parse_options const& opt )
72  
{
72  
{
73  
    parser_for<V> p( opt, &v );
73  
    parser_for<V> p( opt, &v );
74  

74  

75  
    char read_buffer[BOOST_JSON_STACK_BUFFER_SIZE];
75  
    char read_buffer[BOOST_JSON_STACK_BUFFER_SIZE];
76  
    do
76  
    do
77  
    {
77  
    {
78  
        if( is.eof() )
78  
        if( is.eof() )
79  
        {
79  
        {
80  
            p.write_some(false, nullptr, 0, ec);
80  
            p.write_some(false, nullptr, 0, ec);
81  
            break;
81  
            break;
82  
        }
82  
        }
83  

83  

84  
        if( !is )
84  
        if( !is )
85  
        {
85  
        {
86  
            BOOST_JSON_FAIL( ec, error::input_error );
86  
            BOOST_JSON_FAIL( ec, error::input_error );
87  
            break;
87  
            break;
88  
        }
88  
        }
89  

89  

90  
        is.read(read_buffer, sizeof(read_buffer));
90  
        is.read(read_buffer, sizeof(read_buffer));
91  
        std::size_t const consumed = static_cast<std::size_t>( is.gcount() );
91  
        std::size_t const consumed = static_cast<std::size_t>( is.gcount() );
92  

92  

93  
        std::size_t const n = p.write_some( true, read_buffer, consumed, ec );
93  
        std::size_t const n = p.write_some( true, read_buffer, consumed, ec );
94  
        if( !ec.failed() && n < consumed )
94  
        if( !ec.failed() && n < consumed )
95  
        {
95  
        {
96  
            BOOST_JSON_FAIL( ec, error::extra_data );
96  
            BOOST_JSON_FAIL( ec, error::extra_data );
97  
        }
97  
        }
98  
    }
98  
    }
99  
    while( !ec.failed() );
99  
    while( !ec.failed() );
100  
}
100  
}
101  

101  

102  
template<class V>
102  
template<class V>
103  
void
103  
void
104  
parse_into(
104  
parse_into(
105  
    V& v,
105  
    V& v,
106  
    std::istream& is,
106  
    std::istream& is,
107  
    std::error_code& ec,
107  
    std::error_code& ec,
108  
    parse_options const& opt )
108  
    parse_options const& opt )
109  
{
109  
{
110  
    system::error_code jec;
110  
    system::error_code jec;
111  
    parse_into(v, is, jec, opt);
111  
    parse_into(v, is, jec, opt);
112  
    ec = jec;
112  
    ec = jec;
113  
}
113  
}
114  

114  

115  
template<class V>
115  
template<class V>
116  
void
116  
void
117  
parse_into(
117  
parse_into(
118  
    V& v,
118  
    V& v,
119  
    std::istream& is,
119  
    std::istream& is,
120  
    parse_options const& opt )
120  
    parse_options const& opt )
121  
{
121  
{
122  
    system::error_code ec;
122  
    system::error_code ec;
123  
    parse_into(v, is, ec, opt);
123  
    parse_into(v, is, ec, opt);
124  
    if( ec.failed() )
124  
    if( ec.failed() )
125  
        detail::throw_system_error( ec );
125  
        detail::throw_system_error( ec );
126  
}
126  
}
127  

127  

128  
} // namespace boost
128  
} // namespace boost
129  
} // namespace json
129  
} // namespace json
130  

130  

131  
#endif
131  
#endif