From dfe607c6ff15a9f930be9907aa38025838037b86 Mon Sep 17 00:00:00 2001 From: Dennis Fischer Date: Mon, 20 Aug 2018 09:23:12 +0200 Subject: [PATCH 01/50] Export package to allow builds without installing --- CMakeLists.txt | 1 + 1 file changed, 1 insertion(+) diff --git a/CMakeLists.txt b/CMakeLists.txt index 031a66f23..3e2e3d489 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -121,3 +121,4 @@ install( NAMESPACE ${PROJECT_NAME}:: DESTINATION ${NLOHMANN_JSON_CONFIG_INSTALL_DIR} ) +export(PACKAGE ${PROJECT_NAME}) \ No newline at end of file From aada309f6131c1707fdeab480b65a38155653d0d Mon Sep 17 00:00:00 2001 From: Hyeon Kim Date: Fri, 24 Aug 2018 21:52:24 +0900 Subject: [PATCH 02/50] Fix #1213 --- include/nlohmann/detail/macro_scope.hpp | 1 + single_include/nlohmann/json.hpp | 1 + 2 files changed, 2 insertions(+) diff --git a/include/nlohmann/detail/macro_scope.hpp b/include/nlohmann/detail/macro_scope.hpp index a5b6101e8..5f0824fea 100644 --- a/include/nlohmann/detail/macro_scope.hpp +++ b/include/nlohmann/detail/macro_scope.hpp @@ -62,6 +62,7 @@ #if defined(JSON_CATCH_USER) #undef JSON_CATCH #define JSON_CATCH JSON_CATCH_USER + #undef JSON_INTERNAL_CATCH #define JSON_INTERNAL_CATCH JSON_CATCH_USER #endif #if defined(JSON_INTERNAL_CATCH_USER) diff --git a/single_include/nlohmann/json.hpp b/single_include/nlohmann/json.hpp index b80386f3f..ac27d3d4c 100644 --- a/single_include/nlohmann/json.hpp +++ b/single_include/nlohmann/json.hpp @@ -177,6 +177,7 @@ using json = basic_json<>; #if defined(JSON_CATCH_USER) #undef JSON_CATCH #define JSON_CATCH JSON_CATCH_USER + #undef JSON_INTERNAL_CATCH #define JSON_INTERNAL_CATCH JSON_CATCH_USER #endif #if defined(JSON_INTERNAL_CATCH_USER) From bbdfe7dea6457c96a329390fa7a558fe8359361d Mon Sep 17 00:00:00 2001 From: Ben Berman Date: Thu, 30 Aug 2018 12:35:51 -0400 Subject: [PATCH 03/50] Improve error messages for error 305 Addresses #1220 --- include/nlohmann/json.hpp | 12 ++++++------ single_include/nlohmann/json.hpp | 12 ++++++------ 2 files changed, 12 insertions(+), 12 deletions(-) diff --git a/include/nlohmann/json.hpp b/include/nlohmann/json.hpp index 1f39e3ec7..949df15ca 100644 --- a/include/nlohmann/json.hpp +++ b/include/nlohmann/json.hpp @@ -3097,7 +3097,7 @@ class basic_json return m_value.array->operator[](idx); } - JSON_THROW(type_error::create(305, "cannot use operator[] with " + std::string(type_name()))); + JSON_THROW(type_error::create(305, "cannot use operator[] with a numeric argument with " + std::string(type_name()))); } /*! @@ -3127,7 +3127,7 @@ class basic_json return m_value.array->operator[](idx); } - JSON_THROW(type_error::create(305, "cannot use operator[] with " + std::string(type_name()))); + JSON_THROW(type_error::create(305, "cannot use operator[] with a numeric argument with " + std::string(type_name()))); } /*! @@ -3173,7 +3173,7 @@ class basic_json return m_value.object->operator[](key); } - JSON_THROW(type_error::create(305, "cannot use operator[] with " + std::string(type_name()))); + JSON_THROW(type_error::create(305, "cannot use operator[] with a key-style argument with " + std::string(type_name()))); } /*! @@ -3215,7 +3215,7 @@ class basic_json return m_value.object->find(key)->second; } - JSON_THROW(type_error::create(305, "cannot use operator[] with " + std::string(type_name()))); + JSON_THROW(type_error::create(305, "cannot use operator[] with a key-style argument with " + std::string(type_name()))); } /*! @@ -3262,7 +3262,7 @@ class basic_json return m_value.object->operator[](key); } - JSON_THROW(type_error::create(305, "cannot use operator[] with " + std::string(type_name()))); + JSON_THROW(type_error::create(305, "cannot use operator[] with a key-style argument with " + std::string(type_name()))); } /*! @@ -3305,7 +3305,7 @@ class basic_json return m_value.object->find(key)->second; } - JSON_THROW(type_error::create(305, "cannot use operator[] with " + std::string(type_name()))); + JSON_THROW(type_error::create(305, "cannot use operator[] with a key-style argument with " + std::string(type_name()))); } /*! diff --git a/single_include/nlohmann/json.hpp b/single_include/nlohmann/json.hpp index ac27d3d4c..e60eed887 100644 --- a/single_include/nlohmann/json.hpp +++ b/single_include/nlohmann/json.hpp @@ -14204,7 +14204,7 @@ class basic_json return m_value.array->operator[](idx); } - JSON_THROW(type_error::create(305, "cannot use operator[] with " + std::string(type_name()))); + JSON_THROW(type_error::create(305, "cannot use operator[] with a numeric argument with " + std::string(type_name()))); } /*! @@ -14234,7 +14234,7 @@ class basic_json return m_value.array->operator[](idx); } - JSON_THROW(type_error::create(305, "cannot use operator[] with " + std::string(type_name()))); + JSON_THROW(type_error::create(305, "cannot use operator[] with a numeric argument with " + std::string(type_name()))); } /*! @@ -14280,7 +14280,7 @@ class basic_json return m_value.object->operator[](key); } - JSON_THROW(type_error::create(305, "cannot use operator[] with " + std::string(type_name()))); + JSON_THROW(type_error::create(305, "cannot use operator[] with a key-style argument with " + std::string(type_name()))); } /*! @@ -14322,7 +14322,7 @@ class basic_json return m_value.object->find(key)->second; } - JSON_THROW(type_error::create(305, "cannot use operator[] with " + std::string(type_name()))); + JSON_THROW(type_error::create(305, "cannot use operator[] with a key-style argument with " + std::string(type_name()))); } /*! @@ -14369,7 +14369,7 @@ class basic_json return m_value.object->operator[](key); } - JSON_THROW(type_error::create(305, "cannot use operator[] with " + std::string(type_name()))); + JSON_THROW(type_error::create(305, "cannot use operator[] with a key-style argument with " + std::string(type_name()))); } /*! @@ -14412,7 +14412,7 @@ class basic_json return m_value.object->find(key)->second; } - JSON_THROW(type_error::create(305, "cannot use operator[] with " + std::string(type_name()))); + JSON_THROW(type_error::create(305, "cannot use operator[] with a key-style argument with " + std::string(type_name()))); } /*! From ad053ef09c32a84a7792af97480e7542f4a167ed Mon Sep 17 00:00:00 2001 From: Ben Berman Date: Thu, 30 Aug 2018 13:19:09 -0400 Subject: [PATCH 04/50] Fix tests for improved error 305(hopefully) --- test/src/unit-element_access1.cpp | 26 ++++++++-------- test/src/unit-element_access2.cpp | 52 +++++++++++++++---------------- 2 files changed, 39 insertions(+), 39 deletions(-) diff --git a/test/src/unit-element_access1.cpp b/test/src/unit-element_access1.cpp index 2954d696c..2b0c69d8a 100644 --- a/test/src/unit-element_access1.cpp +++ b/test/src/unit-element_access1.cpp @@ -195,7 +195,7 @@ TEST_CASE("element access 1") const json j_nonarray_const(j_nonarray); CHECK_NOTHROW(j_nonarray[0]); CHECK_THROWS_AS(j_nonarray_const[0], json::type_error&); - CHECK_THROWS_WITH(j_nonarray_const[0], "[json.exception.type_error.305] cannot use operator[] with null"); + CHECK_THROWS_WITH(j_nonarray_const[0], "[json.exception.type_error.305] cannot use operator[] with a numeric argument with null"); } SECTION("implicit transformation to properly filled array") @@ -212,8 +212,8 @@ TEST_CASE("element access 1") const json j_nonarray_const(j_nonarray); CHECK_THROWS_AS(j_nonarray[0], json::type_error&); CHECK_THROWS_AS(j_nonarray_const[0], json::type_error&); - CHECK_THROWS_WITH(j_nonarray[0], "[json.exception.type_error.305] cannot use operator[] with boolean"); - CHECK_THROWS_WITH(j_nonarray_const[0], "[json.exception.type_error.305] cannot use operator[] with boolean"); + CHECK_THROWS_WITH(j_nonarray[0], "[json.exception.type_error.305] cannot use operator[] with a numeric argument with boolean"); + CHECK_THROWS_WITH(j_nonarray_const[0], "[json.exception.type_error.305] cannot use operator[] with a numeric argument with boolean"); } SECTION("string") @@ -222,8 +222,8 @@ TEST_CASE("element access 1") const json j_nonarray_const(j_nonarray); CHECK_THROWS_AS(j_nonarray[0], json::type_error&); CHECK_THROWS_AS(j_nonarray_const[0], json::type_error&); - CHECK_THROWS_WITH(j_nonarray[0], "[json.exception.type_error.305] cannot use operator[] with string"); - CHECK_THROWS_WITH(j_nonarray_const[0], "[json.exception.type_error.305] cannot use operator[] with string"); + CHECK_THROWS_WITH(j_nonarray[0], "[json.exception.type_error.305] cannot use operator[] with a numeric argument with string"); + CHECK_THROWS_WITH(j_nonarray_const[0], "[json.exception.type_error.305] cannot use operator[] with a numeric argument with string"); } SECTION("object") @@ -232,8 +232,8 @@ TEST_CASE("element access 1") const json j_nonarray_const(j_nonarray); CHECK_THROWS_AS(j_nonarray[0], json::type_error&); CHECK_THROWS_AS(j_nonarray_const[0], json::type_error&); - CHECK_THROWS_WITH(j_nonarray[0], "[json.exception.type_error.305] cannot use operator[] with object"); - CHECK_THROWS_WITH(j_nonarray_const[0], "[json.exception.type_error.305] cannot use operator[] with object"); + CHECK_THROWS_WITH(j_nonarray[0], "[json.exception.type_error.305] cannot use operator[] with a numeric argument with object"); + CHECK_THROWS_WITH(j_nonarray_const[0], "[json.exception.type_error.305] cannot use operator[] with a numeric argument with object"); } SECTION("number (integer)") @@ -242,8 +242,8 @@ TEST_CASE("element access 1") const json j_nonarray_const(j_nonarray); CHECK_THROWS_AS(j_nonarray[0], json::type_error&); CHECK_THROWS_AS(j_nonarray_const[0], json::type_error&); - CHECK_THROWS_WITH(j_nonarray[0], "[json.exception.type_error.305] cannot use operator[] with number"); - CHECK_THROWS_WITH(j_nonarray_const[0], "[json.exception.type_error.305] cannot use operator[] with number"); + CHECK_THROWS_WITH(j_nonarray[0], "[json.exception.type_error.305] cannot use operator[] with a numeric argument with number"); + CHECK_THROWS_WITH(j_nonarray_const[0], "[json.exception.type_error.305] cannot use operator[] with a numeric argument with number"); } SECTION("number (unsigned)") @@ -252,8 +252,8 @@ TEST_CASE("element access 1") const json j_nonarray_const(j_nonarray); CHECK_THROWS_AS(j_nonarray[0], json::type_error&); CHECK_THROWS_AS(j_nonarray_const[0], json::type_error&); - CHECK_THROWS_WITH(j_nonarray[0], "[json.exception.type_error.305] cannot use operator[] with number"); - CHECK_THROWS_WITH(j_nonarray_const[0], "[json.exception.type_error.305] cannot use operator[] with number"); + CHECK_THROWS_WITH(j_nonarray[0], "[json.exception.type_error.305] cannot use operator[] with a numeric argument with number"); + CHECK_THROWS_WITH(j_nonarray_const[0], "[json.exception.type_error.305] cannot use operator[] with a numeric argument with number"); } SECTION("number (floating-point)") @@ -262,8 +262,8 @@ TEST_CASE("element access 1") const json j_nonarray_const(j_nonarray); CHECK_THROWS_AS(j_nonarray[0], json::type_error&); CHECK_THROWS_AS(j_nonarray_const[0], json::type_error&); - CHECK_THROWS_WITH(j_nonarray[0], "[json.exception.type_error.305] cannot use operator[] with number"); - CHECK_THROWS_WITH(j_nonarray_const[0], "[json.exception.type_error.305] cannot use operator[] with number"); + CHECK_THROWS_WITH(j_nonarray[0], "[json.exception.type_error.305] cannot use operator[] with a numeric argument with number"); + CHECK_THROWS_WITH(j_nonarray_const[0], "[json.exception.type_error.305] cannot use operator[] with a numeric argument with number"); } } } diff --git a/test/src/unit-element_access2.cpp b/test/src/unit-element_access2.cpp index 41ec42bef..8707c472a 100644 --- a/test/src/unit-element_access2.cpp +++ b/test/src/unit-element_access2.cpp @@ -475,9 +475,9 @@ TEST_CASE("element access 2") CHECK_NOTHROW(j_nonobject2[json::object_t::key_type("foo")]); CHECK_THROWS_AS(j_const_nonobject["foo"], json::type_error&); CHECK_THROWS_AS(j_const_nonobject[json::object_t::key_type("foo")], json::type_error&); - CHECK_THROWS_WITH(j_const_nonobject["foo"], "[json.exception.type_error.305] cannot use operator[] with null"); + CHECK_THROWS_WITH(j_const_nonobject["foo"], "[json.exception.type_error.305] cannot use operator[] with a key-style argument with null"); CHECK_THROWS_WITH(j_const_nonobject[json::object_t::key_type("foo")], - "[json.exception.type_error.305] cannot use operator[] with null"); + "[json.exception.type_error.305] cannot use operator[] with a key-style argument with null"); } SECTION("boolean") @@ -489,13 +489,13 @@ TEST_CASE("element access 2") CHECK_THROWS_AS(j_const_nonobject["foo"], json::type_error&); CHECK_THROWS_AS(j_const_nonobject[json::object_t::key_type("foo")], json::type_error&); CHECK_THROWS_WITH(j_nonobject["foo"], - "[json.exception.type_error.305] cannot use operator[] with boolean"); + "[json.exception.type_error.305] cannot use operator[] with a key-style argument with boolean"); CHECK_THROWS_WITH(j_nonobject[json::object_t::key_type("foo")], - "[json.exception.type_error.305] cannot use operator[] with boolean"); + "[json.exception.type_error.305] cannot use operator[] with a key-style argument with boolean"); CHECK_THROWS_WITH(j_const_nonobject["foo"], - "[json.exception.type_error.305] cannot use operator[] with boolean"); + "[json.exception.type_error.305] cannot use operator[] with a key-style argument with boolean"); CHECK_THROWS_WITH(j_const_nonobject[json::object_t::key_type("foo")], - "[json.exception.type_error.305] cannot use operator[] with boolean"); + "[json.exception.type_error.305] cannot use operator[] with a key-style argument with boolean"); } SECTION("string") @@ -507,13 +507,13 @@ TEST_CASE("element access 2") CHECK_THROWS_AS(j_const_nonobject["foo"], json::type_error&); CHECK_THROWS_AS(j_const_nonobject[json::object_t::key_type("foo")], json::type_error&); CHECK_THROWS_WITH(j_nonobject["foo"], - "[json.exception.type_error.305] cannot use operator[] with string"); + "[json.exception.type_error.305] cannot use operator[] with a key-style argument with string"); CHECK_THROWS_WITH(j_nonobject[json::object_t::key_type("foo")], - "[json.exception.type_error.305] cannot use operator[] with string"); + "[json.exception.type_error.305] cannot use operator[] with a key-style argument with string"); CHECK_THROWS_WITH(j_const_nonobject["foo"], - "[json.exception.type_error.305] cannot use operator[] with string"); + "[json.exception.type_error.305] cannot use operator[] with a key-style argument with string"); CHECK_THROWS_WITH(j_const_nonobject[json::object_t::key_type("foo")], - "[json.exception.type_error.305] cannot use operator[] with string"); + "[json.exception.type_error.305] cannot use operator[] with a key-style argument with string"); } SECTION("array") @@ -525,12 +525,12 @@ TEST_CASE("element access 2") CHECK_THROWS_AS(j_const_nonobject["foo"], json::type_error&); CHECK_THROWS_AS(j_const_nonobject[json::object_t::key_type("foo")], json::type_error&); CHECK_THROWS_WITH(j_nonobject["foo"], - "[json.exception.type_error.305] cannot use operator[] with array"); - CHECK_THROWS_WITH(j_nonobject[json::object_t::key_type("foo")], "[json.exception.type_error.305] cannot use operator[] with array"); + "[json.exception.type_error.305] cannot use operator[] with a key-style argument with array"); + CHECK_THROWS_WITH(j_nonobject[json::object_t::key_type("foo")], "[json.exception.type_error.305] cannot use operator[] with a key-style argument with array"); CHECK_THROWS_WITH(j_const_nonobject["foo"], - "[json.exception.type_error.305] cannot use operator[] with array"); + "[json.exception.type_error.305] cannot use operator[] with a key-style argument with array"); CHECK_THROWS_WITH(j_const_nonobject[json::object_t::key_type("foo")], - "[json.exception.type_error.305] cannot use operator[] with array"); + "[json.exception.type_error.305] cannot use operator[] with a key-style argument with array"); } SECTION("number (integer)") @@ -542,13 +542,13 @@ TEST_CASE("element access 2") CHECK_THROWS_AS(j_const_nonobject["foo"], json::type_error&); CHECK_THROWS_AS(j_const_nonobject[json::object_t::key_type("foo")], json::type_error&); CHECK_THROWS_WITH(j_nonobject["foo"], - "[json.exception.type_error.305] cannot use operator[] with number"); + "[json.exception.type_error.305] cannot use operator[] with a key-style argument with number"); CHECK_THROWS_WITH(j_nonobject[json::object_t::key_type("foo")], - "[json.exception.type_error.305] cannot use operator[] with number"); + "[json.exception.type_error.305] cannot use operator[] with a key-style argument with number"); CHECK_THROWS_WITH(j_const_nonobject["foo"], - "[json.exception.type_error.305] cannot use operator[] with number"); + "[json.exception.type_error.305] cannot use operator[] with a key-style argument with number"); CHECK_THROWS_WITH(j_const_nonobject[json::object_t::key_type("foo")], - "[json.exception.type_error.305] cannot use operator[] with number"); + "[json.exception.type_error.305] cannot use operator[] with a key-style argument with number"); } SECTION("number (unsigned)") @@ -560,13 +560,13 @@ TEST_CASE("element access 2") CHECK_THROWS_AS(j_const_nonobject["foo"], json::type_error&); CHECK_THROWS_AS(j_const_nonobject[json::object_t::key_type("foo")], json::type_error&); CHECK_THROWS_WITH(j_nonobject["foo"], - "[json.exception.type_error.305] cannot use operator[] with number"); + "[json.exception.type_error.305] cannot use operator[] with a key-style argument with number"); CHECK_THROWS_WITH(j_nonobject[json::object_t::key_type("foo")], - "[json.exception.type_error.305] cannot use operator[] with number"); + "[json.exception.type_error.305] cannot use operator[] with a key-style argument with number"); CHECK_THROWS_WITH(j_const_nonobject["foo"], - "[json.exception.type_error.305] cannot use operator[] with number"); + "[json.exception.type_error.305] cannot use operator[] with a key-style argument with number"); CHECK_THROWS_WITH(j_const_nonobject[json::object_t::key_type("foo")], - "[json.exception.type_error.305] cannot use operator[] with number"); + "[json.exception.type_error.305] cannot use operator[] with a key-style argument with number"); } SECTION("number (floating-point)") @@ -578,13 +578,13 @@ TEST_CASE("element access 2") CHECK_THROWS_AS(j_const_nonobject["foo"], json::type_error&); CHECK_THROWS_AS(j_const_nonobject[json::object_t::key_type("foo")], json::type_error&); CHECK_THROWS_WITH(j_nonobject["foo"], - "[json.exception.type_error.305] cannot use operator[] with number"); + "[json.exception.type_error.305] cannot use operator[] with a key-style argument with number"); CHECK_THROWS_WITH(j_nonobject[json::object_t::key_type("foo")], - "[json.exception.type_error.305] cannot use operator[] with number"); + "[json.exception.type_error.305] cannot use operator[] with a key-style argument with number"); CHECK_THROWS_WITH(j_const_nonobject["foo"], - "[json.exception.type_error.305] cannot use operator[] with number"); + "[json.exception.type_error.305] cannot use operator[] with a key-style argument with number"); CHECK_THROWS_WITH(j_const_nonobject[json::object_t::key_type("foo")], - "[json.exception.type_error.305] cannot use operator[] with number"); + "[json.exception.type_error.305] cannot use operator[] with a key-style argument with number"); } } } From eb30ff0615f626f7c8b98c8d46bdfac8af758949 Mon Sep 17 00:00:00 2001 From: Niels Lohmann Date: Thu, 6 Sep 2018 07:25:19 +0200 Subject: [PATCH 05/50] :rotating_light: fixed a compiler warning #1224 --- Makefile | 5 ++++- include/nlohmann/detail/macro_scope.hpp | 2 -- include/nlohmann/detail/meta/type_traits.hpp | 6 ------ single_include/nlohmann/json.hpp | 8 -------- 4 files changed, 4 insertions(+), 17 deletions(-) diff --git a/Makefile b/Makefile index 1deb8da51..bfb623496 100644 --- a/Makefile +++ b/Makefile @@ -191,7 +191,10 @@ pedantic_gcc: -Wunused-macros \ -Wunused-parameter \ -Wuseless-cast \ - -Wvariadic-macros" + -Wvariadic-macros \ + -Wctor-dtor-privacy \ + -Winit-self \ + -Wstrict-null-sentinel" ########################################################################## # benchmarks diff --git a/include/nlohmann/detail/macro_scope.hpp b/include/nlohmann/detail/macro_scope.hpp index 5f0824fea..834ac2416 100644 --- a/include/nlohmann/detail/macro_scope.hpp +++ b/include/nlohmann/detail/macro_scope.hpp @@ -115,11 +115,9 @@ contains a `mapped_type`, whereas `std::vector` fails the test. */ #define NLOHMANN_JSON_HAS_HELPER(type) \ template struct has_##type { \ - private: \ template \ static int detect(U &&); \ static void detect(...); \ - public: \ static constexpr bool value = \ std::is_integral()))>::value; \ } diff --git a/include/nlohmann/detail/meta/type_traits.hpp b/include/nlohmann/detail/meta/type_traits.hpp index caf812226..d3a6584d4 100644 --- a/include/nlohmann/detail/meta/type_traits.hpp +++ b/include/nlohmann/detail/meta/type_traits.hpp @@ -141,14 +141,12 @@ struct is_compatible_integer_type template struct has_from_json { - private: // also check the return type of from_json template::from_json( std::declval(), std::declval()))>::value>> static int detect(U&&); static void detect(...); - public: static constexpr bool value = std::is_integral>()))>::value; }; @@ -158,7 +156,6 @@ struct has_from_json template struct has_non_default_from_json { - private: template < typename U, typename = enable_if_t>()))>::value; }; @@ -175,13 +171,11 @@ struct has_non_default_from_json template struct has_to_json { - private: template::to_json( std::declval(), std::declval()))> static int detect(U&&); static void detect(...); - public: static constexpr bool value = std::is_integral>()))>::value; }; diff --git a/single_include/nlohmann/json.hpp b/single_include/nlohmann/json.hpp index ac27d3d4c..9984f14eb 100644 --- a/single_include/nlohmann/json.hpp +++ b/single_include/nlohmann/json.hpp @@ -230,11 +230,9 @@ contains a `mapped_type`, whereas `std::vector` fails the test. */ #define NLOHMANN_JSON_HAS_HELPER(type) \ template struct has_##type { \ - private: \ template \ static int detect(U &&); \ static void detect(...); \ - public: \ static constexpr bool value = \ std::is_integral()))>::value; \ } @@ -471,14 +469,12 @@ struct is_compatible_integer_type template struct has_from_json { - private: // also check the return type of from_json template::from_json( std::declval(), std::declval()))>::value>> static int detect(U&&); static void detect(...); - public: static constexpr bool value = std::is_integral>()))>::value; }; @@ -488,7 +484,6 @@ struct has_from_json template struct has_non_default_from_json { - private: template < typename U, typename = enable_if_t>()))>::value; }; @@ -505,13 +499,11 @@ struct has_non_default_from_json template struct has_to_json { - private: template::to_json( std::declval(), std::declval()))> static int detect(U&&); static void detect(...); - public: static constexpr bool value = std::is_integral>()))>::value; }; From 1ea8cd128c11746d9ee18af1dc4b3d3d516b7296 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Th=C3=A9o=20DELRIEU?= Date: Fri, 7 Sep 2018 12:15:35 +0200 Subject: [PATCH 06/50] fix void_t for older compilers --- include/nlohmann/detail/meta/void_t.hpp | 7 +++++-- single_include/nlohmann/json.hpp | 7 +++++-- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/include/nlohmann/detail/meta/void_t.hpp b/include/nlohmann/detail/meta/void_t.hpp index 66c4a359e..2528ea84c 100644 --- a/include/nlohmann/detail/meta/void_t.hpp +++ b/include/nlohmann/detail/meta/void_t.hpp @@ -4,7 +4,10 @@ namespace nlohmann { namespace detail { -template -using void_t = void; +template struct make_void +{ + using type = void; +}; +template using void_t = typename make_void::type; } } diff --git a/single_include/nlohmann/json.hpp b/single_include/nlohmann/json.hpp index 9984f14eb..bb03b522f 100644 --- a/single_include/nlohmann/json.hpp +++ b/single_include/nlohmann/json.hpp @@ -3572,8 +3572,11 @@ namespace nlohmann { namespace detail { -template -using void_t = void; +template struct make_void +{ + using type = void; +}; +template using void_t = typename make_void::type; } } From b59c3367c9cf4f0168ce7b7fc7314de80823f24f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Th=C3=A9o=20DELRIEU?= Date: Wed, 5 Sep 2018 16:02:13 +0200 Subject: [PATCH 07/50] use detected instead of has_* traits --- include/nlohmann/detail/macro_scope.hpp | 19 -- include/nlohmann/detail/macro_unscope.hpp | 1 - include/nlohmann/detail/meta/type_traits.hpp | 77 +++--- single_include/nlohmann/json.hpp | 238 +++++++++---------- 4 files changed, 157 insertions(+), 178 deletions(-) diff --git a/include/nlohmann/detail/macro_scope.hpp b/include/nlohmann/detail/macro_scope.hpp index 834ac2416..80fbece7e 100644 --- a/include/nlohmann/detail/macro_scope.hpp +++ b/include/nlohmann/detail/macro_scope.hpp @@ -102,22 +102,3 @@ basic_json - -/*! -@brief Helper to determine whether there's a key_type for T. - -This helper is used to tell associative containers apart from other containers -such as sequence containers. For instance, `std::map` passes the test as it -contains a `mapped_type`, whereas `std::vector` fails the test. - -@sa http://stackoverflow.com/a/7728728/266378 -@since version 1.0.0, overworked in version 2.0.6 -*/ -#define NLOHMANN_JSON_HAS_HELPER(type) \ - template struct has_##type { \ - template \ - static int detect(U &&); \ - static void detect(...); \ - static constexpr bool value = \ - std::is_integral()))>::value; \ - } diff --git a/include/nlohmann/detail/macro_unscope.hpp b/include/nlohmann/detail/macro_unscope.hpp index 032b1218e..4c5aa915d 100644 --- a/include/nlohmann/detail/macro_unscope.hpp +++ b/include/nlohmann/detail/macro_unscope.hpp @@ -20,4 +20,3 @@ #undef JSON_HAS_CPP_17 #undef NLOHMANN_BASIC_JSON_TPL_DECLARATION #undef NLOHMANN_BASIC_JSON_TPL -#undef NLOHMANN_JSON_HAS_HELPER diff --git a/include/nlohmann/detail/meta/type_traits.hpp b/include/nlohmann/detail/meta/type_traits.hpp index d3a6584d4..63c0db969 100644 --- a/include/nlohmann/detail/meta/type_traits.hpp +++ b/include/nlohmann/detail/meta/type_traits.hpp @@ -7,6 +7,7 @@ #include #include +#include #include namespace nlohmann @@ -30,9 +31,31 @@ template struct is_basic_json : std::false_type {}; NLOHMANN_BASIC_JSON_TPL_DECLARATION struct is_basic_json : std::true_type {}; -//////////////////////// -// has_/is_ functions // -//////////////////////// +////////////////////////// +// aliases for detected // +////////////////////////// + +template +using mapped_type_t = typename T::mapped_type; + +template +using key_type_t = typename T::key_type; + +template +using value_type_t = typename T::value_type; + +template +using iterator_t = typename T::iterator; + +template +using to_json_function = decltype(T::to_json(std::declval()...)); + +template +using from_json_function = decltype(T::from_json(std::declval()...)); + +/////////////////// +// is_ functions // +/////////////////// // source: https://stackoverflow.com/a/37193089/4116453 @@ -42,11 +65,6 @@ struct is_complete_type : std::false_type {}; template struct is_complete_type : std::true_type {}; -NLOHMANN_JSON_HAS_HELPER(mapped_type); -NLOHMANN_JSON_HAS_HELPER(key_type); -NLOHMANN_JSON_HAS_HELPER(value_type); -NLOHMANN_JSON_HAS_HELPER(iterator); - template struct is_compatible_object_type_impl : std::false_type {}; @@ -74,8 +92,8 @@ struct is_compatible_object_type { static auto constexpr value = is_compatible_object_type_impl < conjunction>, - has_mapped_type, - has_key_type>::value, + is_detected, + is_detected>::value, typename BasicJsonType::object_t, CompatibleObjectType >::value; }; @@ -84,7 +102,7 @@ struct is_compatible_string_type { static auto constexpr value = is_compatible_string_type_impl < conjunction>, - has_value_type>::value, + is_detected>::value, typename BasicJsonType::string_t, CompatibleStringType >::value; }; @@ -107,8 +125,8 @@ struct is_compatible_array_type negation>, negation>, - has_value_type, - has_iterator>::value; + is_detected, + is_detected>::value; }; template @@ -141,14 +159,11 @@ struct is_compatible_integer_type template struct has_from_json { - // also check the return type of from_json - template::from_json( - std::declval(), std::declval()))>::value>> - static int detect(U&&); - static void detect(...); + using serializer = typename BasicJsonType::template json_serializer; - static constexpr bool value = std::is_integral>()))>::value; + static constexpr bool value = + is_detected_exact::value; }; // This trait checks if JSONSerializer::from_json(json const&) exists @@ -156,28 +171,22 @@ struct has_from_json template struct has_non_default_from_json { - template < - typename U, - typename = enable_if_t::from_json(std::declval()))>::value >> - static int detect(U&&); - static void detect(...); + using serializer = typename BasicJsonType::template json_serializer; - static constexpr bool value = std::is_integral>()))>::value; + static constexpr bool value = + is_detected_exact::value; }; // This trait checks if BasicJsonType::json_serializer::to_json exists template struct has_to_json { - template::to_json( - std::declval(), std::declval()))> - static int detect(U&&); - static void detect(...); + using serializer = typename BasicJsonType::template json_serializer; - static constexpr bool value = std::is_integral>()))>::value; + static constexpr bool value = + is_detected_exact::value; }; template diff --git a/single_include/nlohmann/json.hpp b/single_include/nlohmann/json.hpp index bb03b522f..a93521280 100644 --- a/single_include/nlohmann/json.hpp +++ b/single_include/nlohmann/json.hpp @@ -218,25 +218,6 @@ using json = basic_json<>; NumberIntegerType, NumberUnsignedType, NumberFloatType, \ AllocatorType, JSONSerializer> -/*! -@brief Helper to determine whether there's a key_type for T. - -This helper is used to tell associative containers apart from other containers -such as sequence containers. For instance, `std::map` passes the test as it -contains a `mapped_type`, whereas `std::vector` fails the test. - -@sa http://stackoverflow.com/a/7728728/266378 -@since version 1.0.0, overworked in version 2.0.6 -*/ -#define NLOHMANN_JSON_HAS_HELPER(type) \ - template struct has_##type { \ - template \ - static int detect(U &&); \ - static void detect(...); \ - static constexpr bool value = \ - std::is_integral()))>::value; \ - } - // #include @@ -334,6 +315,78 @@ constexpr T static_const::value; // #include +// #include + + +#include + +// #include + + +namespace nlohmann +{ +namespace detail +{ +template struct make_void +{ + using type = void; +}; +template using void_t = typename make_void::type; +} +} + + +// http://en.cppreference.com/w/cpp/experimental/is_detected +namespace nlohmann +{ +namespace detail +{ +struct nonesuch +{ + nonesuch() = delete; + ~nonesuch() = delete; + nonesuch(nonesuch const&) = delete; + void operator=(nonesuch const&) = delete; +}; + +template class Op, + class... Args> +struct detector +{ + using value_t = std::false_type; + using type = Default; +}; + +template class Op, class... Args> +struct detector>, Op, Args...> +{ + using value_t = std::true_type; + using type = Op; +}; + +template