Improved JsonArrayIterator

This commit is contained in:
Benoit Blanchon
2014-10-24 16:12:05 +02:00
parent 8071434515
commit bbef8931a6
5 changed files with 46 additions and 31 deletions

View File

@ -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;
};
}

View File

@ -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;
};
}

View File

@ -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;
};
}

View File

@ -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<T>(*this);
}
private:
explicit JsonValue(Internals::JsonNode *node) : JsonNodeWrapper(node) {}
};
}

View File

@ -21,10 +21,10 @@ TEST(JsonArray_Iterator_Test, SimpleTest) {
JsonArrayIterator end = array.end();
EXPECT_NE(end, it);
EXPECT_EQ(12, (*it).as<int>()); // TODO: use ->
EXPECT_EQ(12, it->as<int>());
++it;
EXPECT_NE(end, it);
EXPECT_EQ(34, (*it).as<int>()); // TODO: use ->
EXPECT_EQ(34, it->as<int>());
++it;
EXPECT_EQ(array.end(), it);
}