From bbef8931a653d1c96d4dad014986a4f63b3bc384 Mon Sep 17 00:00:00 2001 From: Benoit Blanchon Date: Fri, 24 Oct 2014 16:12:05 +0200 Subject: [PATCH] Improved JsonArrayIterator --- include/ArduinoJson/JsonArrayIterator.hpp | 19 ++++++++++------ include/ArduinoJson/JsonObjectIterator.hpp | 26 ++++++++++------------ include/ArduinoJson/JsonObjectKeyValue.hpp | 12 +++++----- include/ArduinoJson/JsonValue.hpp | 16 ++++++++++--- test/JsonArray_Iterator_Tests.cpp | 4 ++-- 5 files changed, 46 insertions(+), 31 deletions(-) diff --git a/include/ArduinoJson/JsonArrayIterator.hpp b/include/ArduinoJson/JsonArrayIterator.hpp index 454be95e..8e52cb8a 100644 --- a/include/ArduinoJson/JsonArrayIterator.hpp +++ b/include/ArduinoJson/JsonArrayIterator.hpp @@ -9,27 +9,32 @@ #include "ArduinoJson/JsonValue.hpp" namespace ArduinoJson { + class JsonArray; class JsonArrayIterator { friend class JsonArray; public: - explicit JsonArrayIterator(Internals::JsonNode *node) : _node(node) {} + explicit JsonArrayIterator(Internals::JsonNode *node) : _value(node) {} - void operator++() { _node = _node->next; } - - JsonValue operator*() const { return JsonValue(_node); } + JsonValue operator*() const { return _value; } + JsonValue *operator->() { return &_value; } bool operator==(const JsonArrayIterator &other) const { - return _node == other._node; + return _value._node == other._value._node; } bool operator!=(const JsonArrayIterator &other) const { - return _node != other._node; + return _value._node != other._value._node; + } + + JsonArrayIterator &operator++() { + _value._node = _value._node->next; + return *this; } private: - Internals::JsonNode *_node; + JsonValue _value; }; } diff --git a/include/ArduinoJson/JsonObjectIterator.hpp b/include/ArduinoJson/JsonObjectIterator.hpp index 10a67e04..ca38acf5 100644 --- a/include/ArduinoJson/JsonObjectIterator.hpp +++ b/include/ArduinoJson/JsonObjectIterator.hpp @@ -15,27 +15,25 @@ class JsonObjectIterator { friend class JsonObject; public: - explicit JsonObjectIterator(Internals::JsonNode *node) - : _objectKeyValue(node) {} - - JsonObjectIterator &operator++() { - _objectKeyValue = JsonObjectKeyValue(_objectKeyValue.next()); - return *this; - } - - JsonObjectKeyValue operator*() const { return _objectKeyValue; } - - JsonObjectKeyValue *operator->() { return &_objectKeyValue; } + JsonObjectKeyValue operator*() const { return _keyValue; } + JsonObjectKeyValue *operator->() { return &_keyValue; } bool operator==(const JsonObjectIterator &other) const { - return _objectKeyValue == other._objectKeyValue; + return _keyValue == other._keyValue; } bool operator!=(const JsonObjectIterator &other) const { - return _objectKeyValue != other._objectKeyValue; + return _keyValue != other._keyValue; + } + + JsonObjectIterator &operator++() { + _keyValue._node = _keyValue._node->next; + return *this; } private: - JsonObjectKeyValue _objectKeyValue; + explicit JsonObjectIterator(Internals::JsonNode *node) : _keyValue(node) {} + + JsonObjectKeyValue _keyValue; }; } diff --git a/include/ArduinoJson/JsonObjectKeyValue.hpp b/include/ArduinoJson/JsonObjectKeyValue.hpp index e02cb311..51047743 100644 --- a/include/ArduinoJson/JsonObjectKeyValue.hpp +++ b/include/ArduinoJson/JsonObjectKeyValue.hpp @@ -9,10 +9,12 @@ #include "ArduinoJson/JsonValue.hpp" namespace ArduinoJson { -class JsonObjectKeyValue { - public: - explicit JsonObjectKeyValue(Internals::JsonNode *node) : _node(node) {} +class JsonObjectIterator; +class JsonObjectKeyValue { + friend class JsonObjectIterator; + + public: const char *key() const { return _node->getAsObjectKey(); } JsonValue value() { return JsonValue(_node->getAsObjectValue()); } @@ -25,9 +27,9 @@ class JsonObjectKeyValue { return _node != other._node; } - Internals::JsonNode *next() { return _node->next; } - private: + explicit JsonObjectKeyValue(Internals::JsonNode *node) : _node(node) {} + Internals::JsonNode *_node; }; } diff --git a/include/ArduinoJson/JsonValue.hpp b/include/ArduinoJson/JsonValue.hpp index f90ba0d3..44d36b40 100644 --- a/include/ArduinoJson/JsonValue.hpp +++ b/include/ArduinoJson/JsonValue.hpp @@ -10,15 +10,22 @@ namespace ArduinoJson { class JsonArray; -class JsonContainer; +class JsonArrayIterator; +class JsonBuffer; class JsonObject; +class JsonObjectIterator; +class JsonObjectKeyValue; class JsonValue : public Internals::JsonNodeWrapper { + friend class JsonArray; + friend class JsonArrayIterator; + friend class JsonBuffer; + friend class JsonObject; + friend class JsonObjectKeyValue; + public: JsonValue() {} - explicit JsonValue(Internals::JsonNode *node) : JsonNodeWrapper(node) {} - void operator=(bool value); void operator=(const char *value); void operator=(double value) { set(value, 2); } @@ -42,5 +49,8 @@ class JsonValue : public Internals::JsonNodeWrapper { T as() { return static_cast(*this); } + + private: + explicit JsonValue(Internals::JsonNode *node) : JsonNodeWrapper(node) {} }; } diff --git a/test/JsonArray_Iterator_Tests.cpp b/test/JsonArray_Iterator_Tests.cpp index 4de46e30..c39f7ebe 100644 --- a/test/JsonArray_Iterator_Tests.cpp +++ b/test/JsonArray_Iterator_Tests.cpp @@ -21,10 +21,10 @@ TEST(JsonArray_Iterator_Test, SimpleTest) { JsonArrayIterator end = array.end(); EXPECT_NE(end, it); - EXPECT_EQ(12, (*it).as()); // TODO: use -> + EXPECT_EQ(12, it->as()); ++it; EXPECT_NE(end, it); - EXPECT_EQ(34, (*it).as()); // TODO: use -> + EXPECT_EQ(34, it->as()); ++it; EXPECT_EQ(array.end(), it); }