Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/add file input adapter #1392

Merged
merged 12 commits into from
Dec 16, 2018
25 changes: 24 additions & 1 deletion include/nlohmann/detail/input/input_adapters.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include <string> // string, char_traits
#include <type_traits> // enable_if, is_base_of, is_pointer, is_integral, remove_pointer
#include <utility> // pair, declval
#include <cstdio> //FILE *

#include <nlohmann/detail/macro_scope.hpp>

Expand Down Expand Up @@ -45,6 +46,27 @@ struct input_adapter_protocol
/// a type to simplify interfaces
using input_adapter_t = std::shared_ptr<input_adapter_protocol>;

/*!
Input adapter for stdio file access. This adapter read only 1 byte and do not use any
buffer. This adapter is a very low level adapter. This adapter
*/
nlohmann marked this conversation as resolved.
Show resolved Hide resolved
class file_input_adapter : public input_adapter_protocol
{
public:
explicit file_input_adapter(const FILE* file) noexcept
: file(file)
nlohmann marked this conversation as resolved.
Show resolved Hide resolved
{}

std::char_traits<char>::int_type get_character() noexcept override
{
return fgetc(const_cast<FILE*>(file));
}
private:
/// the file pointer to read from
const FILE* file;
nlohmann marked this conversation as resolved.
Show resolved Hide resolved
};


/*!
Input adapter for a (caching) istream. Ignores a UFT Byte Order Mark at
beginning of input. Does not support changing the underlying std::streambuf
Expand Down Expand Up @@ -287,7 +309,8 @@ class input_adapter
{
public:
// native support

input_adapter(FILE* file)
nlohmann marked this conversation as resolved.
Show resolved Hide resolved
: ia(std::make_shared<file_input_adapter>(file)) {}
/// input adapter for input stream
input_adapter(std::istream& i)
: ia(std::make_shared<input_stream_adapter>(i)) {}
Expand Down
25 changes: 24 additions & 1 deletion single_include/nlohmann/json.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2058,6 +2058,7 @@ constexpr const auto& to_json = detail::static_const<detail::to_json_fn>::value;
#include <string> // string, char_traits
#include <type_traits> // enable_if, is_base_of, is_pointer, is_integral, remove_pointer
#include <utility> // pair, declval
#include <cstdio> //FILE *

// #include <nlohmann/detail/macro_scope.hpp>

Expand Down Expand Up @@ -2094,6 +2095,27 @@ struct input_adapter_protocol
/// a type to simplify interfaces
using input_adapter_t = std::shared_ptr<input_adapter_protocol>;

/*!
Input adapter for stdio file access. This adapter read only 1 byte and do not use any
buffer. This adapter is a very low level adapter. This adapter
*/
class file_input_adapter : public input_adapter_protocol
{
public:
explicit file_input_adapter(const FILE* file) noexcept
: file(file)
{}

std::char_traits<char>::int_type get_character() noexcept override
{
return fgetc(const_cast<FILE*>(file));
}
private:
/// the file pointer to read from
const FILE* file;
};


/*!
Input adapter for a (caching) istream. Ignores a UFT Byte Order Mark at
beginning of input. Does not support changing the underlying std::streambuf
Expand Down Expand Up @@ -2336,7 +2358,8 @@ class input_adapter
{
public:
// native support

input_adapter(FILE* file)
: ia(std::make_shared<file_input_adapter>(file)) {}
/// input adapter for input stream
input_adapter(std::istream& i)
: ia(std::make_shared<input_stream_adapter>(i)) {}
Expand Down
40 changes: 40 additions & 0 deletions test/src/unit-testsuites.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -384,6 +384,46 @@ TEST_CASE("json.org examples")
json j;
CHECK_NOTHROW(f >> j);
}
SECTION("FILE 1.json")
{
auto f = fopen("test/data/json.org/1.json", "r");
nlohmann marked this conversation as resolved.
Show resolved Hide resolved
json j;
CHECK_NOTHROW(j.parse(f));
fclose(f);
}

SECTION("FILE 2.json")
{
auto f = fopen("test/data/json.org/2.json", "r");
json j;
CHECK_NOTHROW(j.parse(f));
fclose(f);
}

SECTION("FILE 3.json")
{
auto f = fopen("test/data/json.org/3.json", "r");
json j;
CHECK_NOTHROW(j.parse(f));
fclose(f);
}

SECTION("FILE 4.json")
{
auto f = fopen("test/data/json.org/4.json", "r");
json j;
CHECK_NOTHROW(j.parse(f));
fclose(f);
}

SECTION("FILE 5.json")
{
auto f = fopen("test/data/json.org/5.json", "r");
json j;
CHECK_NOTHROW(j.parse(f));
fclose(f);
}

}

TEST_CASE("RFC 7159 examples")
Expand Down