Skip to content

Commit

Permalink
chunk_reader.hpp
Browse files Browse the repository at this point in the history
  • Loading branch information
danielaparker committed Nov 13, 2024
1 parent b7051c9 commit 63b01a2
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 52 deletions.
7 changes: 7 additions & 0 deletions doc/ref/corelib/basic_json_parser.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,13 @@ Type |Definition
json_parser |`jsoncons::basic_json_parser<char,std::allocator<char>>`
wjson_parser |`jsoncons::basic_json_parser<wchar_t,std::allocator<char>>`

#### Member types

Type |Definition
---------------------------|------------------------------
char_type |CharT
temp_allocator_type |TempAllocator

#### Constructors

basic_json_parser(const TempAllocator& temp_alloc = TempAllocator()); (1)
Expand Down
2 changes: 1 addition & 1 deletion doc/ref/corelib/basic_parser_input.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
### jsoncons::basic_parser_input

```cpp
#include <jsoncons/source.hpp>
#include <jsoncons/chunk_reader.hpp>

template <typename CharT>
class basic_parser_input;
Expand Down
2 changes: 1 addition & 1 deletion examples/src/json_parser_examples.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ int main()
std::cout << "\njson_parser examples\n\n";

incremental_parsing_example();
//parse_nan_replacement_example();
parse_nan_replacement_example();

std::cout << std::endl;
}
Expand Down
67 changes: 67 additions & 0 deletions include/jsoncons/chunk_reader.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
// Copyright 2013-2024 Daniel Parker
// Distributed under the Boost license, Version 1.0.
// (See accompanying file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)

// See https://github.com/danielaparker/jsoncons for latest version

#ifndef JSONCONS_CHUNK_READER_HPP
#define JSONCONS_CHUNK_READER_HPP

#include <system_error>
#include <functional>

namespace jsoncons {

template <typename CharT>
class basic_parser_input
{
public:
using char_type = CharT;

virtual ~basic_parser_input() = default;
virtual void set_buffer(const CharT* data, std::size_t length) = 0;
};

template <typename CharT>
class chunk_reader
{
public:
using char_type = CharT;

virtual ~chunk_reader() = default;
virtual bool read_chunk(basic_parser_input<char_type>&, std::error_code&)
{
return false;
}
};

template <typename CharT>
class chunk_reader_adaptor : public chunk_reader<CharT>
{
using char_type = CharT;
using chunk_reader_type = std::function<bool(basic_parser_input<char_type>& input, std::error_code& ec)>;

chunk_reader_type read_chunk_;

public:
chunk_reader_adaptor()
: read_chunk_([](basic_parser_input<char_type>&, std::error_code&){return false;})
{
}
chunk_reader_adaptor(chunk_reader_type read_chunk)
: read_chunk_(read_chunk)
{
}

bool read_chunk(basic_parser_input<char_type>& input, std::error_code& ec)
{
return read_chunk_(input, ec);
}
};

using parser_input = basic_parser_input<char>;
using wparser_input = basic_parser_input<wchar_t>;

} // namespace jsoncons

#endif
1 change: 1 addition & 0 deletions include/jsoncons/json_parser.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#include <jsoncons/json_options.hpp>
#include <jsoncons/json_visitor.hpp>
#include <jsoncons/json_error.hpp>
#include <jsoncons/chunk_reader.hpp>
#include <jsoncons/detail/parse_number.hpp>

#define JSONCONS_ILLEGAL_CONTROL_CHARACTER \
Expand Down
50 changes: 0 additions & 50 deletions include/jsoncons/source.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -789,56 +789,6 @@ namespace jsoncons {
constexpr std::size_t source_reader<Source>::max_buffer_length;
#endif

template <typename CharT>
class basic_parser_input
{
public:
using char_type = CharT;

virtual ~basic_parser_input() = default;
virtual void set_buffer(const CharT* data, std::size_t length) = 0;
};

template <typename CharT>
class chunk_reader
{
public:
using char_type = CharT;

virtual ~chunk_reader() = default;
virtual bool read_chunk(basic_parser_input<char_type>&, std::error_code&)
{
return false;
}
};

template <typename CharT>
class chunk_reader_adaptor : public chunk_reader<CharT>
{
using char_type = CharT;
using chunk_reader_type = std::function<bool(basic_parser_input<char_type>& input, std::error_code& ec)>;

chunk_reader_type read_chunk_;

public:
chunk_reader_adaptor()
: read_chunk_([](basic_parser_input<char_type>&, std::error_code&){return false;})
{
}
chunk_reader_adaptor(chunk_reader_type read_chunk)
: read_chunk_(read_chunk)
{
}

bool read_chunk(basic_parser_input<char_type>& input, std::error_code& ec)
{
return read_chunk_(input, ec);
}
};

using parser_input = basic_parser_input<char>;
using wparser_input = basic_parser_input<wchar_t>;

} // namespace jsoncons

#endif

0 comments on commit 63b01a2

Please sign in to comment.