From 7cf6fe6d62a98f2aacfc4e6590be2330a1feb656 Mon Sep 17 00:00:00 2001 From: Benoit Blanchon Date: Sat, 19 Sep 2015 16:25:18 +0200 Subject: [PATCH] Fixed return type of `JsonArray::is()` and some others (issue #121) --- CHANGELOG.md | 1 + include/ArduinoJson/JsonArray.hpp | 2 +- include/ArduinoJson/JsonArray.ipp | 2 +- include/ArduinoJson/JsonArraySubscript.hpp | 2 +- include/ArduinoJson/JsonObjectSubscript.hpp | 2 +- test/JsonArray_Container_Tests.cpp | 2 ++ test/JsonObject_Container_Tests.cpp | 12 ++++++++++++ 7 files changed, 19 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index ea513b2a..eb7800ae 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,7 @@ v5.0.3 ------ * Fixed `printTo(String)` which wrote numbers instead of strings (issue #120) +* Fixed return type of `JsonArray::is()` and some others (issue #121) v5.0.2 ------ diff --git a/include/ArduinoJson/JsonArray.hpp b/include/ArduinoJson/JsonArray.hpp index 4573ded3..c76c8a45 100644 --- a/include/ArduinoJson/JsonArray.hpp +++ b/include/ArduinoJson/JsonArray.hpp @@ -92,7 +92,7 @@ class JsonArray : public Internals::JsonPrintable, // Check the type of the value at specified index. template - FORCE_INLINE T is(size_t index) const; + FORCE_INLINE bool is(size_t index) const; // Creates a JsonArray and adds a reference at the end of the array. // It's a shortcut for JsonBuffer::createArray() and JsonArray::add() diff --git a/include/ArduinoJson/JsonArray.ipp b/include/ArduinoJson/JsonArray.ipp index 99185281..8c551254 100644 --- a/include/ArduinoJson/JsonArray.ipp +++ b/include/ArduinoJson/JsonArray.ipp @@ -182,7 +182,7 @@ inline T JsonArray::get(size_t index) const { } template -inline T JsonArray::is(size_t index) const { +inline bool JsonArray::is(size_t index) const { node_type *node = getNodeAt(index); return node ? node->content.is() : false; } diff --git a/include/ArduinoJson/JsonArraySubscript.hpp b/include/ArduinoJson/JsonArraySubscript.hpp index dabafeab..90d85aed 100644 --- a/include/ArduinoJson/JsonArraySubscript.hpp +++ b/include/ArduinoJson/JsonArraySubscript.hpp @@ -26,7 +26,7 @@ class JsonArraySubscript : public JsonSubscriptBase { } template - FORCE_INLINE T is() const { + FORCE_INLINE bool is() const { return _array.is(_index); } diff --git a/include/ArduinoJson/JsonObjectSubscript.hpp b/include/ArduinoJson/JsonObjectSubscript.hpp index 65435e84..39afade6 100644 --- a/include/ArduinoJson/JsonObjectSubscript.hpp +++ b/include/ArduinoJson/JsonObjectSubscript.hpp @@ -29,7 +29,7 @@ class JsonObjectSubscript } template - FORCE_INLINE TValue is() const { + FORCE_INLINE bool is() const { return _object.is(_key); } diff --git a/test/JsonArray_Container_Tests.cpp b/test/JsonArray_Container_Tests.cpp index a8c7558d..00a2bb82 100644 --- a/test/JsonArray_Container_Tests.cpp +++ b/test/JsonArray_Container_Tests.cpp @@ -39,11 +39,13 @@ class JsonArray_Container_Tests : public ::testing::Test { private: template void itemMustEqual(int index, T expected) { + EXPECT_TRUE(_array[index].is()); EXPECT_EQ(expected, _array[index].as()); } template void itemMustReference(int index, const T& expected) { + EXPECT_TRUE(_array[index].is()); EXPECT_EQ(&expected, &_array[index].as()); } }; diff --git a/test/JsonObject_Container_Tests.cpp b/test/JsonObject_Container_Tests.cpp index dc183749..2de547d0 100644 --- a/test/JsonObject_Container_Tests.cpp +++ b/test/JsonObject_Container_Tests.cpp @@ -61,6 +61,8 @@ TEST_F(JsonObject_Container_Tests, CanStoreIntegers) { _object["hello"] = 123; _object.set("world", 456); + EXPECT_TRUE(_object["hello"].is()); + EXPECT_FALSE(_object["hello"].is()); EXPECT_EQ(123, _object["hello"].as()); EXPECT_EQ(456, _object["world"].as()); } @@ -69,6 +71,8 @@ TEST_F(JsonObject_Container_Tests, CanStoreDoubles) { _object["hello"] = 123.45; _object.set("world", 456.78); + EXPECT_TRUE(_object["hello"].is()); + EXPECT_FALSE(_object["hello"].is()); EXPECT_EQ(123.45, _object["hello"].as()); EXPECT_EQ(456.78, _object["world"].as()); } @@ -77,6 +81,8 @@ TEST_F(JsonObject_Container_Tests, CanStoreBooleans) { _object["hello"] = true; _object.set("world", false); + EXPECT_TRUE(_object["hello"].is()); + EXPECT_FALSE(_object["hello"].is()); EXPECT_TRUE(_object["hello"].as()); EXPECT_FALSE(_object["world"].as()); } @@ -85,6 +91,8 @@ TEST_F(JsonObject_Container_Tests, CanStoreStrings) { _object["hello"] = "h3110"; _object.set("world", "w0r1d"); + EXPECT_TRUE(_object["hello"].is()); + EXPECT_FALSE(_object["hello"].is()); EXPECT_STREQ("h3110", _object["hello"].as()); EXPECT_STREQ("w0r1d", _object["world"].as()); } @@ -96,6 +104,8 @@ TEST_F(JsonObject_Container_Tests, CanStoreArrays) { _object["hello"] = array1; _object.set("world", array2); + EXPECT_TRUE(_object["hello"].is()); + EXPECT_FALSE(_object["hello"].is()); EXPECT_EQ(&array1, &_object["hello"].asArray()); EXPECT_EQ(&array2, &_object["world"].asArray()); } @@ -107,6 +117,8 @@ TEST_F(JsonObject_Container_Tests, CanStoreObjects) { _object["hello"] = object1; _object.set("world", object2); + EXPECT_TRUE(_object["hello"].is()); + EXPECT_FALSE(_object["hello"].is()); EXPECT_EQ(&object1, &_object["hello"].asObject()); EXPECT_EQ(&object2, &_object["world"].asObject()); }