mirror of
https://github.com/bblanchon/ArduinoJson.git
synced 2025-07-21 06:22:23 +02:00
Added JsonArray::removeAt() (issue #58)
This commit is contained in:
@ -4,6 +4,7 @@ Arduino JSON: change log
|
|||||||
HEAD
|
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 stack-overflow in `DynamicJsonBuffer` when parsing huge JSON files (issue #65)
|
||||||
* Fixed wrong return value of `parseArray()` and `parseObject()` when allocation fails (issue #68)
|
* Fixed wrong return value of `parseArray()` and `parseObject()` when allocation fails (issue #68)
|
||||||
|
|
||||||
|
@ -71,6 +71,9 @@ class JsonArray : public Internals::JsonPrintable<JsonArray>,
|
|||||||
// It's a shortcut for JsonBuffer::createObject() and JsonArray::add()
|
// It's a shortcut for JsonBuffer::createObject() and JsonArray::add()
|
||||||
JsonObject &createNestedObject();
|
JsonObject &createNestedObject();
|
||||||
|
|
||||||
|
// Removes element at specified index.
|
||||||
|
void removeAt(int index);
|
||||||
|
|
||||||
// Returns a reference an invalid JsonArray.
|
// Returns a reference an invalid JsonArray.
|
||||||
// This object is meant to replace a NULL pointer.
|
// This object is meant to replace a NULL pointer.
|
||||||
// This is used when memory allocation or JSON parsing fail.
|
// This is used when memory allocation or JSON parsing fail.
|
||||||
@ -84,6 +87,8 @@ class JsonArray : public Internals::JsonPrintable<JsonArray>,
|
|||||||
explicit JsonArray(JsonBuffer *buffer)
|
explicit JsonArray(JsonBuffer *buffer)
|
||||||
: Internals::List<JsonVariant>(buffer) {}
|
: Internals::List<JsonVariant>(buffer) {}
|
||||||
|
|
||||||
|
node_type *getNodeAt(int index) const;
|
||||||
|
|
||||||
// The instance returned by JsonArray::invalid()
|
// The instance returned by JsonArray::invalid()
|
||||||
static JsonArray _invalid;
|
static JsonArray _invalid;
|
||||||
};
|
};
|
||||||
|
@ -15,8 +15,7 @@ using namespace ArduinoJson::Internals;
|
|||||||
JsonArray JsonArray::_invalid(NULL);
|
JsonArray JsonArray::_invalid(NULL);
|
||||||
|
|
||||||
JsonVariant &JsonArray::at(int index) const {
|
JsonVariant &JsonArray::at(int index) const {
|
||||||
node_type *node = _firstNode;
|
node_type *node = getNodeAt(index);
|
||||||
while (node && index--) node = node->next;
|
|
||||||
return node ? node->content : JsonVariant::invalid();
|
return node ? node->content : JsonVariant::invalid();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -43,6 +42,14 @@ JsonObject &JsonArray::createNestedObject() {
|
|||||||
return object;
|
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 {
|
void JsonArray::writeTo(JsonWriter &writer) const {
|
||||||
writer.beginArray();
|
writer.beginArray();
|
||||||
|
|
||||||
|
@ -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) {
|
TEST_F(JsonArray_Container_Tests, SuccessIsTrue) {
|
||||||
EXPECT_TRUE(_array.success());
|
EXPECT_TRUE(_array.success());
|
||||||
}
|
}
|
||||||
@ -87,14 +92,11 @@ TEST_F(JsonArray_Container_Tests, CanStoreBooleans) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
TEST_F(JsonArray_Container_Tests, CanStoreStrings) {
|
TEST_F(JsonArray_Container_Tests, CanStoreStrings) {
|
||||||
const char* firstString = "h3110";
|
_array.add("hello");
|
||||||
const char* secondString = "w0r1d";
|
_array.add("world");
|
||||||
|
|
||||||
_array.add(firstString);
|
firstMustEqual("hello");
|
||||||
_array.add(secondString);
|
secondMustEqual("world");
|
||||||
|
|
||||||
firstMustEqual(firstString);
|
|
||||||
secondMustEqual(secondString);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST_F(JsonArray_Container_Tests, CanStoreNestedArrays) {
|
TEST_F(JsonArray_Container_Tests, CanStoreNestedArrays) {
|
||||||
@ -134,3 +136,39 @@ TEST_F(JsonArray_Container_Tests, CanCreateNestedObjects) {
|
|||||||
firstMustReference(innerObject1);
|
firstMustReference(innerObject1);
|
||||||
secondMustReference(innerObject2);
|
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");
|
||||||
|
}
|
||||||
|
Reference in New Issue
Block a user