From 67b0daf27ba7659add80662945793d26e7b4a2b1 Mon Sep 17 00:00:00 2001 From: Jonathan Dumaresq Date: Tue, 11 Dec 2018 09:33:30 -0500 Subject: [PATCH 01/12] Add the possibility of using FILE * from cstdio library to read a file. This enable the possibility of using low eand device with this library. --- single_include/nlohmann/json.hpp | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/single_include/nlohmann/json.hpp b/single_include/nlohmann/json.hpp index 7038084d..36939b31 100644 --- a/single_include/nlohmann/json.hpp +++ b/single_include/nlohmann/json.hpp @@ -55,6 +55,7 @@ SOFTWARE. #include // allocator #include // string #include // vector +#include /*! @brief namespace for Niels Lohmann @@ -2285,6 +2286,25 @@ struct wide_string_input_helper } }; +class file_input_adapter : public input_adapter_protocol +{ +public: + explicit file_input_adapter(const FILE *file) noexcept + : file(file) + {} + + std::char_traits::int_type get_character() noexcept override + { + auto res = fgetc(const_cast(file)); + if(res == EOF) + return std::char_traits::eof(); + else + return static_cast::int_type>(res); + } +private: + /// the file pointer to read from + const FILE * file; +}; template class wide_string_input_adapter : public input_adapter_protocol { @@ -2354,6 +2374,9 @@ class input_adapter input_adapter(const std::u32string& ws) : ia(std::make_shared>(ws)) {} + input_adapter(const FILE *file) + : ia(std::make_shared(file)) {} + /// input adapter for buffer template Date: Tue, 11 Dec 2018 13:17:13 -0500 Subject: [PATCH 02/12] Add the possibility of using FILE * from cstdio library to read a file. This enable the possibility of using low eand device with this library. --- .../nlohmann/detail/input/input_adapters.hpp | 25 ++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/include/nlohmann/detail/input/input_adapters.hpp b/include/nlohmann/detail/input/input_adapters.hpp index 79a19c17..1e200298 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; +class file_input_adapter : public input_adapter_protocol +{ +public: + explicit file_input_adapter(const FILE *file) noexcept + : file(file) + {} + + std::char_traits::int_type get_character() noexcept override + { + auto res = fgetc(const_cast(file)); + if(res == EOF) + return std::char_traits::eof(); + else + return static_cast::int_type>(res); + } +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 @@ -287,7 +309,8 @@ class input_adapter { public: // native support - + input_adapter(FILE * file) + : ia(std::make_shared(file)) {} /// input adapter for input stream input_adapter(std::istream& i) : ia(std::make_shared(i)) {} From ae48acbb2377543cb066a19580073991134629a5 Mon Sep 17 00:00:00 2001 From: Jonathan Dumaresq Date: Wed, 12 Dec 2018 09:28:42 -0500 Subject: [PATCH 03/12] remove non usefull code. Add small description --- include/nlohmann/detail/input/input_adapters.hpp | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/include/nlohmann/detail/input/input_adapters.hpp b/include/nlohmann/detail/input/input_adapters.hpp index 1e200298..f623a230 100644 --- a/include/nlohmann/detail/input/input_adapters.hpp +++ b/include/nlohmann/detail/input/input_adapters.hpp @@ -46,6 +46,10 @@ 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. This adapter +*/ class file_input_adapter : public input_adapter_protocol { public: @@ -55,11 +59,7 @@ public: std::char_traits::int_type get_character() noexcept override { - auto res = fgetc(const_cast(file)); - if(res == EOF) - return std::char_traits::eof(); - else - return static_cast::int_type>(res); + return fgetc(const_cast(file)); } private: /// the file pointer to read from From 3335da622a987d565e4790964c087d113a8d32d5 Mon Sep 17 00:00:00 2001 From: Jonathan Dumaresq Date: Wed, 12 Dec 2018 09:32:52 -0500 Subject: [PATCH 04/12] remove non usefull code. --- single_include/nlohmann/json.hpp | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/single_include/nlohmann/json.hpp b/single_include/nlohmann/json.hpp index 36939b31..2e1fdbd5 100644 --- a/single_include/nlohmann/json.hpp +++ b/single_include/nlohmann/json.hpp @@ -2295,16 +2295,13 @@ public: std::char_traits::int_type get_character() noexcept override { - auto res = fgetc(const_cast(file)); - if(res == EOF) - return std::char_traits::eof(); - else - return static_cast::int_type>(res); + return fgetc(const_cast(file)); } private: /// the file pointer to read from const FILE * file; }; + template class wide_string_input_adapter : public input_adapter_protocol { From ef283e0cf87fca3da2c238e116fd6ef9d1430e2a Mon Sep 17 00:00:00 2001 From: Jonathan Dumaresq Date: Wed, 12 Dec 2018 10:18:37 -0500 Subject: [PATCH 05/12] add tests to cover the new input adapter --- .../nlohmann/detail/input/input_adapters.hpp | 16 ++++---- test/src/unit-testsuites.cpp | 40 +++++++++++++++++++ 2 files changed, 48 insertions(+), 8 deletions(-) diff --git a/include/nlohmann/detail/input/input_adapters.hpp b/include/nlohmann/detail/input/input_adapters.hpp index f623a230..e0945b3f 100644 --- a/include/nlohmann/detail/input/input_adapters.hpp +++ b/include/nlohmann/detail/input/input_adapters.hpp @@ -52,18 +52,18 @@ Input adapter for stdio file access. This adapter read only 1 byte and do not us */ class file_input_adapter : public input_adapter_protocol { -public: - explicit file_input_adapter(const FILE *file) noexcept - : file(file) + public: + explicit file_input_adapter(const FILE* file) noexcept + : file(file) {} std::char_traits::int_type get_character() noexcept override { - return fgetc(const_cast(file)); + return fgetc(const_cast(file)); } -private: + private: /// the file pointer to read from - const FILE * file; + const FILE* file; }; @@ -309,8 +309,8 @@ class input_adapter { public: // native support - input_adapter(FILE * file) - : ia(std::make_shared(file)) {} + input_adapter(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 3c00fada..782c3f52 100644 --- a/test/src/unit-testsuites.cpp +++ b/test/src/unit-testsuites.cpp @@ -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"); + 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") From fa7f1a524e8cb414de67f6da51d364ea66bf8f2d Mon Sep 17 00:00:00 2001 From: Jonathan Dumaresq Date: Wed, 12 Dec 2018 10:19:22 -0500 Subject: [PATCH 06/12] new unified json.hpp generated with make amalgamate --- single_include/nlohmann/json.hpp | 45 +++++++++++++++++--------------- 1 file changed, 24 insertions(+), 21 deletions(-) diff --git a/single_include/nlohmann/json.hpp b/single_include/nlohmann/json.hpp index 2e1fdbd5..871bb9ae 100644 --- a/single_include/nlohmann/json.hpp +++ b/single_include/nlohmann/json.hpp @@ -55,7 +55,6 @@ SOFTWARE. #include // allocator #include // string #include // vector -#include /*! @brief namespace for Niels Lohmann @@ -2059,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 @@ -2095,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. This adapter +*/ +class file_input_adapter : public input_adapter_protocol +{ + public: + explicit file_input_adapter(const FILE* file) noexcept + : file(file) + {} + + std::char_traits::int_type get_character() noexcept override + { + return fgetc(const_cast(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 @@ -2286,22 +2307,6 @@ struct wide_string_input_helper } }; -class file_input_adapter : public input_adapter_protocol -{ -public: - explicit file_input_adapter(const FILE *file) noexcept - : file(file) - {} - - std::char_traits::int_type get_character() noexcept override - { - return fgetc(const_cast(file)); - } -private: - /// the file pointer to read from - const FILE * file; -}; - template class wide_string_input_adapter : public input_adapter_protocol { @@ -2353,7 +2358,8 @@ class input_adapter { public: // native support - + input_adapter(FILE* file) + : ia(std::make_shared(file)) {} /// input adapter for input stream input_adapter(std::istream& i) : ia(std::make_shared(i)) {} @@ -2371,9 +2377,6 @@ class input_adapter input_adapter(const std::u32string& ws) : ia(std::make_shared>(ws)) {} - input_adapter(const FILE *file) - : ia(std::make_shared(file)) {} - /// input adapter for buffer template Date: Wed, 12 Dec 2018 14:15:49 -0500 Subject: [PATCH 07/12] remove comment --- include/nlohmann/detail/input/input_adapters.hpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/include/nlohmann/detail/input/input_adapters.hpp b/include/nlohmann/detail/input/input_adapters.hpp index e0945b3f..83fd1609 100644 --- a/include/nlohmann/detail/input/input_adapters.hpp +++ b/include/nlohmann/detail/input/input_adapters.hpp @@ -48,7 +48,7 @@ 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. This adapter + buffer. This adapter is a very low level adapter. */ class file_input_adapter : public input_adapter_protocol { From 91ff96a7376881ab4a9933bf6a8bfbf63bf8384a Mon Sep 17 00:00:00 2001 From: Jonathan Dumaresq Date: Wed, 12 Dec 2018 14:16:12 -0500 Subject: [PATCH 08/12] remove the const attribute --- include/nlohmann/detail/input/input_adapters.hpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/include/nlohmann/detail/input/input_adapters.hpp b/include/nlohmann/detail/input/input_adapters.hpp index 83fd1609..98249774 100644 --- a/include/nlohmann/detail/input/input_adapters.hpp +++ b/include/nlohmann/detail/input/input_adapters.hpp @@ -53,17 +53,17 @@ Input adapter for stdio file access. This adapter read only 1 byte and do not us class file_input_adapter : public input_adapter_protocol { public: - explicit file_input_adapter(const FILE* file) noexcept + explicit file_input_adapter(FILE* file) noexcept : file(file) {} std::char_traits::int_type get_character() noexcept override { - return fgetc(const_cast(file)); + return fgetc(file); } private: /// the file pointer to read from - const FILE* file; + FILE* file; }; From a794cfdba3f52d80684fd526a0def4484e61b476 Mon Sep 17 00:00:00 2001 From: Jonathan Dumaresq Date: Wed, 12 Dec 2018 14:46:17 -0500 Subject: [PATCH 09/12] refactor unit test in case of throw, the fclose will not be called. using unique_ptr with custom destructor will ensure that --- test/src/unit-testsuites.cpp | 25 ++++++++++--------------- 1 file changed, 10 insertions(+), 15 deletions(-) diff --git a/test/src/unit-testsuites.cpp b/test/src/unit-testsuites.cpp index 782c3f52..fe3e0175 100644 --- a/test/src/unit-testsuites.cpp +++ b/test/src/unit-testsuites.cpp @@ -386,42 +386,37 @@ TEST_CASE("json.org examples") } SECTION("FILE 1.json") { - auto f = fopen("test/data/json.org/1.json", "r"); + std::unique_ptr f(fopen("test/data/json.org/1.json", "r"), &fclose); json j; - CHECK_NOTHROW(j.parse(f)); - fclose(f); + CHECK_NOTHROW(j.parse(f.get())); } SECTION("FILE 2.json") { - auto f = fopen("test/data/json.org/2.json", "r"); + std::unique_ptr f(fopen("test/data/json.org/2.json", "r"), &fclose); json j; - CHECK_NOTHROW(j.parse(f)); - fclose(f); + CHECK_NOTHROW(j.parse(f.get())); } SECTION("FILE 3.json") { - auto f = fopen("test/data/json.org/3.json", "r"); + std::unique_ptr f(fopen("test/data/json.org/3.json", "r"), &fclose); json j; - CHECK_NOTHROW(j.parse(f)); - fclose(f); + CHECK_NOTHROW(j.parse(f.get())); } SECTION("FILE 4.json") { - auto f = fopen("test/data/json.org/4.json", "r"); + std::unique_ptr f(fopen("test/data/json.org/4.json", "r"), &fclose); json j; - CHECK_NOTHROW(j.parse(f)); - fclose(f); + CHECK_NOTHROW(j.parse(f.get())); } SECTION("FILE 5.json") { - auto f = fopen("test/data/json.org/5.json", "r"); + std::unique_ptr f(fopen("test/data/json.org/5.json", "r"), &fclose); json j; - CHECK_NOTHROW(j.parse(f)); - fclose(f); + CHECK_NOTHROW(j.parse(f.get())); } } From cf31193de2082f6332a11df28862232169a4c271 Mon Sep 17 00:00:00 2001 From: Jonathan Dumaresq Date: Wed, 12 Dec 2018 14:46:52 -0500 Subject: [PATCH 10/12] create single json.hpp file --- single_include/nlohmann/json.hpp | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/single_include/nlohmann/json.hpp b/single_include/nlohmann/json.hpp index 871bb9ae..93ed2b24 100644 --- a/single_include/nlohmann/json.hpp +++ b/single_include/nlohmann/json.hpp @@ -2097,22 +2097,22 @@ 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. This adapter + buffer. This adapter is a very low level adapter. */ class file_input_adapter : public input_adapter_protocol { public: - explicit file_input_adapter(const FILE* file) noexcept + explicit file_input_adapter(FILE* file) noexcept : file(file) {} std::char_traits::int_type get_character() noexcept override { - return fgetc(const_cast(file)); + return fgetc(file); } private: /// the file pointer to read from - const FILE* file; + FILE* file; }; From 635a4fc3441be4f7040b0654a3ac8f9e305a70d4 Mon Sep 17 00:00:00 2001 From: Jonathan Dumaresq Date: Wed, 12 Dec 2018 16:33:25 -0500 Subject: [PATCH 11/12] use namespace std when possible. Change the name of private variable. --- include/nlohmann/detail/input/input_adapters.hpp | 8 ++++---- single_include/nlohmann/json.hpp | 8 ++++---- test/src/unit-testsuites.cpp | 10 +++++----- 3 files changed, 13 insertions(+), 13 deletions(-) diff --git a/include/nlohmann/detail/input/input_adapters.hpp b/include/nlohmann/detail/input/input_adapters.hpp index 98249774..3c097a6a 100644 --- a/include/nlohmann/detail/input/input_adapters.hpp +++ b/include/nlohmann/detail/input/input_adapters.hpp @@ -53,17 +53,17 @@ Input adapter for stdio file access. This adapter read only 1 byte and do not us class file_input_adapter : public input_adapter_protocol { public: - explicit file_input_adapter(FILE* file) noexcept - : file(file) + explicit file_input_adapter(std::FILE* f) noexcept + : m_file(f) {} std::char_traits::int_type get_character() noexcept override { - return fgetc(file); + return std::fgetc(m_file); } private: /// the file pointer to read from - FILE* file; + std::FILE* m_file; }; diff --git a/single_include/nlohmann/json.hpp b/single_include/nlohmann/json.hpp index 93ed2b24..781d6f14 100644 --- a/single_include/nlohmann/json.hpp +++ b/single_include/nlohmann/json.hpp @@ -2102,17 +2102,17 @@ Input adapter for stdio file access. This adapter read only 1 byte and do not us class file_input_adapter : public input_adapter_protocol { public: - explicit file_input_adapter(FILE* file) noexcept - : file(file) + explicit file_input_adapter(std::FILE* f) noexcept + : m_file(f) {} std::char_traits::int_type get_character() noexcept override { - return fgetc(file); + return std::fgetc(m_file); } private: /// the file pointer to read from - FILE* file; + std::FILE* m_file; }; diff --git a/test/src/unit-testsuites.cpp b/test/src/unit-testsuites.cpp index fe3e0175..9563b498 100644 --- a/test/src/unit-testsuites.cpp +++ b/test/src/unit-testsuites.cpp @@ -386,35 +386,35 @@ TEST_CASE("json.org examples") } SECTION("FILE 1.json") { - std::unique_ptr f(fopen("test/data/json.org/1.json", "r"), &fclose); + 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(fopen("test/data/json.org/2.json", "r"), &fclose); + 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(fopen("test/data/json.org/3.json", "r"), &fclose); + 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(fopen("test/data/json.org/4.json", "r"), &fclose); + 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(fopen("test/data/json.org/5.json", "r"), &fclose); + std::unique_ptr f(std::fopen("test/data/json.org/5.json", "r"), &std::fclose); json j; CHECK_NOTHROW(j.parse(f.get())); } From c1c85b025ca3f8619ba80c6c26ca1b52f282f3bb Mon Sep 17 00:00:00 2001 From: Jonathan Dumaresq Date: Fri, 14 Dec 2018 07:33:28 -0500 Subject: [PATCH 12/12] Forget one std::FILE --- include/nlohmann/detail/input/input_adapters.hpp | 2 +- single_include/nlohmann/json.hpp | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/include/nlohmann/detail/input/input_adapters.hpp b/include/nlohmann/detail/input/input_adapters.hpp index 3c097a6a..aaf9ebc5 100644 --- a/include/nlohmann/detail/input/input_adapters.hpp +++ b/include/nlohmann/detail/input/input_adapters.hpp @@ -309,7 +309,7 @@ class input_adapter { public: // native support - input_adapter(FILE* file) + input_adapter(std::FILE* file) : ia(std::make_shared(file)) {} /// input adapter for input stream input_adapter(std::istream& i) diff --git a/single_include/nlohmann/json.hpp b/single_include/nlohmann/json.hpp index 781d6f14..f1dd9bea 100644 --- a/single_include/nlohmann/json.hpp +++ b/single_include/nlohmann/json.hpp @@ -2358,7 +2358,7 @@ class input_adapter { public: // native support - input_adapter(FILE* file) + input_adapter(std::FILE* file) : ia(std::make_shared(file)) {} /// input adapter for input stream input_adapter(std::istream& i)