diff --git a/include/ArduinoJson/JsonObject.hpp b/include/ArduinoJson/JsonObject.hpp index a452f88f..3a6d4a75 100644 --- a/include/ArduinoJson/JsonObject.hpp +++ b/include/ArduinoJson/JsonObject.hpp @@ -1,6 +1,7 @@ #pragma once -#include "JsonContainer.hpp" +#include "ArduinoJson/JsonContainer.hpp" +#include "ArduinoJson/JsonObjectIterator.hpp" namespace ArduinoJson { @@ -28,6 +29,13 @@ namespace ArduinoJson return _node && _node->isObject(); } + JsonObjectIterator begin(); + + JsonObjectIterator end() + { + return JsonObjectIterator(0); + } + private: Internals::JsonNode* getOrCreateNodeAt(const char* key); }; diff --git a/include/ArduinoJson/JsonObjectIterator.hpp b/include/ArduinoJson/JsonObjectIterator.hpp new file mode 100644 index 00000000..06a6fb9d --- /dev/null +++ b/include/ArduinoJson/JsonObjectIterator.hpp @@ -0,0 +1,52 @@ +#pragma once + +#include "ArduinoJson/JsonObjectKeyValue.hpp" + +namespace ArduinoJson +{ + class JsonObject; + + class JsonObjectIterator + { + friend class JsonObject; + + public: + explicit JsonObjectIterator(Internals::JsonNode* node) + : _node(node) + { + } + + const char* key() const + { + return operator*().key(); + } + + JsonValue value() const + { + return operator*().value(); + } + + void operator++() + { + _node = _node->next; + } + + JsonObjectKeyValue operator*() const + { + return JsonObjectKeyValue(_node); + } + + bool operator==(const JsonObjectIterator& other) const + { + return _node == other._node; + } + + bool operator!=(const JsonObjectIterator& other) const + { + return _node != other._node; + } + + private: + Internals::JsonNode* _node; + }; +} \ No newline at end of file diff --git a/include/ArduinoJson/JsonObjectKeyValue.hpp b/include/ArduinoJson/JsonObjectKeyValue.hpp new file mode 100644 index 00000000..51855fbc --- /dev/null +++ b/include/ArduinoJson/JsonObjectKeyValue.hpp @@ -0,0 +1,28 @@ +#pragma once + +#include "ArduinoJson/JsonValue.hpp" + +namespace ArduinoJson +{ + class JsonObjectKeyValue + { + public: + explicit JsonObjectKeyValue(Internals::JsonNode* node) + : _node(node) + { + } + + const char* key() + { + return _node->getAsObjectKey(); + } + + JsonValue value() + { + return JsonValue(_node->getAsObjectValue()); + } + + private: + Internals::JsonNode* _node; + }; +} \ No newline at end of file diff --git a/src/JsonObject.cpp b/src/JsonObject.cpp index 9c95a98a..78f0ecdb 100644 --- a/src/JsonObject.cpp +++ b/src/JsonObject.cpp @@ -70,4 +70,9 @@ JsonNode* JsonObject::getOrCreateNodeAt(const char* key) addChild(newKeyNode); return newValueNode; +} + +JsonObjectIterator JsonObject::begin() +{ + return JsonObjectIterator(_node->getContainerChild()); } \ No newline at end of file diff --git a/test/JsonObject_Iterator_Tests.cpp b/test/JsonObject_Iterator_Tests.cpp new file mode 100644 index 00000000..da0b24d7 --- /dev/null +++ b/test/JsonObject_Iterator_Tests.cpp @@ -0,0 +1,27 @@ +#include +#include +#include + +using namespace ArduinoJson; + +TEST(JsonObject_Iterator_Test, SimpleTest) +{ + StaticJsonBuffer<42> jsonBuffer; + + JsonObject object = jsonBuffer.createObject(); + object["ab"] = 12; + object["cd"] = 34; + + JsonObjectIterator it = object.begin(); + JsonObjectIterator end = object.end(); + + EXPECT_NE(end, it); + EXPECT_STREQ("ab", it.key()); + EXPECT_EQ(12, static_cast(it.value())); + ++it; + EXPECT_NE(end, it); + EXPECT_STREQ("cd", it.key()); + EXPECT_EQ(34, static_cast(it.value())); + ++it; + EXPECT_EQ(object.end(), it); +} \ No newline at end of file