From 1a1381f071156cffdc8f3d90d413e721ea6467a7 Mon Sep 17 00:00:00 2001 From: David Pfahler Date: Wed, 21 Apr 2021 10:24:01 +0200 Subject: [PATCH 1/4] Fixes #2728 includes some macros to be defined for using without file io. --- include/nlohmann/detail/input/input_adapters.hpp | 4 ++++ include/nlohmann/detail/output/output_adapters.hpp | 4 ++++ include/nlohmann/json.hpp | 8 ++++---- 3 files changed, 12 insertions(+), 4 deletions(-) diff --git a/include/nlohmann/detail/input/input_adapters.hpp b/include/nlohmann/detail/input/input_adapters.hpp index 63921ca55..9bb4a2b48 100644 --- a/include/nlohmann/detail/input/input_adapters.hpp +++ b/include/nlohmann/detail/input/input_adapters.hpp @@ -26,6 +26,7 @@ enum class input_format_t { json, cbor, msgpack, ubjson, bson }; // input adapters // //////////////////// +#ifndef JSON_NO_IO /*! 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. @@ -115,6 +116,7 @@ class input_stream_adapter std::istream* is = nullptr; std::streambuf* sb = nullptr; }; +#endif // JSON_NO_IO // General-purpose iterator-based adapter. It might not be as fast as // theoretically possible for some containers, but it is extremely versatile. @@ -381,6 +383,7 @@ auto input_adapter(const ContainerType& container) -> decltype(input_adapter(beg return input_adapter(begin(container), end(container)); } +#ifndef JSON_NO_IO // Special cases with fast paths inline file_input_adapter input_adapter(std::FILE* file) { @@ -396,6 +399,7 @@ inline input_stream_adapter input_adapter(std::istream&& stream) { return input_stream_adapter(stream); } +#endif // JSON_NO_IO using contiguous_bytes_input_adapter = decltype(input_adapter(std::declval(), std::declval())); diff --git a/include/nlohmann/detail/output/output_adapters.hpp b/include/nlohmann/detail/output/output_adapters.hpp index 71ca65b92..46c82e9fd 100644 --- a/include/nlohmann/detail/output/output_adapters.hpp +++ b/include/nlohmann/detail/output/output_adapters.hpp @@ -50,6 +50,7 @@ class output_vector_adapter : public output_adapter_protocol std::vector& v; }; +#ifndef JSON_NO_IO /// output adapter for output streams template class output_stream_adapter : public output_adapter_protocol @@ -73,6 +74,7 @@ class output_stream_adapter : public output_adapter_protocol private: std::basic_ostream& stream; }; +#endif // JSON_NO_IO /// output adapter for basic_string template> @@ -105,8 +107,10 @@ class output_adapter output_adapter(std::vector& vec) : oa(std::make_shared>(vec)) {} +#ifndef JSON_NO_IO output_adapter(std::basic_ostream& s) : oa(std::make_shared>(s)) {} +#endif // JSON_NO_IO output_adapter(StringType& s) : oa(std::make_shared>(s)) {} diff --git a/include/nlohmann/json.hpp b/include/nlohmann/json.hpp index a9e6c76d1..a783e3d59 100644 --- a/include/nlohmann/json.hpp +++ b/include/nlohmann/json.hpp @@ -6521,7 +6521,7 @@ class basic_json /// @name serialization /// @{ - +#ifndef JSON_NO_IO /*! @brief serialize to stream @@ -6581,7 +6581,7 @@ class basic_json { return o << j; } - +#endif // JSON_NO_IO /// @} @@ -6837,7 +6837,7 @@ class basic_json ? parser(std::move(ia), nullptr, true, ignore_comments).sax_parse(sax, strict) : detail::binary_reader(std::move(ia)).sax_parse(format, sax, strict); } - +#ifndef JSON_NO_IO /*! @brief deserialize from stream @deprecated This stream operator is deprecated and will be removed in @@ -6882,7 +6882,7 @@ class basic_json parser(detail::input_adapter(i)).parse(false, j); return i; } - +#endif // JSON_NO_IO /// @} /////////////////////////// From 0a2de2f5b9149c80825d2cf19ebf7f2c5cb8b511 Mon Sep 17 00:00:00 2001 From: David Pfahler Date: Wed, 21 Apr 2021 12:54:55 +0200 Subject: [PATCH 2/4] fixed amalgation file for #2728 --- single_include/nlohmann/json.hpp | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/single_include/nlohmann/json.hpp b/single_include/nlohmann/json.hpp index a70aaf8cb..752f8d8af 100644 --- a/single_include/nlohmann/json.hpp +++ b/single_include/nlohmann/json.hpp @@ -4747,6 +4747,7 @@ enum class input_format_t { json, cbor, msgpack, ubjson, bson }; // input adapters // //////////////////// +#ifndef JSON_NO_IO /*! 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. @@ -4836,6 +4837,7 @@ class input_stream_adapter std::istream* is = nullptr; std::streambuf* sb = nullptr; }; +#endif // JSON_NO_IO // General-purpose iterator-based adapter. It might not be as fast as // theoretically possible for some containers, but it is extremely versatile. @@ -5102,6 +5104,7 @@ auto input_adapter(const ContainerType& container) -> decltype(input_adapter(beg return input_adapter(begin(container), end(container)); } +#ifndef JSON_NO_IO // Special cases with fast paths inline file_input_adapter input_adapter(std::FILE* file) { @@ -5117,6 +5120,7 @@ inline input_stream_adapter input_adapter(std::istream&& stream) { return input_stream_adapter(stream); } +#endif // JSON_NO_IO using contiguous_bytes_input_adapter = decltype(input_adapter(std::declval(), std::declval())); @@ -12664,6 +12668,7 @@ class output_vector_adapter : public output_adapter_protocol std::vector& v; }; +#ifndef JSON_NO_IO /// output adapter for output streams template class output_stream_adapter : public output_adapter_protocol @@ -12687,6 +12692,7 @@ class output_stream_adapter : public output_adapter_protocol private: std::basic_ostream& stream; }; +#endif // JSON_NO_IO /// output adapter for basic_string template> @@ -12719,8 +12725,10 @@ class output_adapter output_adapter(std::vector& vec) : oa(std::make_shared>(vec)) {} +#ifndef JSON_NO_IO output_adapter(std::basic_ostream& s) : oa(std::make_shared>(s)) {} +#endif // JSON_NO_IO output_adapter(StringType& s) : oa(std::make_shared>(s)) {} @@ -23009,7 +23017,7 @@ class basic_json /// @name serialization /// @{ - +#ifndef JSON_NO_IO /*! @brief serialize to stream @@ -23069,7 +23077,7 @@ class basic_json { return o << j; } - +#endif // JSON_NO_IO /// @} @@ -23325,7 +23333,7 @@ class basic_json ? parser(std::move(ia), nullptr, true, ignore_comments).sax_parse(sax, strict) : detail::binary_reader(std::move(ia)).sax_parse(format, sax, strict); } - +#ifndef JSON_NO_IO /*! @brief deserialize from stream @deprecated This stream operator is deprecated and will be removed in @@ -23370,7 +23378,7 @@ class basic_json parser(detail::input_adapter(i)).parse(false, j); return i; } - +#endif // JSON_NO_IO /// @} /////////////////////////// From ae9bbbc9412656e9d71be0862ba919dccf0b21ec Mon Sep 17 00:00:00 2001 From: David Pfahler Date: Mon, 31 May 2021 14:26:45 +0200 Subject: [PATCH 3/4] include io only if JSON_NO_IO is not set for #2728 --- include/nlohmann/detail/input/input_adapters.hpp | 7 +++++-- include/nlohmann/detail/output/output_adapters.hpp | 8 ++++++-- include/nlohmann/json.hpp | 6 ++++-- 3 files changed, 15 insertions(+), 6 deletions(-) diff --git a/include/nlohmann/detail/input/input_adapters.hpp b/include/nlohmann/detail/input/input_adapters.hpp index 9bb4a2b48..fd9a3ab88 100644 --- a/include/nlohmann/detail/input/input_adapters.hpp +++ b/include/nlohmann/detail/input/input_adapters.hpp @@ -2,9 +2,7 @@ #include // array #include // size_t -#include //FILE * #include // strlen -#include // istream #include // begin, end, iterator_traits, random_access_iterator_tag, distance, next #include // shared_ptr, make_shared, addressof #include // accumulate @@ -12,6 +10,11 @@ #include // enable_if, is_base_of, is_pointer, is_integral, remove_pointer #include // pair, declval +#ifndef JSON_NO_IO + #include //FILE * + #include // istream +#endif // JSON_NO_IO + #include #include diff --git a/include/nlohmann/detail/output/output_adapters.hpp b/include/nlohmann/detail/output/output_adapters.hpp index 46c82e9fd..d192da4e5 100644 --- a/include/nlohmann/detail/output/output_adapters.hpp +++ b/include/nlohmann/detail/output/output_adapters.hpp @@ -2,12 +2,16 @@ #include // copy #include // size_t -#include // streamsize #include // back_inserter #include // shared_ptr, make_shared -#include // basic_ostream #include // basic_string #include // vector + +#ifndef JSON_NO_IO + #include // streamsize + #include // basic_ostream +#endif // JSON_NO_IO + #include namespace nlohmann diff --git a/include/nlohmann/json.hpp b/include/nlohmann/json.hpp index a783e3d59..70e263bf4 100644 --- a/include/nlohmann/json.hpp +++ b/include/nlohmann/json.hpp @@ -38,7 +38,9 @@ SOFTWARE. #include // nullptr_t, ptrdiff_t, size_t #include // hash, less #include // initializer_list -#include // istream, ostream +#ifndef JSON_NO_IO + #include // istream, ostream +#endif // JSON_NO_IO #include // random_access_iterator_tag #include // unique_ptr #include // accumulate @@ -6882,7 +6884,7 @@ class basic_json parser(detail::input_adapter(i)).parse(false, j); return i; } -#endif // JSON_NO_IO +#endif // JSON_NO_IO /// @} /////////////////////////// From e939b59683281cc4c061597868e78786a9812482 Mon Sep 17 00:00:00 2001 From: David Pfahler Date: Mon, 31 May 2021 14:27:23 +0200 Subject: [PATCH 4/4] fixed amalgation file for #2728 --- single_include/nlohmann/json.hpp | 21 +++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/single_include/nlohmann/json.hpp b/single_include/nlohmann/json.hpp index 752f8d8af..3ccab3001 100644 --- a/single_include/nlohmann/json.hpp +++ b/single_include/nlohmann/json.hpp @@ -38,7 +38,9 @@ SOFTWARE. #include // nullptr_t, ptrdiff_t, size_t #include // hash, less #include // initializer_list -#include // istream, ostream +#ifndef JSON_NO_IO + #include // istream, ostream +#endif // JSON_NO_IO #include // random_access_iterator_tag #include // unique_ptr #include // accumulate @@ -4721,9 +4723,7 @@ std::size_t hash(const BasicJsonType& j) #include // array #include // size_t -#include //FILE * #include // strlen -#include // istream #include // begin, end, iterator_traits, random_access_iterator_tag, distance, next #include // shared_ptr, make_shared, addressof #include // accumulate @@ -4731,6 +4731,11 @@ std::size_t hash(const BasicJsonType& j) #include // enable_if, is_base_of, is_pointer, is_integral, remove_pointer #include // pair, declval +#ifndef JSON_NO_IO + #include //FILE * + #include // istream +#endif // JSON_NO_IO + // #include // #include @@ -12619,12 +12624,16 @@ class json_ref #include // copy #include // size_t -#include // streamsize #include // back_inserter #include // shared_ptr, make_shared -#include // basic_ostream #include // basic_string #include // vector + +#ifndef JSON_NO_IO + #include // streamsize + #include // basic_ostream +#endif // JSON_NO_IO + // #include @@ -23378,7 +23387,7 @@ class basic_json parser(detail::input_adapter(i)).parse(false, j); return i; } -#endif // JSON_NO_IO +#endif // JSON_NO_IO /// @} ///////////////////////////