10000 Added JsonArray::removeAt() (issue #58) · littlestrange/ArduinoJson@0eff567 · GitHub
[go: up one dir, main page]

Skip to content

Commit 0eff567

Browse files
committed
Added JsonArray::removeAt() (issue bblanchon#58)
1 parent 94d38c0 commit 0eff567

File tree

4 files changed

+60
-9
lines changed

4 files changed

+60
-9
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ Arduino JSON: change log
44
HEAD
55
----
66

7+
* Added `JsonArray::removeAt()` to remove an element of an array (issue #58)
78
* Fixed stack-overflow in `DynamicJsonBuffer` when parsing huge JSON files (issue #65)
89
* Fixed wrong return value of `parseArray()` and `parseObject()` when allocation fails (issue #68)
910

include/ArduinoJson/JsonArray.hpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,9 @@ class JsonArray : public Internals::JsonPrintable<JsonArray>,
7171
// It's a shortcut for JsonBuffer::createObject() and JsonArray::add()
7272
JsonObject &createNestedObject();
7373

74+
// Removes element at specified index.
75+
void removeAt(int index);
76+
7477
// Returns a reference an invalid JsonArray.
7578
// This object is meant to replace a NULL pointer.
7679
// This is used when memory allocation or JSON parsing fail.
@@ -84,6 +87,8 @@ class JsonArray : public Internals::JsonPrintable<JsonArray>,
8487
explicit JsonArray(JsonBuffer *buffer)
8588
: Internals::List<JsonVariant>(buffer) {}
8689

90+
node_type *getNodeAt(int index) const;
91+
8792
// The instance returned by JsonArray::invalid()
8893
static JsonArray _invalid;
8994
};

src/JsonArray.cpp

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,7 @@ using namespace ArduinoJson::Internals;
1515
JsonArray JsonArray::_invalid(NULL);
1616

1717
JsonVariant &JsonArray::at(int index) const {
18-
node_type *node = _firstNode;
19-
while (node && index--) node = node->next;
18+
node_type *node = getNodeAt(index);
2019
return node ? node->content : JsonVariant::invalid();
2120
}
2221

@@ -43,6 +42,14 @@ JsonObject &JsonArray::createNestedObject() {
4342
return object;
4443
}
4544

45+
JsonArray::node_type *JsonArray::getNodeAt(int index) const {
46+
node_type *node = _firstNode;
47+
while (node && index--) node = node->next;
48+
return node;
49+
}
50+
51+
void JsonArray::removeAt(int index) { removeNode(getNodeAt(index)); }
52+
4653
void JsonArray::writeTo(JsonWriter &writer) const {
4754
writer.beginArray();
4855

test/JsonArray_Container_Tests.cpp

Lines changed: 45 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,11 @@ class JsonArray_Container_Tests : public ::testing::Test {
4848
}
4949
};
5050

51+
template <>
52+
void JsonArray_Container_Tests::itemMustEqual(int index, const char* expected) {
53+
EXPECT_STREQ(expected, _array[index].asString());
54+
}
55+
5156
TEST_F(JsonArray_Container_Tests, SuccessIsTrue) {
5257
EXPECT_TRUE(_array.success());
5358
}
@@ -87,14 +92,11 @@ TEST_F(JsonArray_Container_Tests, CanStoreBooleans) {
8792
}
8893

8994
TEST_F(JsonArray_Container_Tests, CanStoreStrings) {
90-
const char* firstString = "h3110";
91-
const char* secondString = "w0r1d";
92-
93-
_array.add(firstString);
94-
_array.add(secondString);
95+
_array.add("hello");
96+
_array.add("world");
9597

96-
firstMustEqual(firstString);
97-
secondMustEqual(secondString);
98+
firstMustEqual("hello");
99+
secondMustEqual("world");
98100
}
99101

100102
TEST_F(JsonArray_Container_Tests, CanStoreNestedArrays) {
@@ -134,3 +136,39 @@ TEST_F(JsonArray_Container_Tests, CanCreateNestedObjects) {
134136
firstMustReference(innerObject1);
135137
secondMustReference(innerObject2);
136138
}
139+
140+
TEST_F(JsonArray_Container_Tests, RemoveFirstElement) {
141+
_array.add("one");
142+
_array.add("two");
143+
_array.add("three");
144+
145+
_array.removeAt(0);
146+
147+
sizeMustBe(2);
148+
firstMustEqual("two");
149+
secondMustEqual("three");
150+
}
151+
152+
TEST_F(JsonArray_Container_Tests, RemoveMiddleElement) {
153+
_array.add("one");
154+
_array.add("two");
155+
_array.add("three");
156+
157+
_array.removeAt(1);
158+
159+
sizeMustBe(2);
160+
firstMustEqual("one");
161+
secondMustEqual("three");
162+
}
163+
164+
TEST_F(JsonArray_Container_Tests, RemoveLastElement) {
165+
_array.add("one");
166+
_array.add("two");
167+
_array.add("three");
168+
169+
_array.removeAt(2);
170+
171+
sizeMustBe(2);
172+
firstMustEqual("one");
173+
secondMustEqual("two");
174+
}

0 commit comments

Comments
 (0)
0