From d300a8e2681c95fd0b11569a74d4f88d608a3f69 Mon Sep 17 00:00:00 2001 From: Niels Lohmann Date: Fri, 13 Oct 2017 08:46:29 +0200 Subject: [PATCH 01/10] :rotating_light: fixed warnings #776 --- src/json.hpp | 36 ++++++++++++++++++------------------ 1 file changed, 18 insertions(+), 18 deletions(-) diff --git a/src/json.hpp b/src/json.hpp index 2477382377..3f0b6e3e9f 100644 --- a/src/json.hpp +++ b/src/json.hpp @@ -206,9 +206,9 @@ class exception : public std::exception protected: exception(int id_, const char* what_arg) : id(id_), m(what_arg) {} - static std::string name(const std::string& ename, int id) + static std::string name(const std::string& ename, int id_) { - return "[json.exception." + ename + "." + std::to_string(id) + "] "; + return "[json.exception." + ename + "." + std::to_string(id_) + "] "; } private: @@ -264,18 +264,18 @@ class parse_error : public exception public: /*! @brief create a parse error exception - @param[in] id the id of the exception + @param[in] id_ the id of the exception @param[in] byte_ the byte index where the error occurred (or 0 if the position cannot be determined) @param[in] what_arg the explanatory string @return parse_error object */ - static parse_error create(int id, std::size_t byte_, const std::string& what_arg) + static parse_error create(int id_, std::size_t byte_, const std::string& what_arg) { - std::string w = exception::name("parse_error", id) + "parse error" + + std::string w = exception::name("parse_error", id_) + "parse error" + (byte_ != 0 ? (" at " + std::to_string(byte_)) : "") + ": " + what_arg; - return parse_error(id, byte_, w.c_str()); + return parse_error(id_, byte_, w.c_str()); } /*! @@ -334,10 +334,10 @@ caught.,invalid_iterator} class invalid_iterator : public exception { public: - static invalid_iterator create(int id, const std::string& what_arg) + static invalid_iterator create(int id_, const std::string& what_arg) { - std::string w = exception::name("invalid_iterator", id) + what_arg; - return invalid_iterator(id, w.c_str()); + std::string w = exception::name("invalid_iterator", id_) + what_arg; + return invalid_iterator(id_, w.c_str()); } private: @@ -385,10 +385,10 @@ caught.,type_error} class type_error : public exception { public: - static type_error create(int id, const std::string& what_arg) + static type_error create(int id_, const std::string& what_arg) { - std::string w = exception::name("type_error", id) + what_arg; - return type_error(id, w.c_str()); + std::string w = exception::name("type_error", id_) + what_arg; + return type_error(id_, w.c_str()); } private: @@ -428,10 +428,10 @@ caught.,out_of_range} class out_of_range : public exception { public: - static out_of_range create(int id, const std::string& what_arg) + static out_of_range create(int id_, const std::string& what_arg) { - std::string w = exception::name("out_of_range", id) + what_arg; - return out_of_range(id, w.c_str()); + std::string w = exception::name("out_of_range", id_) + what_arg; + return out_of_range(id_, w.c_str()); } private: @@ -466,10 +466,10 @@ caught.,other_error} class other_error : public exception { public: - static other_error create(int id, const std::string& what_arg) + static other_error create(int id_, const std::string& what_arg) { - std::string w = exception::name("other_error", id) + what_arg; - return other_error(id, w.c_str()); + std::string w = exception::name("other_error", id_) + what_arg; + return other_error(id_, w.c_str()); } private: From 727ee7d03e40c81d6c38cdbdd1a562718fe92f7d Mon Sep 17 00:00:00 2001 From: Nate Vargas <22281318+eld00d@users.noreply.github.com> Date: Sat, 14 Oct 2017 19:06:38 -0500 Subject: [PATCH 02/10] Set GENERATE_TAGFILE in Doxyfile Allows documentation to be linked from other projects to https://nlohmann.github.io/json using Doxygen with the tag: TAGFILES = $(SOME_PATH)/nlohmann_json.tag=https://nlohmann.github.io/json --- doc/Doxyfile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/Doxyfile b/doc/Doxyfile index 8dfc0dcd10..757a0103f6 100644 --- a/doc/Doxyfile +++ b/doc/Doxyfile @@ -281,7 +281,7 @@ SKIP_FUNCTION_MACROS = YES # Configuration options related to external references #--------------------------------------------------------------------------- TAGFILES = -GENERATE_TAGFILE = +GENERATE_TAGFILE = html/nlohmann_json.tag ALLEXTERNALS = NO EXTERNAL_GROUPS = YES EXTERNAL_PAGES = YES From 5ec44fe9e3fce9f8a0a9d6c25b63e3d2b4c9c122 Mon Sep 17 00:00:00 2001 From: Jamie Seward Date: Sun, 15 Oct 2017 22:56:38 -0700 Subject: [PATCH 03/10] Add /W4 for MSVS --- test/CMakeLists.txt | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 4ccbaf06d1..26b474c036 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -91,6 +91,16 @@ foreach(file ${files}) set_target_properties(${testcase} PROPERTIES COMPILE_FLAGS "${CMAKE_CXX_FLAGS} -Wno-deprecated -Wno-float-equal") endif() + # https://stackoverflow.com/questions/2368811/how-to-set-warning-level-in-cmake + if(MSVC) + # Force to always compile with W4 + if(CMAKE_CXX_FLAGS MATCHES "/W[0-4]") + string(REGEX REPLACE "/W[0-4]" "/W4" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}") + else() + set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /W4") + endif() + endif() + add_test(NAME "${testcase}_default" COMMAND ${testcase} ${CATCH_TEST_FILTER} WORKING_DIRECTORY ${CMAKE_SOURCE_DIR} From 19f8f1c075e2d90d6a153b8876c58b645d51c67c Mon Sep 17 00:00:00 2001 From: Jamie Seward Date: Mon, 16 Oct 2017 00:21:38 -0700 Subject: [PATCH 04/10] Add missing "u8" This causes test-udt to crash due to bad iterator --- test/src/unit-udt.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/src/unit-udt.cpp b/test/src/unit-udt.cpp index 6aa469fe4e..409cac8397 100644 --- a/test/src/unit-udt.cpp +++ b/test/src/unit-udt.cpp @@ -201,7 +201,7 @@ void from_json(const BasicJsonType& j, country& c) { {u8"中华人民共和国", country::china}, {"France", country::france}, - {"Российская Федерация", country::russia} + {u8"Российская Федерация", country::russia} }; const auto it = m.find(str); From 8a4af820c767bda5aae714c1ff1f86e55bff1a20 Mon Sep 17 00:00:00 2001 From: Jamie Seward Date: Mon, 16 Oct 2017 00:41:58 -0700 Subject: [PATCH 05/10] Fix warning C4706 --- src/json.hpp | 36 ++++++++++++++++++++++++++---------- 1 file changed, 26 insertions(+), 10 deletions(-) diff --git a/src/json.hpp b/src/json.hpp index 3f0b6e3e9f..d4dfd53824 100644 --- a/src/json.hpp +++ b/src/json.hpp @@ -3034,12 +3034,20 @@ class parser { case token_type::begin_object: { - if (keep and (not callback or ((keep = callback(depth++, parse_event_t::object_start, result))))) - { - // explicitly set result to object to cope with {} - result.m_type = value_t::object; - result.m_value = value_t::object; - } + if (keep) + { + if (callback) + { + keep = callback(depth++, parse_event_t::object_start, result); + } + + if (not callback or keep) + { + // explicitly set result to object to cope with {} + result.m_type = value_t::object; + result.m_value = value_t::object; + } + } // read next token get_token(); @@ -3130,11 +3138,19 @@ class parser case token_type::begin_array: { - if (keep and (not callback or ((keep = callback(depth++, parse_event_t::array_start, result))))) + if (keep) { - // explicitly set result to object to cope with [] - result.m_type = value_t::array; - result.m_value = value_t::array; + if (callback) + { + keep = callback(depth++, parse_event_t::array_start, result); + } + + if (not callback or keep) + { + // explicitly set result to object to cope with [] + result.m_type = value_t::array; + result.m_value = value_t::array; + } } // read next token From 8ba7f69ab48eccb067a865612aee7a508009b39f Mon Sep 17 00:00:00 2001 From: Jamie Seward Date: Mon, 16 Oct 2017 00:49:59 -0700 Subject: [PATCH 06/10] Fix whitespace --- src/json.hpp | 48 ++++++++++++++++++++++++------------------------ 1 file changed, 24 insertions(+), 24 deletions(-) diff --git a/src/json.hpp b/src/json.hpp index d4dfd53824..ef55e2ead9 100644 --- a/src/json.hpp +++ b/src/json.hpp @@ -3035,19 +3035,19 @@ class parser case token_type::begin_object: { if (keep) - { - if (callback) - { - keep = callback(depth++, parse_event_t::object_start, result); - } - - if (not callback or keep) - { - // explicitly set result to object to cope with {} - result.m_type = value_t::object; - result.m_value = value_t::object; - } - } + { + if (callback) + { + keep = callback(depth++, parse_event_t::object_start, result); + } + + if (not callback or keep) + { + // explicitly set result to object to cope with {} + result.m_type = value_t::object; + result.m_value = value_t::object; + } + } // read next token get_token(); @@ -3140,17 +3140,17 @@ class parser { if (keep) { - if (callback) - { - keep = callback(depth++, parse_event_t::array_start, result); - } - - if (not callback or keep) - { - // explicitly set result to object to cope with [] - result.m_type = value_t::array; - result.m_value = value_t::array; - } + if (callback) + { + keep = callback(depth++, parse_event_t::array_start, result); + } + + if (not callback or keep) + { + // explicitly set result to array to cope with [] + result.m_type = value_t::array; + result.m_value = value_t::array; + } } // read next token From af99090742fdb3e7de5572aa2006be228f2311cc Mon Sep 17 00:00:00 2001 From: Jamie Seward Date: Mon, 16 Oct 2017 01:02:48 -0700 Subject: [PATCH 07/10] Disable warning C4389: '==': signed/unsigned mismatch Lots of tests have this warning. Also moved out of for loop, doesn't need to be done every loop. --- test/CMakeLists.txt | 23 +++++++++++++---------- 1 file changed, 13 insertions(+), 10 deletions(-) diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 26b474c036..b51aff76de 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -66,6 +66,19 @@ set_target_properties(catch_main PROPERTIES ) target_include_directories(catch_main PRIVATE "thirdparty/catch") +# https://stackoverflow.com/questions/2368811/how-to-set-warning-level-in-cmake +if(MSVC) + # Force to always compile with W4 + if(CMAKE_CXX_FLAGS MATCHES "/W[0-4]") + string(REGEX REPLACE "/W[0-4]" "/W4" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}") + else() + set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /W4") + endif() + + # Disable warning C4389: '==': signed/unsigned mismatch + set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /wd4389") +endif() + ############################################################################# # one executable for each unit test file ############################################################################# @@ -91,16 +104,6 @@ foreach(file ${files}) set_target_properties(${testcase} PROPERTIES COMPILE_FLAGS "${CMAKE_CXX_FLAGS} -Wno-deprecated -Wno-float-equal") endif() - # https://stackoverflow.com/questions/2368811/how-to-set-warning-level-in-cmake - if(MSVC) - # Force to always compile with W4 - if(CMAKE_CXX_FLAGS MATCHES "/W[0-4]") - string(REGEX REPLACE "/W[0-4]" "/W4" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}") - else() - set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /W4") - endif() - endif() - add_test(NAME "${testcase}_default" COMMAND ${testcase} ${CATCH_TEST_FILTER} WORKING_DIRECTORY ${CMAKE_SOURCE_DIR} From 917d9d8bc361e9c8015a5b532ff6c453e4991c1a Mon Sep 17 00:00:00 2001 From: Jamie Seward Date: Mon, 16 Oct 2017 23:23:55 -0700 Subject: [PATCH 08/10] Fix Visual Studio 2017 warnings --- src/json.hpp | 2 +- test/CMakeLists.txt | 7 +++++-- test/src/unit-constructor1.cpp | 2 +- test/src/unit-readme.cpp | 9 +++++++++ 4 files changed, 16 insertions(+), 4 deletions(-) diff --git a/src/json.hpp b/src/json.hpp index ef55e2ead9..ade7366278 100644 --- a/src/json.hpp +++ b/src/json.hpp @@ -5292,7 +5292,7 @@ class binary_reader { get(); check_eof(); - return current; + return static_cast(current); }); return result; } diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index b51aff76de..0a4680ea68 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -75,8 +75,11 @@ if(MSVC) set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /W4") endif() - # Disable warning C4389: '==': signed/unsigned mismatch - set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /wd4389") + # warning C4389: '==': signed/unsigned mismatch + # warning C4309: 'static_cast': truncation of constant value + # warning C4566: character represented by universal-character-name '\uFF01' cannot be represented in the current code page (1252) + # warning C4996: 'nlohmann::basic_json::operator <<': was declared deprecated + set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /wd4389 /wd4309 /wd4566 /wd4996") endif() ############################################################################# diff --git a/test/src/unit-constructor1.cpp b/test/src/unit-constructor1.cpp index ec096dcd45..ed2c80078d 100644 --- a/test/src/unit-constructor1.cpp +++ b/test/src/unit-constructor1.cpp @@ -242,7 +242,7 @@ TEST_CASE("constructors") SECTION("std::pair") { - std::pair p{1.0, "string"}; + std::pair p{1.0f, "string"}; json j(p); CHECK(j.type() == json::value_t::array); diff --git a/test/src/unit-readme.cpp b/test/src/unit-readme.cpp index e921c4b687..576f5834b4 100644 --- a/test/src/unit-readme.cpp +++ b/test/src/unit-readme.cpp @@ -38,6 +38,11 @@ using nlohmann::json; #include #include +#if defined(_MSC_VER) +#pragma warning (push) +#pragma warning (disable : 4189) // local variable is initialized but not referenced +#endif + TEST_CASE("README", "[hide]") { { @@ -298,3 +303,7 @@ TEST_CASE("README", "[hide]") std::cout.rdbuf(old_cout_buffer); } } + +#if defined(_MSC_VER) +#pragma warning (pop) +#endif \ No newline at end of file From a99fcb4e7d53575d2fe2795c1a968fabb1e764ef Mon Sep 17 00:00:00 2001 From: Jamie Seward Date: Tue, 17 Oct 2017 22:53:35 -0700 Subject: [PATCH 09/10] Add comments and newline --- test/CMakeLists.txt | 8 ++++---- test/src/unit-readme.cpp | 2 +- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 0a4680ea68..c72867839f 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -75,10 +75,10 @@ if(MSVC) set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /W4") endif() - # warning C4389: '==': signed/unsigned mismatch - # warning C4309: 'static_cast': truncation of constant value - # warning C4566: character represented by universal-character-name '\uFF01' cannot be represented in the current code page (1252) - # warning C4996: 'nlohmann::basic_json::operator <<': was declared deprecated + # Disable warning C4389: '==': signed/unsigned mismatch + # Disable warning C4309: 'static_cast': truncation of constant value + # Disable warning C4566: character represented by universal-character-name '\uFF01' cannot be represented in the current code page (1252) + # Disable warning C4996: 'nlohmann::basic_json::operator <<': was declared deprecated set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /wd4389 /wd4309 /wd4566 /wd4996") endif() diff --git a/test/src/unit-readme.cpp b/test/src/unit-readme.cpp index 576f5834b4..5a442c601e 100644 --- a/test/src/unit-readme.cpp +++ b/test/src/unit-readme.cpp @@ -306,4 +306,4 @@ TEST_CASE("README", "[hide]") #if defined(_MSC_VER) #pragma warning (pop) -#endif \ No newline at end of file +#endif From 92da334862df7fc6841689685e44852317e9a8c6 Mon Sep 17 00:00:00 2001 From: Sonu Lohani Date: Sat, 21 Oct 2017 12:52:44 +0530 Subject: [PATCH 10/10] Error : 'identifier "size_t" is undefined' in linux --- src/json.hpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/json.hpp b/src/json.hpp index ade7366278..589f04470c 100644 --- a/src/json.hpp +++ b/src/json.hpp @@ -1544,7 +1544,7 @@ class input_buffer_adapter : public input_adapter_protocol std::string read(std::size_t offset, std::size_t length) override { // avoid reading too many characters - const auto max_length = static_cast(limit - start); + const auto max_length = static_cast(limit - start); return std::string(start + offset, (std::min)(length, max_length - offset)); }