Removed usages of JsonNodeIterator

This commit is contained in:
Benoit Blanchon
2014-10-24 18:31:50 +02:00
parent 68fb03577c
commit 1f6cd8e56e
16 changed files with 84 additions and 40 deletions

View File

@ -9,6 +9,7 @@
namespace ArduinoJson { namespace ArduinoJson {
class JsonArray; class JsonArray;
class JsonArrayIterator; class JsonArrayIterator;
class JsonArrayConstIterator;
class JsonBuffer; class JsonBuffer;
class JsonObject; class JsonObject;
class JsonObjectIterator; class JsonObjectIterator;

View File

@ -12,9 +12,13 @@
namespace ArduinoJson { namespace ArduinoJson {
class JsonArray : public JsonContainer { class JsonArray : public JsonContainer {
public: public:
typedef JsonArrayIterator iterator;
typedef JsonArrayConstIterator const_iterator;
JsonArray() {} JsonArray() {}
explicit JsonArray(Internals::JsonNode *node) : JsonContainer(node) {} explicit JsonArray(Internals::JsonNode *node)
: JsonContainer(node) {} // TODO: hide
JsonValue operator[](int index) const; JsonValue operator[](int index) const;
@ -30,8 +34,10 @@ class JsonArray : public JsonContainer {
bool success() { return _node && _node->isArray(); } bool success() { return _node && _node->isArray(); }
JsonArrayIterator begin(); iterator begin() { return iterator(firstChild()); }
iterator end() { return iterator(0); }
JsonArrayIterator end() { return JsonArrayIterator(0); } const_iterator begin() const { return const_iterator(firstChild()); }
const_iterator end() const { return const_iterator(0); }
}; };
} }

View File

@ -36,4 +36,30 @@ class JsonArrayIterator {
private: private:
JsonValue _value; JsonValue _value;
}; };
class JsonArrayConstIterator {
friend class JsonArray;
public:
explicit JsonArrayConstIterator(Internals::JsonNode *node) : _value(node) {}
const JsonValue operator*() const { return _value; }
const JsonValue *operator->() { return &_value; }
bool operator==(const JsonArrayConstIterator &other) const {
return _value._node == other._value._node;
}
bool operator!=(const JsonArrayConstIterator &other) const {
return _value._node != other._value._node;
}
JsonArrayConstIterator &operator++() {
_value._node = _value._node->next;
return *this;
}
private:
JsonValue _value;
};
} }

View File

@ -44,5 +44,6 @@ class JsonContainer : public Printable, public Internals::JsonNodeWrapper {
void addChild(Internals::JsonNode *); void addChild(Internals::JsonNode *);
void removeChild(Internals::JsonNode *); void removeChild(Internals::JsonNode *);
Internals::JsonNode *createNode(); Internals::JsonNode *createNode();
Internals::JsonNode* firstChild() const;
}; };
} }

View File

@ -24,7 +24,7 @@ class JsonObject : public JsonContainer {
bool success() { return _node && _node->isObject(); } bool success() { return _node && _node->isObject(); }
JsonObjectIterator begin(); JsonObjectIterator begin() { return JsonObjectIterator(firstChild()); }
JsonObjectIterator end() { return JsonObjectIterator(0); } JsonObjectIterator end() { return JsonObjectIterator(0); }

View File

@ -19,11 +19,11 @@ class JsonObjectIterator {
JsonObjectKeyValue *operator->() { return &_keyValue; } JsonObjectKeyValue *operator->() { return &_keyValue; }
bool operator==(const JsonObjectIterator &other) const { bool operator==(const JsonObjectIterator &other) const {
return _keyValue == other._keyValue; return _keyValue._node == other._keyValue._node;
} }
bool operator!=(const JsonObjectIterator &other) const { bool operator!=(const JsonObjectIterator &other) const {
return _keyValue != other._keyValue; return _keyValue._node != other._keyValue._node;
} }
JsonObjectIterator &operator++() { JsonObjectIterator &operator++() {

View File

@ -11,21 +11,13 @@
namespace ArduinoJson { namespace ArduinoJson {
class JsonObjectKeyValue { class JsonObjectKeyValue {
friend class JsonObject;
friend class JsonObjectIterator; friend class JsonObjectIterator;
public: public:
const char *key() const { return _node->getAsObjectKey(); } const char *key() const { return _node->getAsObjectKey(); }
JsonValue value() { return JsonValue(_node->getAsObjectValue()); } JsonValue value() { return JsonValue(_node->getAsObjectValue()); }
bool operator==(const JsonObjectKeyValue &other) const {
return _node == other._node;
}
bool operator!=(const JsonObjectKeyValue &other) const {
return _node != other._node;
}
private: private:
explicit JsonObjectKeyValue(Internals::JsonNode *node) : _node(node) {} explicit JsonObjectKeyValue(Internals::JsonNode *node) : _node(node) {}

View File

@ -14,6 +14,7 @@ namespace ArduinoJson {
class JsonValue : public Internals::JsonNodeWrapper { class JsonValue : public Internals::JsonNodeWrapper {
friend class JsonArray; friend class JsonArray;
friend class JsonArrayIterator; friend class JsonArrayIterator;
friend class JsonArrayConstIterator;
friend class JsonBuffer; friend class JsonBuffer;
friend class JsonObject; friend class JsonObject;
friend class JsonObjectKeyValue; friend class JsonObjectKeyValue;

View File

@ -12,8 +12,8 @@ using namespace ArduinoJson;
using namespace ArduinoJson::Internals; using namespace ArduinoJson::Internals;
JsonValue JsonArray::operator[](int index) const { JsonValue JsonArray::operator[](int index) const {
for (JsonNodeIterator it = beginChildren(); it != endChildren(); ++it) { for (const_iterator it = begin(); it != end(); ++it) {
if (!index) return JsonValue(*it); if (!index) return *it;
index--; index--;
} }
@ -82,9 +82,3 @@ JsonObject JsonArray::createNestedObject() {
return JsonObject(node); return JsonObject(node);
} }
JsonArrayIterator JsonArray::begin() {
if (!_node) return end();
return JsonArrayIterator(_node->getContainerChild());
}

View File

@ -73,3 +73,8 @@ size_t JsonContainer::size() const {
return n; return n;
} }
JsonNode* JsonContainer::firstChild() const
{
return _node ? _node->getContainerChild() : 0;
}

View File

@ -22,11 +22,9 @@ JsonValue JsonObject::operator[](char const *key) {
} }
void JsonObject::remove(char const *key) { void JsonObject::remove(char const *key) {
for (JsonNodeIterator it = beginChildren(); it != endChildren(); ++it) { for (JsonObjectIterator it = begin(); it != end(); ++it) {
const char *childKey = it->getAsObjectKey(); if (!strcmp(it->key(), key)) {
removeChild(it->_node);
if (!strcmp(childKey, key)) {
removeChild(*it);
} }
} }
} }
@ -48,10 +46,8 @@ JsonObject JsonObject::createNestedObject(char const *key) {
} }
JsonNode *JsonObject::getOrCreateNodeAt(const char *key) { JsonNode *JsonObject::getOrCreateNodeAt(const char *key) {
for (JsonNodeIterator it = beginChildren(); it != endChildren(); ++it) { for (JsonObjectIterator it = begin(); it != end(); ++it) {
const char *childKey = it->getAsObjectKey(); if (!strcmp(it->key(), key)) return it->value()._node;
if (!strcmp(childKey, key)) return it->getAsObjectValue();
} }
JsonNode *newValueNode = createNode(); JsonNode *newValueNode = createNode();
@ -66,7 +62,3 @@ JsonNode *JsonObject::getOrCreateNodeAt(const char *key) {
return newValueNode; return newValueNode;
} }
JsonObjectIterator JsonObject::begin() {
return JsonObjectIterator(_node->getContainerChild());
}

View File

@ -1,7 +1,7 @@
set(GTEST_DIR ../third-party/gtest-1.7.0) set(GTEST_DIR ../third-party/gtest-1.7.0)
file(GLOB_RECURSE INC_FILES ../include/*.h) file(GLOB_RECURSE INC_FILES ../include/*.h)
file(GLOB TESTS_FILES *.cpp) file(GLOB TESTS_FILES *.hpp *.cpp)
include_directories( include_directories(
../include ../include

View File

@ -7,6 +7,7 @@
#include <gtest/gtest.h> #include <gtest/gtest.h>
#include <ArduinoJson/StaticJsonBuffer.hpp> #include <ArduinoJson/StaticJsonBuffer.hpp>
#include <ArduinoJson/JsonValue.hpp> #include <ArduinoJson/JsonValue.hpp>
#include "Printers.hpp"
using namespace ArduinoJson; using namespace ArduinoJson;
@ -81,8 +82,8 @@ TEST_F(JsonArray_Container_Tests, CanStoreBooleans) {
} }
TEST_F(JsonArray_Container_Tests, CanStoreStrings) { TEST_F(JsonArray_Container_Tests, CanStoreStrings) {
const char *firstString = "h3110"; const char* firstString = "h3110";
const char *secondString = "w0r1d"; const char* secondString = "w0r1d";
array.add(firstString); array.add(firstString);
array.add(secondString); array.add(secondString);

View File

@ -7,6 +7,7 @@
#include <gtest/gtest.h> #include <gtest/gtest.h>
#include <ArduinoJson/StaticJsonBuffer.hpp> #include <ArduinoJson/StaticJsonBuffer.hpp>
#include <ArduinoJson/JsonValue.hpp> #include <ArduinoJson/JsonValue.hpp>
#include "Printers.hpp"
using namespace ArduinoJson; using namespace ArduinoJson;

12
test/Printers.cpp Normal file
View File

@ -0,0 +1,12 @@
// Copyright Benoit Blanchon 2014
// MIT License
//
// Arduino JSON library
// https://github.com/bblanchon/ArduinoJson
#include "Printers.hpp"
std::ostream& operator<<(std::ostream& os, const ArduinoJson::JsonValue& v) {
os << "JsonValue"; // TODO
return os;
}

12
test/Printers.hpp Normal file
View File

@ -0,0 +1,12 @@
// Copyright Benoit Blanchon 2014
// MIT License
//
// Arduino JSON library
// https://github.com/bblanchon/ArduinoJson
#pragma once
#include <ArduinoJson/ForwardDeclarations.hpp>
#include <ostream>
std::ostream& operator<<(std::ostream& os, const ArduinoJson::JsonValue& v);