Skip to content

Commit

Permalink
Fixed return type of JsonArray::is<T>() and some others (issue #121)
Browse files Browse the repository at this point in the history
  • Loading branch information
bblanchon committed Sep 19, 2015
1 parent 155dd65 commit 7cf6fe6
Show file tree
Hide file tree
Showing 7 changed files with 19 additions and 4 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ v5.0.3
------

* Fixed `printTo(String)` which wrote numbers instead of strings (issue #120)
* Fixed return type of `JsonArray::is<T>()` and some others (issue #121)

v5.0.2
------
Expand Down
2 changes: 1 addition & 1 deletion include/ArduinoJson/JsonArray.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ class JsonArray : public Internals::JsonPrintable<JsonArray>,

// Check the type of the value at specified index.
template <typename T>
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()
Expand Down
2 changes: 1 addition & 1 deletion include/ArduinoJson/JsonArray.ipp
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ inline T JsonArray::get(size_t index) const {
}

template <typename T>
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<T>() : false;
}
Expand Down
2 changes: 1 addition & 1 deletion include/ArduinoJson/JsonArraySubscript.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class JsonArraySubscript : public JsonSubscriptBase<JsonArraySubscript> {
}

template <typename T>
FORCE_INLINE T is() const {
FORCE_INLINE bool is() const {
return _array.is<T>(_index);
}

Expand Down
2 changes: 1 addition & 1 deletion include/ArduinoJson/JsonObjectSubscript.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class JsonObjectSubscript
}

template <typename TValue>
FORCE_INLINE TValue is() const {
FORCE_INLINE bool is() const {
return _object.is<TValue>(_key);
}

Expand Down
2 changes: 2 additions & 0 deletions test/JsonArray_Container_Tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,13 @@ class JsonArray_Container_Tests : public ::testing::Test {
private:
template <typename T>
void itemMustEqual(int index, T expected) {
EXPECT_TRUE(_array[index].is<T>());
EXPECT_EQ(expected, _array[index].as<T>());
}

template <typename T>
void itemMustReference(int index, const T& expected) {
EXPECT_TRUE(_array[index].is<T&>());
EXPECT_EQ(&expected, &_array[index].as<T&>());
}
};
Expand Down
12 changes: 12 additions & 0 deletions test/JsonObject_Container_Tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ TEST_F(JsonObject_Container_Tests, CanStoreIntegers) {
_object["hello"] = 123;
_object.set("world", 456);

EXPECT_TRUE(_object["hello"].is<int>());
EXPECT_FALSE(_object["hello"].is<double>());
EXPECT_EQ(123, _object["hello"].as<int>());
EXPECT_EQ(456, _object["world"].as<int>());
}
Expand All @@ -69,6 +71,8 @@ TEST_F(JsonObject_Container_Tests, CanStoreDoubles) {
_object["hello"] = 123.45;
_object.set("world", 456.78);

EXPECT_TRUE(_object["hello"].is<double>());
EXPECT_FALSE(_object["hello"].is<long>());
EXPECT_EQ(123.45, _object["hello"].as<double>());
EXPECT_EQ(456.78, _object["world"].as<double>());
}
Expand All @@ -77,6 +81,8 @@ TEST_F(JsonObject_Container_Tests, CanStoreBooleans) {
_object["hello"] = true;
_object.set("world", false);

EXPECT_TRUE(_object["hello"].is<bool>());
EXPECT_FALSE(_object["hello"].is<long>());
EXPECT_TRUE(_object["hello"].as<bool>());
EXPECT_FALSE(_object["world"].as<bool>());
}
Expand All @@ -85,6 +91,8 @@ TEST_F(JsonObject_Container_Tests, CanStoreStrings) {
_object["hello"] = "h3110";
_object.set("world", "w0r1d");

EXPECT_TRUE(_object["hello"].is<const char*>());
EXPECT_FALSE(_object["hello"].is<long>());
EXPECT_STREQ("h3110", _object["hello"].as<const char*>());
EXPECT_STREQ("w0r1d", _object["world"].as<const char*>());
}
Expand All @@ -96,6 +104,8 @@ TEST_F(JsonObject_Container_Tests, CanStoreArrays) {
_object["hello"] = array1;
_object.set("world", array2);

EXPECT_TRUE(_object["hello"].is<JsonArray&>());
EXPECT_FALSE(_object["hello"].is<JsonObject&>());
EXPECT_EQ(&array1, &_object["hello"].asArray());
EXPECT_EQ(&array2, &_object["world"].asArray());
}
Expand All @@ -107,6 +117,8 @@ TEST_F(JsonObject_Container_Tests, CanStoreObjects) {
_object["hello"] = object1;
_object.set("world", object2);

EXPECT_TRUE(_object["hello"].is<JsonObject&>());
EXPECT_FALSE(_object["hello"].is<JsonArray&>());
EXPECT_EQ(&object1, &_object["hello"].asObject());
EXPECT_EQ(&object2, &_object["world"].asObject());
}
Expand Down

0 comments on commit 7cf6fe6

Please sign in to comment.