diff --git a/include/ArduinoJson/Internals/JsonArrayConstIterator.hpp b/include/ArduinoJson/Internals/JsonArrayConstIterator.hpp new file mode 100644 index 00000000..c42ad6ce --- /dev/null +++ b/include/ArduinoJson/Internals/JsonArrayConstIterator.hpp @@ -0,0 +1,38 @@ +// Copyright Benoit Blanchon 2014 +// MIT License +// +// Arduino JSON library +// https://github.com/bblanchon/ArduinoJson + +#pragma once + +#include "JsonValueInternal.hpp" + +namespace ArduinoJson { +namespace Internals { + +class JsonArrayConstIterator { + public: + explicit JsonArrayConstIterator(Internals::JsonNode *node) : _value(node) {} + + JsonValue operator*() const { return _value; } + JsonValue *operator->() { return &_value; } + + bool operator==(const JsonArrayConstIterator &other) const { + return _value.isSameAs(other._value); + } + + bool operator!=(const JsonArrayConstIterator &other) const { + return !_value.isSameAs(other._value); + } + + JsonArrayConstIterator &operator++() { + _value.moveToNext(); + return *this; + } + + private: + JsonValueInternal _value; +}; +} +} diff --git a/include/ArduinoJson/Internals/JsonArrayIterator.hpp b/include/ArduinoJson/Internals/JsonArrayIterator.hpp new file mode 100644 index 00000000..e79f2f36 --- /dev/null +++ b/include/ArduinoJson/Internals/JsonArrayIterator.hpp @@ -0,0 +1,38 @@ +// Copyright Benoit Blanchon 2014 +// MIT License +// +// Arduino JSON library +// https://github.com/bblanchon/ArduinoJson + +#pragma once + +#include "JsonValueInternal.hpp" + +namespace ArduinoJson { +namespace Internals { + +class JsonArrayIterator { + public: + explicit JsonArrayIterator(Internals::JsonNode *node) : _value(node) {} + + JsonValue operator*() const { return _value; } + JsonValue *operator->() { return &_value; } + + bool operator==(const JsonArrayIterator &other) const { + return _value.isSameAs(other._value); + } + + bool operator!=(const JsonArrayIterator &other) const { + return !_value.isSameAs(other._value); + } + + JsonArrayIterator &operator++() { + _value.moveToNext(); + return *this; + } + + private: + JsonValueInternal _value; +}; +} +} diff --git a/include/ArduinoJson/Internals/JsonObjectConstIterator.hpp b/include/ArduinoJson/Internals/JsonObjectConstIterator.hpp new file mode 100644 index 00000000..0cc78f85 --- /dev/null +++ b/include/ArduinoJson/Internals/JsonObjectConstIterator.hpp @@ -0,0 +1,38 @@ +// Copyright Benoit Blanchon 2014 +// MIT License +// +// Arduino JSON library +// https://github.com/bblanchon/ArduinoJson + +#pragma once + +#include "JsonPairInternal.hpp" + +namespace ArduinoJson { +namespace Internals { + +class JsonObjectConstIterator { + public: + explicit JsonObjectConstIterator(Internals::JsonNode *node) : _pair(node) {} + + JsonPair operator*() const { return _pair; } + JsonPair *operator->() { return &_pair; } + + bool operator==(const JsonObjectConstIterator &other) const { + return _pair.isSameAs(other._pair); + } + + bool operator!=(const JsonObjectConstIterator &other) const { + return !_pair.isSameAs(other._pair); + } + + JsonObjectConstIterator &operator++() { + _pair.moveToNext(); + return *this; + } + + private: + JsonPairInternal _pair; +}; +} +} diff --git a/include/ArduinoJson/Internals/JsonObjectIterator.hpp b/include/ArduinoJson/Internals/JsonObjectIterator.hpp new file mode 100644 index 00000000..745380c9 --- /dev/null +++ b/include/ArduinoJson/Internals/JsonObjectIterator.hpp @@ -0,0 +1,38 @@ +// Copyright Benoit Blanchon 2014 +// MIT License +// +// Arduino JSON library +// https://github.com/bblanchon/ArduinoJson + +#pragma once + +#include "JsonPairInternal.hpp" + +namespace ArduinoJson { +namespace Internals { + +class JsonObjectIterator { + public: + explicit JsonObjectIterator(Internals::JsonNode *node) : _pair(node) {} + + JsonPair operator*() const { return _pair; } + JsonPair *operator->() { return &_pair; } + + bool operator==(const JsonObjectIterator &other) const { + return _pair.isSameAs(other._pair); + } + + bool operator!=(const JsonObjectIterator &other) const { + return !_pair.isSameAs(other._pair); + } + + JsonObjectIterator &operator++() { + _pair.moveToNext(); + return *this; + } + + private: + JsonPairInternal _pair; +}; +} +} diff --git a/include/ArduinoJson/Internals/JsonPairInternal.hpp b/include/ArduinoJson/Internals/JsonPairInternal.hpp new file mode 100644 index 00000000..eb2db8ed --- /dev/null +++ b/include/ArduinoJson/Internals/JsonPairInternal.hpp @@ -0,0 +1,22 @@ +// Copyright Benoit Blanchon 2014 +// MIT License +// +// Arduino JSON library +// https://github.com/bblanchon/ArduinoJson + +#pragma once + +#include "../JsonPair.hpp" + +namespace ArduinoJson { +class JsonPairInternal : public JsonPair { + public: + explicit JsonPairInternal(Internals::JsonNode* node) : JsonPair(node) {} + + void moveToNext() { _node = _node->next; } + + bool isSameAs(const JsonPairInternal& other) const { + return _node == other._node; + } +}; +} diff --git a/include/ArduinoJson/Internals/JsonValueInternal.hpp b/include/ArduinoJson/Internals/JsonValueInternal.hpp new file mode 100644 index 00000000..af398433 --- /dev/null +++ b/include/ArduinoJson/Internals/JsonValueInternal.hpp @@ -0,0 +1,25 @@ +// Copyright Benoit Blanchon 2014 +// MIT License +// +// Arduino JSON library +// https://github.com/bblanchon/ArduinoJson + +#pragma once + +#include "../JsonValue.hpp" + +namespace ArduinoJson { +namespace Internals { + +class JsonValueInternal : public JsonValue { + public: + explicit JsonValueInternal(Internals::JsonNode* node) : JsonValue(node) {} + + void moveToNext() { _node = _node->next; } + + bool isSameAs(const JsonValueInternal& other) const { + return _node == other._node; + } +}; +} +} diff --git a/include/ArduinoJson/JsonArray.hpp b/include/ArduinoJson/JsonArray.hpp index 31217bf4..8164781a 100644 --- a/include/ArduinoJson/JsonArray.hpp +++ b/include/ArduinoJson/JsonArray.hpp @@ -6,14 +6,16 @@ #pragma once +#include "Internals/JsonArrayConstIterator.hpp" +#include "Internals/JsonArrayIterator.hpp" #include "JsonContainer.hpp" -#include "JsonIterator.hpp" namespace ArduinoJson { class JsonArray : public JsonContainer { public: - typedef JsonIterator iterator; - typedef JsonConstIterator const_iterator; + typedef JsonValue value_type; + typedef Internals::JsonArrayIterator iterator; + typedef Internals::JsonArrayConstIterator const_iterator; JsonArray() {} diff --git a/include/ArduinoJson/JsonIterator.hpp b/include/ArduinoJson/JsonIterator.hpp deleted file mode 100644 index d2280538..00000000 --- a/include/ArduinoJson/JsonIterator.hpp +++ /dev/null @@ -1,67 +0,0 @@ -// Copyright Benoit Blanchon 2014 -// MIT License -// -// Arduino JSON library -// https://github.com/bblanchon/ArduinoJson - -#pragma once - -#include "ForwardDeclarations.hpp" -#include "JsonValue.hpp" - -namespace ArduinoJson { - -template -class JsonIterator { - friend class JsonArray; - - public: - explicit JsonIterator(Internals::JsonNode *node) : _value(node) {} - - T operator*() const { return _value; } - T *operator->() { return &_value; } - - bool operator==(const JsonIterator &other) const { - return _value._node == other._value._node; - } - - bool operator!=(const JsonIterator &other) const { - return _value._node != other._value._node; - } - - JsonIterator &operator++() { - _value._node = _value._node->next; - return *this; - } - - private: - T _value; -}; - -template -class JsonConstIterator { - friend class JsonArray; - - public: - explicit JsonConstIterator(Internals::JsonNode *node) : _value(node) {} - - const JsonValue operator*() const { return _value; } - const JsonValue *operator->() { return &_value; } - - bool operator==(const JsonConstIterator &other) const { - return _value._node == other._value._node; - } - - bool operator!=(const JsonConstIterator &other) const { - return _value._node != other._value._node; - } - - JsonConstIterator &operator++() { - _value._node = _value._node->next; - return *this; - } - - private: - JsonValue _value; -}; -} \ No newline at end of file diff --git a/include/ArduinoJson/JsonObject.hpp b/include/ArduinoJson/JsonObject.hpp index 129c2086..1c56deec 100644 --- a/include/ArduinoJson/JsonObject.hpp +++ b/include/ArduinoJson/JsonObject.hpp @@ -6,15 +6,16 @@ #pragma once +#include "Internals/JsonObjectConstIterator.hpp" +#include "Internals/JsonObjectIterator.hpp" #include "JsonContainer.hpp" -#include "JsonIterator.hpp" -#include "JsonObjectKeyValue.hpp" namespace ArduinoJson { class JsonObject : public JsonContainer { public: - typedef JsonIterator iterator; - typedef JsonConstIterator const_iterator; + typedef JsonPair value_type; + typedef Internals::JsonObjectIterator iterator; + typedef Internals::JsonObjectConstIterator const_iterator; JsonObject() {} @@ -35,6 +36,7 @@ class JsonObject : public JsonContainer { const_iterator end() const { return const_iterator(0); } private: - Internals::JsonNode *getOrCreateNodeAt(const char *key); + Internals::JsonNode *getPairAt(const char *key); + Internals::JsonNode *getOrCreateValueAt(const char *key); }; } diff --git a/include/ArduinoJson/JsonObjectKeyValue.hpp b/include/ArduinoJson/JsonObjectKeyValue.hpp deleted file mode 100644 index b0bd0226..00000000 --- a/include/ArduinoJson/JsonObjectKeyValue.hpp +++ /dev/null @@ -1,27 +0,0 @@ -// Copyright Benoit Blanchon 2014 -// MIT License -// -// Arduino JSON library -// https://github.com/bblanchon/ArduinoJson - -#pragma once - -#include "ForwardDeclarations.hpp" -#include "JsonValue.hpp" - -namespace ArduinoJson { -class JsonObjectKeyValue { - friend class JsonObject; - template - friend class JsonIterator; - - public: - const char *key() const { return _node->getAsObjectKey(); } - JsonValue value() { return JsonValue(_node->getAsObjectValue()); } - - private: - explicit JsonObjectKeyValue(Internals::JsonNode *node) : _node(node) {} - - Internals::JsonNode *_node; -}; -} diff --git a/include/ArduinoJson/JsonPair.hpp b/include/ArduinoJson/JsonPair.hpp new file mode 100644 index 00000000..0cf6a892 --- /dev/null +++ b/include/ArduinoJson/JsonPair.hpp @@ -0,0 +1,25 @@ +// Copyright Benoit Blanchon 2014 +// MIT License +// +// Arduino JSON library +// https://github.com/bblanchon/ArduinoJson + +#pragma once + +#include "Internals/JsonValueInternal.hpp" + +namespace ArduinoJson { +class JsonPair { + public: + const char *key() const { return _node->getAsObjectKey(); } + + JsonValue value() { + return Internals::JsonValueInternal(_node->getAsObjectValue()); + } + + protected: + explicit JsonPair(Internals::JsonNode *node) : _node(node) {} + + Internals::JsonNode *_node; +}; +} diff --git a/include/ArduinoJson/JsonValue.hpp b/include/ArduinoJson/JsonValue.hpp index ae19cc17..a1819d70 100644 --- a/include/ArduinoJson/JsonValue.hpp +++ b/include/ArduinoJson/JsonValue.hpp @@ -12,15 +12,6 @@ namespace ArduinoJson { class JsonValue : public Internals::JsonNodeWrapper { - friend class JsonArray; - template - friend class JsonIterator; - template - friend class JsonConstIterator; - friend class JsonBuffer; - friend class JsonObject; - friend class JsonObjectKeyValue; - public: JsonValue() {} @@ -48,7 +39,7 @@ class JsonValue : public Internals::JsonNodeWrapper { return static_cast(*this); } - private: - explicit JsonValue(Internals::JsonNode *node) : JsonNodeWrapper(node) {} + protected: + JsonValue(Internals::JsonNode *node) : Internals::JsonNodeWrapper(node) {} }; } diff --git a/src/JsonBuffer.cpp b/src/JsonBuffer.cpp index 82e3b089..1d58b9f5 100644 --- a/src/JsonBuffer.cpp +++ b/src/JsonBuffer.cpp @@ -8,14 +8,14 @@ #include -#include "ArduinoJson/JsonValue.hpp" +#include "ArduinoJson/Internals/JsonValueInternal.hpp" #include "ArduinoJson/Internals/JsonParser.hpp" #include "ArduinoJson/Internals/JsonNode.hpp" using namespace ArduinoJson; using namespace ArduinoJson::Internals; -JsonValue JsonBuffer::createValue() { return JsonValue(createNode()); } +JsonValue JsonBuffer::createValue() { return JsonValueInternal(createNode()); } JsonNode *JsonBuffer::createNode() { void *node = allocateNode(); @@ -36,7 +36,7 @@ JsonObject JsonBuffer::parseObject(char *json) { JsonValue JsonBuffer::parseValue(char *json) { JsonParser parser(this, json); - return JsonValue(parser.parseAnything()); + return JsonValueInternal(parser.parseAnything()); } JsonNode *JsonBuffer::createArrayNode() { diff --git a/src/JsonObject.cpp b/src/JsonObject.cpp index 3831efc4..92a3c6dc 100644 --- a/src/JsonObject.cpp +++ b/src/JsonObject.cpp @@ -17,20 +17,17 @@ using namespace ArduinoJson; using namespace ArduinoJson::Internals; JsonValue JsonObject::operator[](char const *key) { - JsonNode *node = getOrCreateNodeAt(key); - return JsonValue(node); + JsonNode *node = getOrCreateValueAt(key); + return JsonValueInternal(node); } void JsonObject::remove(char const *key) { - for (iterator it = begin(); it != end(); ++it) { - if (!strcmp(it->key(), key)) { - removeChild(it->_node); - } - } + JsonNode *nodeToRemove = getPairAt(key); + if (nodeToRemove) removeChild(nodeToRemove); } JsonArray JsonObject::createNestedArray(char const *key) { - JsonNode *node = getOrCreateNodeAt(key); + JsonNode *node = getOrCreateValueAt(key); if (node) node->setAsArray(_node->getContainerBuffer()); @@ -38,17 +35,23 @@ JsonArray JsonObject::createNestedArray(char const *key) { } JsonObject JsonObject::createNestedObject(char const *key) { - JsonNode *node = getOrCreateNodeAt(key); + JsonNode *node = getOrCreateValueAt(key); if (node) node->setAsObject(_node->getContainerBuffer()); return JsonObject(node); } -JsonNode *JsonObject::getOrCreateNodeAt(const char *key) { - for (iterator it = begin(); it != end(); ++it) { - if (!strcmp(it->key(), key)) return it->value()._node; +JsonNode *JsonObject::getPairAt(const char *key) { + for (JsonNode *node = firstChild(); node; node = node->next) { + if (!strcmp(node->getAsObjectKey(), key)) return node; } + return NULL; +} + +JsonNode *JsonObject::getOrCreateValueAt(const char *key) { + JsonNode *existingNode = getPairAt(key); + if (existingNode) return existingNode->getAsObjectValue(); JsonNode *newValueNode = createNode(); if (!newValueNode) return 0;