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, class Ctx >
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 +
    Ctx const& ctx )
28  
{
29  
{
29 -
    parser_for<V> p( opt, &v );
30 +
    parser_for<V, Ctx> p(opt, &v, ctx);
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 -

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

37  

39 -
template<class V>
38 +
template< class V, class Ctx >
40  
void
39  
void
41  
parse_into(
40  
parse_into(
42  
    V& v,
41  
    V& v,
43  
    string_view sv,
42  
    string_view sv,
44  
    std::error_code& ec,
43  
    std::error_code& ec,
45 -
    parse_options const& opt )
44 +
    parse_options const& opt,
 
45 +
    Ctx const& ctx )
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, ctx);
49  
    ec = jec;
49  
    ec = jec;
50  
}
50  
}
51  

51  

52 -
template<class V>
52 +
template< class V, class Ctx >
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 +
    Ctx const& ctx )
58  
{
59  
{
59  
    system::error_code ec;
60  
    system::error_code ec;
60 -
    parse_into(v, sv, ec, opt);
61 +
    parse_into(v, sv, ec, opt, ctx);
61  
    if( ec.failed() )
62  
    if( ec.failed() )
62  
        detail::throw_system_error( ec );
63  
        detail::throw_system_error( ec );
63  
}
64  
}
64  

65  

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

76  

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

85  

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

91  

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

94  

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

103  

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

117  

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

131  

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

134  

131  
#endif
135  
#endif