diff --git a/include/ArduinoJson/Internals/JsonArrayConstIterator.hpp b/include/ArduinoJson/Internals/JsonArrayConstIterator.hpp index 49d8d900..423e4864 100644 --- a/include/ArduinoJson/Internals/JsonArrayConstIterator.hpp +++ b/include/ArduinoJson/Internals/JsonArrayConstIterator.hpp @@ -11,6 +11,7 @@ namespace ArduinoJson { namespace Internals { +// TODO: copy from JsonArrayIterator class JsonArrayConstIterator { public: explicit JsonArrayConstIterator(JsonArrayNode *node) : _node(node) {} diff --git a/include/ArduinoJson/Internals/JsonArrayImpl.hpp b/include/ArduinoJson/Internals/JsonArrayImpl.hpp index e7375916..cb60a82e 100644 --- a/include/ArduinoJson/Internals/JsonArrayImpl.hpp +++ b/include/ArduinoJson/Internals/JsonArrayImpl.hpp @@ -39,6 +39,8 @@ class JsonArrayImpl { private: JsonArrayImpl(JsonBuffer *buffer) : _buffer(buffer), _firstNode(NULL) {} + inline void addNode(JsonArrayNode *node); + JsonBuffer *_buffer; Internals::JsonArrayNode *_firstNode; }; diff --git a/include/ArduinoJson/Internals/JsonArrayIterator.hpp b/include/ArduinoJson/Internals/JsonArrayIterator.hpp index 0cfd041c..3163cd17 100644 --- a/include/ArduinoJson/Internals/JsonArrayIterator.hpp +++ b/include/ArduinoJson/Internals/JsonArrayIterator.hpp @@ -29,8 +29,10 @@ class JsonArrayIterator { } JsonArrayIterator &operator++() { - _node = _node->next; - updateValue(); + if (_node) { + _node = _node->next; + updateValue(); + } return *this; } diff --git a/include/ArduinoJson/Internals/JsonObjectIterator.hpp b/include/ArduinoJson/Internals/JsonObjectIterator.hpp index 926ad9f2..ebfd87fd 100644 --- a/include/ArduinoJson/Internals/JsonObjectIterator.hpp +++ b/include/ArduinoJson/Internals/JsonObjectIterator.hpp @@ -25,7 +25,7 @@ class JsonObjectIterator { } JsonObjectIterator &operator++() { - _pair._node = _pair._node->next; + if (_pair._node) _pair._node = _pair._node->next; return *this; } diff --git a/include/ArduinoJson/Internals/JsonObjectNode.hpp b/include/ArduinoJson/Internals/JsonObjectNode.hpp index 0f516057..c7c9b2c1 100644 --- a/include/ArduinoJson/Internals/JsonObjectNode.hpp +++ b/include/ArduinoJson/Internals/JsonObjectNode.hpp @@ -24,7 +24,7 @@ class JsonObjectNode { JsonObjectNode* next; private: - JsonObjectNode(const char* k) : key(k) {} + JsonObjectNode(const char* k) : key(k), next(NULL) {} }; } } diff --git a/include/ArduinoJson/StaticJsonBuffer.hpp b/include/ArduinoJson/StaticJsonBuffer.hpp index a6428e92..5df85b7c 100644 --- a/include/ArduinoJson/StaticJsonBuffer.hpp +++ b/include/ArduinoJson/StaticJsonBuffer.hpp @@ -17,9 +17,11 @@ class StaticJsonBuffer : public JsonBuffer { virtual ~StaticJsonBuffer() {} - int capacity() { return CAPACITY; } + int capacity() const { return CAPACITY; } - int size() { return _size; } + int size() const { return _size; } + + void clear() { _size = 0; } protected: virtual void* alloc(size_t size) { diff --git a/src/Internals/JsonArrayImpl.cpp b/src/Internals/JsonArrayImpl.cpp index b67721cb..d640364e 100644 --- a/src/Internals/JsonArrayImpl.cpp +++ b/src/Internals/JsonArrayImpl.cpp @@ -27,19 +27,28 @@ int JsonArrayImpl::size() const { JsonValueImpl *JsonArrayImpl::operator[](int index) const { JsonArrayNode *node = _firstNode; while (node && index--) node = node->next; - - return NULL; + return node ? &node->value : NULL; } JsonValueImpl *JsonArrayImpl::add() { - if (_buffer) return NULL; - JsonArrayNode *node = JsonArrayNode::createFrom(_buffer); if (!node) return NULL; + addNode(node); + return &node->value; } +void JsonArrayImpl::addNode(JsonArrayNode *newNode) { + if (_firstNode) { + JsonArrayNode *lastNode = _firstNode; + while (lastNode->next) lastNode = lastNode->next; + lastNode->next = newNode; + } else { + _firstNode = newNode; + } +} + JsonArrayImpl *JsonArrayImpl::createNestedArray() { JsonValueImpl *value = add(); if (!value) return NULL; diff --git a/test/Issue10.cpp b/test/Issue10.cpp index dab35f43..7a4acaf2 100644 --- a/test/Issue10.cpp +++ b/test/Issue10.cpp @@ -42,7 +42,7 @@ class Issue10 : public testing::Test { }; TEST_F(Issue10, PopulateArrayByAddingAnObject) { - StaticJsonBuffer<20> json; + StaticJsonBuffer<200> json; JsonArray array = json.createArray(); for (int i = 0; i < 2; i++) { @@ -59,7 +59,7 @@ TEST_F(Issue10, PopulateArrayByAddingAnObject) { } TEST_F(Issue10, PopulateArrayByCreatingNestedObjects) { - StaticJsonBuffer<20> json; + StaticJsonBuffer<200> json; JsonArray array = json.createArray(); for (int i = 0; i < 2; i++) { diff --git a/test/JsonArray_Container_Tests.cpp b/test/JsonArray_Container_Tests.cpp index c885c64b..0745f115 100644 --- a/test/JsonArray_Container_Tests.cpp +++ b/test/JsonArray_Container_Tests.cpp @@ -15,9 +15,10 @@ using namespace ArduinoJson; class JsonArray_Container_Tests : public ::testing::Test { protected: - virtual void SetUp() { array = json.createArray(); } - - void nodeCountMustBe(int expected) { EXPECT_EQ(expected, json.size()); } + virtual void SetUp() { + json.clear(); + array = json.createArray(); + } template void firstElementMustBe(T expected) { @@ -31,7 +32,7 @@ class JsonArray_Container_Tests : public ::testing::Test { void sizeMustBe(int expected) { EXPECT_EQ(expected, array.size()); } - StaticJsonBuffer<42> json; + StaticJsonBuffer<256> json; JsonArray array; private: @@ -41,19 +42,18 @@ class JsonArray_Container_Tests : public ::testing::Test { } }; -TEST_F(JsonArray_Container_Tests, InitialSizeIsZero) { - sizeMustBe(0); - nodeCountMustBe(1); +TEST_F(JsonArray_Container_Tests, SuccessIsTrue) { + EXPECT_TRUE(array.success()); } +TEST_F(JsonArray_Container_Tests, InitialSizeIsZero) { sizeMustBe(0); } + TEST_F(JsonArray_Container_Tests, Grow_WhenValuesAreAdded) { array.add("hello"); sizeMustBe(1); - nodeCountMustBe(2); array.add("world"); sizeMustBe(2); - nodeCountMustBe(3); } TEST_F(JsonArray_Container_Tests, CanStoreIntegers) { @@ -62,7 +62,6 @@ TEST_F(JsonArray_Container_Tests, CanStoreIntegers) { firstElementMustBe(123); secondElementMustBe(456); - nodeCountMustBe(3); } TEST_F(JsonArray_Container_Tests, CanStoreDoubles) { @@ -71,7 +70,6 @@ TEST_F(JsonArray_Container_Tests, CanStoreDoubles) { firstElementMustBe(123.45); secondElementMustBe(456.78); - nodeCountMustBe(3); } TEST_F(JsonArray_Container_Tests, CanStoreBooleans) { @@ -80,7 +78,6 @@ TEST_F(JsonArray_Container_Tests, CanStoreBooleans) { firstElementMustBe(true); secondElementMustBe(false); - nodeCountMustBe(3); } TEST_F(JsonArray_Container_Tests, CanStoreStrings) { @@ -92,7 +89,6 @@ TEST_F(JsonArray_Container_Tests, CanStoreStrings) { firstElementMustBe(firstString); secondElementMustBe(secondString); - nodeCountMustBe(3); } TEST_F(JsonArray_Container_Tests, CanStoreNestedArrays) { @@ -104,7 +100,6 @@ TEST_F(JsonArray_Container_Tests, CanStoreNestedArrays) { firstElementMustBe(innerarray1); secondElementMustBe(innerarray2); - nodeCountMustBe(1 + 3 + 3); } TEST_F(JsonArray_Container_Tests, CanStoreNestedObjects) { @@ -116,7 +111,6 @@ TEST_F(JsonArray_Container_Tests, CanStoreNestedObjects) { firstElementMustBe(innerObject1); secondElementMustBe(innerObject2); - nodeCountMustBe(1 + 3 + 3); } TEST_F(JsonArray_Container_Tests, CanCreateNestedArrays) { @@ -125,7 +119,6 @@ TEST_F(JsonArray_Container_Tests, CanCreateNestedArrays) { firstElementMustBe(innerarray1); secondElementMustBe(innerarray2); - nodeCountMustBe(1 + 1 + 1); } TEST_F(JsonArray_Container_Tests, CanCreateNestedObjects) { @@ -134,5 +127,4 @@ TEST_F(JsonArray_Container_Tests, CanCreateNestedObjects) { firstElementMustBe(innerObject1); secondElementMustBe(innerObject2); - nodeCountMustBe(3); } diff --git a/test/JsonArray_Iterator_Tests.cpp b/test/JsonArray_Iterator_Tests.cpp index fa0857fa..05e25af6 100644 --- a/test/JsonArray_Iterator_Tests.cpp +++ b/test/JsonArray_Iterator_Tests.cpp @@ -11,7 +11,7 @@ using namespace ArduinoJson; TEST(JsonArray_Iterator_Test, SimpleTest) { - StaticJsonBuffer<42> jsonBuffer; + StaticJsonBuffer<100> jsonBuffer; JsonArray array = jsonBuffer.createArray(); array.add(12);