Kaynağa Gözat

Added JsonArray::removeAt() (issue #58)

Benoit Blanchon 10 yıl önce
ebeveyn
işleme
0eff567910

+ 1 - 0
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)
 

+ 5 - 0
include/ArduinoJson/JsonArray.hpp

@@ -71,6 +71,9 @@ class JsonArray : public Internals::JsonPrintable<JsonArray>,
   // 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<JsonArray>,
   explicit JsonArray(JsonBuffer *buffer)
       : Internals::List<JsonVariant>(buffer) {}
 
+  node_type *getNodeAt(int index) const;
+
   // The instance returned by JsonArray::invalid()
   static JsonArray _invalid;
 };

+ 9 - 2
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();
 

+ 45 - 7
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(firstString);
-  _array.add(secondString);
+  _array.add("hello");
+  _array.add("world");
 
-  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");
+}