From 0eff5679109b3dc38c8a63868ee355e959724d42 Mon Sep 17 00:00:00 2001 From: Benoit Blanchon Date: Sat, 2 May 2015 15:16:18 +0200 Subject: [PATCH] Added JsonArray::removeAt() (issue #58) --- CHANGELOG.md | 1 + include/ArduinoJson/JsonArray.hpp | 5 +++ src/JsonArray.cpp | 11 +++++-- test/JsonArray_Container_Tests.cpp | 52 ++++++++++++++++++++++++++---- 4 files changed, 60 insertions(+), 9 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index cadc4fc2..c166e6d7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,7 @@ Arduino JSON: change log HEAD ---- +* Added `JsonArray::removeAt()` to remove an element of an array (issue #58) * Fixed stack-overflow in `DynamicJsonBuffer` when parsing huge JSON files (issue #65) * Fixed wrong return value of `parseArray()` and `parseObject()` when allocation fails (issue #68) diff --git a/include/ArduinoJson/JsonArray.hpp b/include/ArduinoJson/JsonArray.hpp index 6dcc8b1a..3a54518d 100644 --- a/include/ArduinoJson/JsonArray.hpp +++ b/include/ArduinoJson/JsonArray.hpp @@ -71,6 +71,9 @@ class JsonArray : public Internals::JsonPrintable, // It's a shortcut for JsonBuffer::createObject() and JsonArray::add() JsonObject &createNestedObject(); + // Removes element at specified index. + void removeAt(int index); + // Returns a reference an invalid JsonArray. // This object is meant to replace a NULL pointer. // This is used when memory allocation or JSON parsing fail. @@ -84,6 +87,8 @@ class JsonArray : public Internals::JsonPrintable, explicit JsonArray(JsonBuffer *buffer) : Internals::List(buffer) {} + node_type *getNodeAt(int index) const; + // The instance returned by JsonArray::invalid() static JsonArray _invalid; }; diff --git a/src/JsonArray.cpp b/src/JsonArray.cpp index bd4ff33f..65d3e126 100644 --- a/src/JsonArray.cpp +++ b/src/JsonArray.cpp @@ -15,8 +15,7 @@ using namespace ArduinoJson::Internals; JsonArray JsonArray::_invalid(NULL); JsonVariant &JsonArray::at(int index) const { - node_type *node = _firstNode; - while (node && index--) node = node->next; + node_type *node = getNodeAt(index); return node ? node->content : JsonVariant::invalid(); } @@ -43,6 +42,14 @@ JsonObject &JsonArray::createNestedObject() { return object; } +JsonArray::node_type *JsonArray::getNodeAt(int index) const { + node_type *node = _firstNode; + while (node && index--) node = node->next; + return node; +} + +void JsonArray::removeAt(int index) { removeNode(getNodeAt(index)); } + void JsonArray::writeTo(JsonWriter &writer) const { writer.beginArray(); diff --git a/test/JsonArray_Container_Tests.cpp b/test/JsonArray_Container_Tests.cpp index 283e453d..7b3a7fa1 100644 --- a/test/JsonArray_Container_Tests.cpp +++ b/test/JsonArray_Container_Tests.cpp @@ -48,6 +48,11 @@ class JsonArray_Container_Tests : public ::testing::Test { } }; +template <> +void JsonArray_Container_Tests::itemMustEqual(int index, const char* expected) { + EXPECT_STREQ(expected, _array[index].asString()); +} + TEST_F(JsonArray_Container_Tests, SuccessIsTrue) { EXPECT_TRUE(_array.success()); } @@ -87,14 +92,11 @@ TEST_F(JsonArray_Container_Tests, CanStoreBooleans) { } TEST_F(JsonArray_Container_Tests, CanStoreStrings) { - const char* firstString = "h3110"; - const char* secondString = "w0r1d"; + _array.add("hello"); + _array.add("world"); - _array.add(firstString); - _array.add(secondString); - - firstMustEqual(firstString); - secondMustEqual(secondString); + firstMustEqual("hello"); + secondMustEqual("world"); } TEST_F(JsonArray_Container_Tests, CanStoreNestedArrays) { @@ -134,3 +136,39 @@ TEST_F(JsonArray_Container_Tests, CanCreateNestedObjects) { firstMustReference(innerObject1); secondMustReference(innerObject2); } + +TEST_F(JsonArray_Container_Tests, RemoveFirstElement) { + _array.add("one"); + _array.add("two"); + _array.add("three"); + + _array.removeAt(0); + + sizeMustBe(2); + firstMustEqual("two"); + secondMustEqual("three"); +} + +TEST_F(JsonArray_Container_Tests, RemoveMiddleElement) { + _array.add("one"); + _array.add("two"); + _array.add("three"); + + _array.removeAt(1); + + sizeMustBe(2); + firstMustEqual("one"); + secondMustEqual("three"); +} + +TEST_F(JsonArray_Container_Tests, RemoveLastElement) { + _array.add("one"); + _array.add("two"); + _array.add("three"); + + _array.removeAt(2); + + sizeMustBe(2); + firstMustEqual("one"); + secondMustEqual("two"); +}