diff --git a/include/nlohmann/detail/input/input_adapters.hpp b/include/nlohmann/detail/input/input_adapters.hpp index e9990767be..b16183ef0f 100644 --- a/include/nlohmann/detail/input/input_adapters.hpp +++ b/include/nlohmann/detail/input/input_adapters.hpp @@ -10,6 +10,7 @@ #include // string, char_traits #include // enable_if, is_base_of, is_pointer, is_integral, remove_pointer #include // pair, declval +#include //FILE * #include @@ -45,6 +46,27 @@ struct input_adapter_protocol /// a type to simplify interfaces using input_adapter_t = std::shared_ptr; +/*! +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. +*/ +class file_input_adapter : public input_adapter_protocol +{ + public: + explicit file_input_adapter(std::FILE* f) noexcept + : m_file(f) + {} + + std::char_traits::int_type get_character() noexcept override + { + return std::fgetc(m_file); + } + private: + /// the file pointer to read from + std::FILE* m_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 @@ -293,7 +315,8 @@ class input_adapter { public: // native support - + input_adapter(std::FILE* file) + : ia(std::make_shared(file)) {} /// input adapter for input stream input_adapter(std::istream& i) : ia(std::make_shared(i)) {} diff --git a/single_include/nlohmann/json.hpp b/single_include/nlohmann/json.hpp index 016ba39d7b..f044d3a29b 100644 --- a/single_include/nlohmann/json.hpp +++ b/single_include/nlohmann/json.hpp @@ -2058,6 +2058,7 @@ constexpr const auto& to_json = detail::static_const::value; #include // string, char_traits #include // enable_if, is_base_of, is_pointer, is_integral, remove_pointer #include // pair, declval +#include //FILE * // #include @@ -2094,6 +2095,27 @@ struct input_adapter_protocol /// a type to simplify interfaces using input_adapter_t = std::shared_ptr; +/*! +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. +*/ +class file_input_adapter : public input_adapter_protocol +{ + public: + explicit file_input_adapter(std::FILE* f) noexcept + : m_file(f) + {} + + std::char_traits::int_type get_character() noexcept override + { + return std::fgetc(m_file); + } + private: + /// the file pointer to read from + std::FILE* m_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 @@ -2342,7 +2364,8 @@ class input_adapter { public: // native support - + input_adapter(std::FILE* file) + : ia(std::make_shared(file)) {} /// input adapter for input stream input_adapter(std::istream& i) : ia(std::make_shared(i)) {} diff --git a/test/src/unit-testsuites.cpp b/test/src/unit-testsuites.cpp index 3c00fadaaf..9563b4988b 100644 --- a/test/src/unit-testsuites.cpp +++ b/test/src/unit-testsuites.cpp @@ -384,6 +384,41 @@ TEST_CASE("json.org examples") json j; CHECK_NOTHROW(f >> j); } + SECTION("FILE 1.json") + { + std::unique_ptr f(std::fopen("test/data/json.org/1.json", "r"), &std::fclose); + json j; + CHECK_NOTHROW(j.parse(f.get())); + } + + SECTION("FILE 2.json") + { + std::unique_ptr f(std::fopen("test/data/json.org/2.json", "r"), &std::fclose); + json j; + CHECK_NOTHROW(j.parse(f.get())); + } + + SECTION("FILE 3.json") + { + std::unique_ptr f(std::fopen("test/data/json.org/3.json", "r"), &std::fclose); + json j; + CHECK_NOTHROW(j.parse(f.get())); + } + + SECTION("FILE 4.json") + { + std::unique_ptr f(std::fopen("test/data/json.org/4.json", "r"), &std::fclose); + json j; + CHECK_NOTHROW(j.parse(f.get())); + } + + SECTION("FILE 5.json") + { + std::unique_ptr f(std::fopen("test/data/json.org/5.json", "r"), &std::fclose); + json j; + CHECK_NOTHROW(j.parse(f.get())); + } + } TEST_CASE("RFC 7159 examples")