From f2873e6d3a46847126c03ac754dd9993d32a98a5 Mon Sep 17 00:00:00 2001 From: Jared Grubb Date: Sun, 29 Jan 2017 02:46:58 +0000 Subject: [PATCH 1/4] README: adjust boost::optional example The "from_json" example for boost::optional is not complete and should also handle the 'none' case. --- README.md | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 1ee358e5b..6451e2c55 100644 --- a/README.md +++ b/README.md @@ -563,7 +563,9 @@ namespace nlohmann { } static void from_json(const json& j, boost::optional& opt) { - if (!j.is_null()) { + if (j.is_null()) { + opt = boost::none; + } else { opt = j.get(); // same as above, but with // adl_serializer::from_json } From 0164b10e102f2f8694d380554389a4a4a917f781 Mon Sep 17 00:00:00 2001 From: Niels Lohmann Date: Sun, 29 Jan 2017 17:18:56 +0100 Subject: [PATCH 2/4] :white_check_mark: added missing tests found by mutate++ --- test/src/unit-class_parser.cpp | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/test/src/unit-class_parser.cpp b/test/src/unit-class_parser.cpp index 3dfad5bd3..59ea3cefb 100644 --- a/test/src/unit-class_parser.cpp +++ b/test/src/unit-class_parser.cpp @@ -596,6 +596,32 @@ TEST_CASE("parser class") "missing or wrong low surrogate"); } + SECTION("tests found by mutate++") + { + // test case to make sure no comma preceeds the first key + CHECK_THROWS_AS(json::parser("{,\"key\": false}").parse(), std::invalid_argument); + // test case to make sure an object is properly closed + CHECK_THROWS_AS(json::parser("[{\"key\": false true]").parse(), std::invalid_argument); + + // test case to make sure the callback is properly evaluated after reading a key + { + json::parser_callback_t cb = [](int depth, json::parse_event_t event, json & parsed) + { + if (event == json::parse_event_t::key) + { + return false; + } + else + { + return true; + } + }; + + json x = json::parse("{\"key\": false}", cb); + CHECK(x == json::object()); + } + } + SECTION("callback function") { auto s_object = R"( From b210f2dbf41fc207a91e57359c8b2cffdba64ebf Mon Sep 17 00:00:00 2001 From: Niels Lohmann Date: Sun, 29 Jan 2017 21:40:37 +0100 Subject: [PATCH 3/4] :lipstick: minor refactoring --- src/json.hpp | 6 ++---- src/json.hpp.re2c | 6 ++---- 2 files changed, 4 insertions(+), 8 deletions(-) diff --git a/src/json.hpp b/src/json.hpp index 5fdd83d6e..d7af3a707 100644 --- a/src/json.hpp +++ b/src/json.hpp @@ -3036,10 +3036,8 @@ class basic_json { return m_value.boolean; } - else - { - JSON_THROW(std::domain_error("type must be boolean, but is " + type_name())); - } + + JSON_THROW(std::domain_error("type must be boolean, but is " + type_name())); } /// get a pointer to the value (object) diff --git a/src/json.hpp.re2c b/src/json.hpp.re2c index 068b87630..cc11457f4 100644 --- a/src/json.hpp.re2c +++ b/src/json.hpp.re2c @@ -3036,10 +3036,8 @@ class basic_json { return m_value.boolean; } - else - { - JSON_THROW(std::domain_error("type must be boolean, but is " + type_name())); - } + + JSON_THROW(std::domain_error("type must be boolean, but is " + type_name())); } /// get a pointer to the value (object) From c75865d88c0176b85f3e8d073a784602323b2160 Mon Sep 17 00:00:00 2001 From: Niels Lohmann Date: Wed, 1 Feb 2017 15:13:21 +0100 Subject: [PATCH 4/4] :bug: fixed `-Weffc++` warnings --- src/json.hpp | 10 ++++++---- src/json.hpp.re2c | 10 ++++++---- 2 files changed, 12 insertions(+), 8 deletions(-) diff --git a/src/json.hpp b/src/json.hpp index d7af3a707..c77945a87 100644 --- a/src/json.hpp +++ b/src/json.hpp @@ -8532,10 +8532,11 @@ class basic_json return *this; } - primitive_iterator_t& operator++(int) + primitive_iterator_t operator++(int) { + auto result = *this; m_it++; - return *this; + return result; } primitive_iterator_t& operator--() @@ -8544,10 +8545,11 @@ class basic_json return *this; } - primitive_iterator_t& operator--(int) + primitive_iterator_t operator--(int) { + auto result = *this; m_it--; - return *this; + return result; } primitive_iterator_t& operator+=(difference_type n) diff --git a/src/json.hpp.re2c b/src/json.hpp.re2c index cc11457f4..109a4f262 100644 --- a/src/json.hpp.re2c +++ b/src/json.hpp.re2c @@ -8532,10 +8532,11 @@ class basic_json return *this; } - primitive_iterator_t& operator++(int) + primitive_iterator_t operator++(int) { + auto result = *this; m_it++; - return *this; + return result; } primitive_iterator_t& operator--() @@ -8544,10 +8545,11 @@ class basic_json return *this; } - primitive_iterator_t& operator--(int) + primitive_iterator_t operator--(int) { + auto result = *this; m_it--; - return *this; + return result; } primitive_iterator_t& operator+=(difference_type n)