forked from bblanchon/ArduinoJson
Merged JsonArrayIterator and JsonObjectIterator into a one template class
This commit is contained in:
@ -8,13 +8,16 @@
|
||||
|
||||
namespace ArduinoJson {
|
||||
class JsonArray;
|
||||
class JsonArrayIterator;
|
||||
class JsonArrayConstIterator;
|
||||
class JsonBuffer;
|
||||
class JsonObject;
|
||||
class JsonObjectIterator;
|
||||
class JsonObjectKeyValue;
|
||||
class JsonValue;
|
||||
template <typename T>
|
||||
class JsonIterator;
|
||||
template <typename T>
|
||||
class JsonConstIterator;
|
||||
|
||||
namespace Internals {
|
||||
class IndentedPrint;
|
||||
class JsonNode;
|
||||
|
@ -7,13 +7,13 @@
|
||||
#pragma once
|
||||
|
||||
#include "JsonContainer.hpp"
|
||||
#include "JsonArrayIterator.hpp"
|
||||
#include "JsonIterator.hpp"
|
||||
|
||||
namespace ArduinoJson {
|
||||
class JsonArray : public JsonContainer {
|
||||
public:
|
||||
typedef JsonArrayIterator iterator;
|
||||
typedef JsonArrayConstIterator const_iterator;
|
||||
typedef JsonIterator<JsonValue> iterator;
|
||||
typedef JsonConstIterator<JsonValue> const_iterator;
|
||||
|
||||
JsonArray() {}
|
||||
|
||||
|
@ -11,50 +11,52 @@
|
||||
|
||||
namespace ArduinoJson {
|
||||
|
||||
class JsonArrayIterator {
|
||||
template <typename T>
|
||||
class JsonIterator {
|
||||
friend class JsonArray;
|
||||
|
||||
public:
|
||||
explicit JsonArrayIterator(Internals::JsonNode *node) : _value(node) {}
|
||||
explicit JsonIterator(Internals::JsonNode *node) : _value(node) {}
|
||||
|
||||
JsonValue operator*() const { return _value; }
|
||||
JsonValue *operator->() { return &_value; }
|
||||
T operator*() const { return _value; }
|
||||
T *operator->() { return &_value; }
|
||||
|
||||
bool operator==(const JsonArrayIterator &other) const {
|
||||
bool operator==(const JsonIterator &other) const {
|
||||
return _value._node == other._value._node;
|
||||
}
|
||||
|
||||
bool operator!=(const JsonArrayIterator &other) const {
|
||||
bool operator!=(const JsonIterator &other) const {
|
||||
return _value._node != other._value._node;
|
||||
}
|
||||
|
||||
JsonArrayIterator &operator++() {
|
||||
JsonIterator &operator++() {
|
||||
_value._node = _value._node->next;
|
||||
return *this;
|
||||
}
|
||||
|
||||
private:
|
||||
JsonValue _value;
|
||||
T _value;
|
||||
};
|
||||
|
||||
class JsonArrayConstIterator {
|
||||
template <typename T>
|
||||
class JsonConstIterator {
|
||||
friend class JsonArray;
|
||||
|
||||
public:
|
||||
explicit JsonArrayConstIterator(Internals::JsonNode *node) : _value(node) {}
|
||||
explicit JsonConstIterator(Internals::JsonNode *node) : _value(node) {}
|
||||
|
||||
const JsonValue operator*() const { return _value; }
|
||||
const JsonValue *operator->() { return &_value; }
|
||||
|
||||
bool operator==(const JsonArrayConstIterator &other) const {
|
||||
bool operator==(const JsonConstIterator &other) const {
|
||||
return _value._node == other._value._node;
|
||||
}
|
||||
|
||||
bool operator!=(const JsonArrayConstIterator &other) const {
|
||||
bool operator!=(const JsonConstIterator &other) const {
|
||||
return _value._node != other._value._node;
|
||||
}
|
||||
|
||||
JsonArrayConstIterator &operator++() {
|
||||
JsonConstIterator &operator++() {
|
||||
_value._node = _value._node->next;
|
||||
return *this;
|
||||
}
|
@ -7,11 +7,15 @@
|
||||
#pragma once
|
||||
|
||||
#include "JsonContainer.hpp"
|
||||
#include "JsonObjectIterator.hpp"
|
||||
#include "JsonIterator.hpp"
|
||||
#include "JsonObjectKeyValue.hpp"
|
||||
|
||||
namespace ArduinoJson {
|
||||
class JsonObject : public JsonContainer {
|
||||
public:
|
||||
typedef JsonIterator<JsonObjectKeyValue> iterator;
|
||||
typedef JsonConstIterator<JsonObjectKeyValue> const_iterator;
|
||||
|
||||
JsonObject() {}
|
||||
|
||||
explicit JsonObject(Internals::JsonNode *node) : JsonContainer(node) {}
|
||||
@ -24,9 +28,11 @@ class JsonObject : public JsonContainer {
|
||||
|
||||
bool success() { return _node && _node->isObject(); }
|
||||
|
||||
JsonObjectIterator begin() { return JsonObjectIterator(firstChild()); }
|
||||
iterator begin() { return iterator(firstChild()); }
|
||||
iterator end() { return iterator(0); }
|
||||
|
||||
JsonObjectIterator end() { return JsonObjectIterator(0); }
|
||||
const_iterator begin() const { return const_iterator(firstChild()); }
|
||||
const_iterator end() const { return const_iterator(0); }
|
||||
|
||||
private:
|
||||
Internals::JsonNode *getOrCreateNodeAt(const char *key);
|
||||
|
@ -1,39 +0,0 @@
|
||||
// Copyright Benoit Blanchon 2014
|
||||
// MIT License
|
||||
//
|
||||
// Arduino JSON library
|
||||
// https://github.com/bblanchon/ArduinoJson
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "ForwardDeclarations.hpp"
|
||||
#include "JsonObjectKeyValue.hpp"
|
||||
|
||||
namespace ArduinoJson {
|
||||
|
||||
class JsonObjectIterator {
|
||||
friend class JsonObject;
|
||||
|
||||
public:
|
||||
JsonObjectKeyValue operator*() const { return _keyValue; }
|
||||
JsonObjectKeyValue *operator->() { return &_keyValue; }
|
||||
|
||||
bool operator==(const JsonObjectIterator &other) const {
|
||||
return _keyValue._node == other._keyValue._node;
|
||||
}
|
||||
|
||||
bool operator!=(const JsonObjectIterator &other) const {
|
||||
return _keyValue._node != other._keyValue._node;
|
||||
}
|
||||
|
||||
JsonObjectIterator &operator++() {
|
||||
_keyValue._node = _keyValue._node->next;
|
||||
return *this;
|
||||
}
|
||||
|
||||
private:
|
||||
explicit JsonObjectIterator(Internals::JsonNode *node) : _keyValue(node) {}
|
||||
|
||||
JsonObjectKeyValue _keyValue;
|
||||
};
|
||||
}
|
@ -12,7 +12,8 @@
|
||||
namespace ArduinoJson {
|
||||
class JsonObjectKeyValue {
|
||||
friend class JsonObject;
|
||||
friend class JsonObjectIterator;
|
||||
template <typename T>
|
||||
friend class JsonIterator;
|
||||
|
||||
public:
|
||||
const char *key() const { return _node->getAsObjectKey(); }
|
||||
|
@ -13,8 +13,10 @@ namespace ArduinoJson {
|
||||
|
||||
class JsonValue : public Internals::JsonNodeWrapper {
|
||||
friend class JsonArray;
|
||||
friend class JsonArrayIterator;
|
||||
friend class JsonArrayConstIterator;
|
||||
template <typename T>
|
||||
friend class JsonIterator;
|
||||
template <typename T>
|
||||
friend class JsonConstIterator;
|
||||
friend class JsonBuffer;
|
||||
friend class JsonObject;
|
||||
friend class JsonObjectKeyValue;
|
||||
|
@ -22,7 +22,7 @@ JsonValue JsonObject::operator[](char const *key) {
|
||||
}
|
||||
|
||||
void JsonObject::remove(char const *key) {
|
||||
for (JsonObjectIterator it = begin(); it != end(); ++it) {
|
||||
for (iterator it = begin(); it != end(); ++it) {
|
||||
if (!strcmp(it->key(), key)) {
|
||||
removeChild(it->_node);
|
||||
}
|
||||
@ -46,7 +46,7 @@ JsonObject JsonObject::createNestedObject(char const *key) {
|
||||
}
|
||||
|
||||
JsonNode *JsonObject::getOrCreateNodeAt(const char *key) {
|
||||
for (JsonObjectIterator it = begin(); it != end(); ++it) {
|
||||
for (iterator it = begin(); it != end(); ++it) {
|
||||
if (!strcmp(it->key(), key)) return it->value()._node;
|
||||
}
|
||||
|
||||
|
@ -17,8 +17,8 @@ TEST(JsonArray_Iterator_Test, SimpleTest) {
|
||||
array.add(12);
|
||||
array.add(34);
|
||||
|
||||
JsonArrayIterator it = array.begin();
|
||||
JsonArrayIterator end = array.end();
|
||||
JsonArray::iterator it = array.begin();
|
||||
JsonArray::iterator end = array.end();
|
||||
|
||||
EXPECT_NE(end, it);
|
||||
EXPECT_EQ(12, it->as<int>());
|
||||
|
@ -17,8 +17,8 @@ TEST(JsonObject_Iterator_Test, SimpleTest) {
|
||||
object["ab"] = 12;
|
||||
object["cd"] = 34;
|
||||
|
||||
JsonObjectIterator it = object.begin();
|
||||
JsonObjectIterator end = object.end();
|
||||
JsonObject::iterator it = object.begin();
|
||||
JsonObject::iterator end = object.end();
|
||||
|
||||
EXPECT_NE(end, it);
|
||||
EXPECT_STREQ("ab", it->key());
|
||||
|
@ -7,6 +7,7 @@
|
||||
#include <gtest/gtest.h>
|
||||
#include <ArduinoJson/StaticJsonBuffer.hpp>
|
||||
#include <ArduinoJson/JsonValue.hpp>
|
||||
#include "Printers.hpp"
|
||||
|
||||
using namespace ArduinoJson;
|
||||
|
||||
|
Reference in New Issue
Block a user