mirror of
https://github.com/bblanchon/ArduinoJson.git
synced 2025-07-15 11:36:36 +02:00
Added JsonArrayIterator
This commit is contained in:
@ -3,16 +3,13 @@
|
|||||||
namespace ArduinoJson
|
namespace ArduinoJson
|
||||||
{
|
{
|
||||||
class JsonBuffer;
|
class JsonBuffer;
|
||||||
|
|
||||||
namespace Internals
|
namespace Internals
|
||||||
{
|
{
|
||||||
class JsonWriter;
|
class JsonWriter;
|
||||||
class JsonNodeIterator;
|
|
||||||
|
|
||||||
class JsonNode
|
class JsonNode
|
||||||
{
|
{
|
||||||
friend class JsonNodeIterator;
|
|
||||||
|
|
||||||
enum JsonNodeType
|
enum JsonNodeType
|
||||||
{
|
{
|
||||||
JSON_UNDEFINED,
|
JSON_UNDEFINED,
|
||||||
@ -63,6 +60,8 @@ namespace ArduinoJson
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
JsonNode* next;
|
||||||
|
|
||||||
void writeTo(JsonWriter&); // TODO: <- move in JsonNodeSerializer
|
void writeTo(JsonWriter&); // TODO: <- move in JsonNodeSerializer
|
||||||
|
|
||||||
void setAsArray(JsonBuffer* buffer)
|
void setAsArray(JsonBuffer* buffer)
|
||||||
@ -175,7 +174,6 @@ namespace ArduinoJson
|
|||||||
|
|
||||||
private:
|
private:
|
||||||
JsonNodeType type;
|
JsonNodeType type;
|
||||||
JsonNode* next;
|
|
||||||
JsonNodeContent content;
|
JsonNodeContent content;
|
||||||
|
|
||||||
inline void writeArrayTo(JsonWriter&);// TODO: <- move in JsonNodeSerializer
|
inline void writeArrayTo(JsonWriter&);// TODO: <- move in JsonNodeSerializer
|
||||||
|
@ -6,6 +6,7 @@ namespace ArduinoJson
|
|||||||
{
|
{
|
||||||
namespace Internals
|
namespace Internals
|
||||||
{
|
{
|
||||||
|
// TODO: replace by JsonArrayIterator and JsonObjectIterator
|
||||||
class JsonNodeIterator
|
class JsonNodeIterator
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include "JsonContainer.hpp"
|
#include "JsonContainer.hpp"
|
||||||
|
#include "JsonArrayIterator.hpp"
|
||||||
|
|
||||||
namespace ArduinoJson
|
namespace ArduinoJson
|
||||||
{
|
{
|
||||||
@ -32,5 +33,12 @@ namespace ArduinoJson
|
|||||||
{
|
{
|
||||||
return _node && _node->isArray();
|
return _node && _node->isArray();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
JsonArrayIterator begin();
|
||||||
|
|
||||||
|
JsonArrayIterator end()
|
||||||
|
{
|
||||||
|
return JsonArrayIterator(0);
|
||||||
|
}
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
42
include/ArduinoJson/JsonArrayIterator.hpp
Normal file
42
include/ArduinoJson/JsonArrayIterator.hpp
Normal file
@ -0,0 +1,42 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include "ArduinoJson/JsonValue.hpp"
|
||||||
|
|
||||||
|
namespace ArduinoJson
|
||||||
|
{
|
||||||
|
class JsonArray;
|
||||||
|
|
||||||
|
class JsonArrayIterator
|
||||||
|
{
|
||||||
|
friend class JsonArray;
|
||||||
|
|
||||||
|
public:
|
||||||
|
explicit JsonArrayIterator(Internals::JsonNode* node)
|
||||||
|
: _node(node)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
void operator++()
|
||||||
|
{
|
||||||
|
_node = _node->next;
|
||||||
|
}
|
||||||
|
|
||||||
|
JsonValue operator*() const
|
||||||
|
{
|
||||||
|
return JsonValue(_node);
|
||||||
|
}
|
||||||
|
|
||||||
|
bool operator==(const JsonArrayIterator& other) const
|
||||||
|
{
|
||||||
|
return _node == other._node;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool operator!=(const JsonArrayIterator& other) const
|
||||||
|
{
|
||||||
|
return _node != other._node;
|
||||||
|
}
|
||||||
|
|
||||||
|
private:
|
||||||
|
Internals::JsonNode* _node;
|
||||||
|
};
|
||||||
|
}
|
@ -86,4 +86,12 @@ JsonObject JsonArray::createNestedObject()
|
|||||||
}
|
}
|
||||||
|
|
||||||
return JsonObject(node);
|
return JsonObject(node);
|
||||||
|
}
|
||||||
|
|
||||||
|
JsonArrayIterator JsonArray::begin()
|
||||||
|
{
|
||||||
|
if (!_node)
|
||||||
|
return end();
|
||||||
|
|
||||||
|
return JsonArrayIterator(_node->getContainerChild());
|
||||||
}
|
}
|
25
test/JsonArray_Iterator_Tests.cpp
Normal file
25
test/JsonArray_Iterator_Tests.cpp
Normal file
@ -0,0 +1,25 @@
|
|||||||
|
#include <gtest/gtest.h>
|
||||||
|
#include <ArduinoJson/JsonArray.hpp>
|
||||||
|
#include <ArduinoJson/StaticJsonBuffer.hpp>
|
||||||
|
|
||||||
|
using namespace ArduinoJson;
|
||||||
|
|
||||||
|
TEST(JsonArray_Iterator_Test, SimpleTest)
|
||||||
|
{
|
||||||
|
StaticJsonBuffer<42> jsonBuffer;
|
||||||
|
|
||||||
|
JsonArray array = jsonBuffer.createArray();
|
||||||
|
array.add(12);
|
||||||
|
array.add(34);
|
||||||
|
|
||||||
|
JsonArrayIterator it = array.begin();
|
||||||
|
JsonArrayIterator end = array.end();
|
||||||
|
|
||||||
|
EXPECT_NE(end, it);
|
||||||
|
EXPECT_EQ(12, static_cast<int>(*it));
|
||||||
|
++it;
|
||||||
|
EXPECT_NE(end, it);
|
||||||
|
EXPECT_EQ(34, static_cast<int>(*it));
|
||||||
|
++it;
|
||||||
|
EXPECT_EQ(array.end(), it);
|
||||||
|
}
|
Reference in New Issue
Block a user