forked from bblanchon/ArduinoJson
Extracting a common base class for JsonArray and JsonObject...
This commit is contained in:
@ -16,6 +16,7 @@ namespace Internals {
|
||||
template <typename T>
|
||||
class List {
|
||||
public:
|
||||
typedef T value_type;
|
||||
typedef Node<T> node_type;
|
||||
typedef NodeIterator<T> iterator;
|
||||
typedef NodeConstIterator<T> const_iterator;
|
||||
@ -23,6 +24,7 @@ class List {
|
||||
List(JsonBuffer *buffer) : _buffer(buffer), _firstNode(NULL) {}
|
||||
|
||||
bool success() const { return _buffer != NULL; }
|
||||
int size() const;
|
||||
|
||||
iterator begin() { return iterator(_firstNode); }
|
||||
iterator end() { return iterator(NULL); }
|
||||
|
@ -7,9 +7,7 @@
|
||||
#pragma once
|
||||
|
||||
#include "Internals/JsonPrintable.hpp"
|
||||
#include "Internals/Node.hpp"
|
||||
#include "Internals/NodeConstIterator.hpp"
|
||||
#include "Internals/NodeIterator.hpp"
|
||||
#include "Internals/List.hpp"
|
||||
#include "Internals/ReferenceType.hpp"
|
||||
#include "JsonVariant.hpp"
|
||||
|
||||
@ -22,19 +20,11 @@ class JsonObject;
|
||||
class JsonBuffer;
|
||||
|
||||
class JsonArray : public Internals::JsonPrintable<JsonArray>,
|
||||
public Internals::ReferenceType {
|
||||
public Internals::ReferenceType,
|
||||
public Internals::List<JsonVariant> {
|
||||
friend class JsonBuffer;
|
||||
|
||||
public:
|
||||
typedef JsonVariant value_type;
|
||||
typedef Internals::Node<JsonVariant> node_type;
|
||||
typedef Internals::NodeIterator<JsonVariant> iterator;
|
||||
typedef Internals::NodeConstIterator<JsonVariant> const_iterator;
|
||||
|
||||
int size() const;
|
||||
|
||||
bool success() { return _buffer != NULL; }
|
||||
|
||||
value_type &operator[](int index) const { return at(index); }
|
||||
value_type &at(int index) const;
|
||||
|
||||
@ -51,12 +41,6 @@ class JsonArray : public Internals::JsonPrintable<JsonArray>,
|
||||
JsonArray &createNestedArray();
|
||||
JsonObject &createNestedObject();
|
||||
|
||||
iterator begin() { return iterator(_firstNode); }
|
||||
iterator end() { return iterator(NULL); }
|
||||
|
||||
const_iterator begin() const { return const_iterator(_firstNode); }
|
||||
const_iterator end() const { return const_iterator(NULL); }
|
||||
|
||||
static JsonArray &invalid() { return _invalid; }
|
||||
|
||||
template <typename T>
|
||||
@ -64,13 +48,11 @@ class JsonArray : public Internals::JsonPrintable<JsonArray>,
|
||||
|
||||
private:
|
||||
// constructor is private: instance must be created via a JsonBuffer
|
||||
JsonArray(JsonBuffer *buffer) : _buffer(buffer), _firstNode(NULL) {}
|
||||
JsonArray(JsonBuffer *buffer) : List(buffer) {}
|
||||
|
||||
node_type *createNode();
|
||||
inline void addNode(node_type *node);
|
||||
|
||||
JsonBuffer *_buffer;
|
||||
node_type *_firstNode;
|
||||
static JsonArray _invalid;
|
||||
};
|
||||
}
|
||||
|
@ -8,7 +8,6 @@
|
||||
|
||||
#include "Internals/JsonPrintable.hpp"
|
||||
#include "Internals/List.hpp"
|
||||
#include "Internals/Node.hpp"
|
||||
#include "Internals/ReferenceType.hpp"
|
||||
#include "JsonPair.hpp"
|
||||
|
||||
@ -29,8 +28,6 @@ class JsonObject : public Internals::JsonPrintable<JsonObject>,
|
||||
typedef const char *key_type;
|
||||
typedef JsonPair value_type;
|
||||
|
||||
int size() const;
|
||||
|
||||
JsonVariant &at(key_type key);
|
||||
const JsonVariant &at(key_type key) const;
|
||||
JsonVariant &operator[](key_type key);
|
||||
|
23
src/Internals/List.cpp
Normal file
23
src/Internals/List.cpp
Normal file
@ -0,0 +1,23 @@
|
||||
// Copyright Benoit Blanchon 2014
|
||||
// MIT License
|
||||
//
|
||||
// Arduino JSON library
|
||||
// https://github.com/bblanchon/ArduinoJson
|
||||
|
||||
#include "../../include/ArduinoJson/Internals/List.hpp"
|
||||
|
||||
#include "../../include/ArduinoJson/JsonPair.hpp"
|
||||
#include "../../include/ArduinoJson/JsonVariant.hpp"
|
||||
|
||||
using namespace ArduinoJson;
|
||||
using namespace ArduinoJson::Internals;
|
||||
|
||||
template <typename T>
|
||||
int List<T>::size() const {
|
||||
int nodeCount = 0;
|
||||
for (node_type *node = _firstNode; node; node = node->next) nodeCount++;
|
||||
return nodeCount;
|
||||
}
|
||||
|
||||
template class List<JsonPair>;
|
||||
template class List<JsonVariant>;
|
@ -16,12 +16,6 @@ using namespace ArduinoJson::Internals;
|
||||
|
||||
JsonArray JsonArray::_invalid(NULL);
|
||||
|
||||
int JsonArray::size() const {
|
||||
int nodeCount = 0;
|
||||
for (node_type *node = _firstNode; node; node = node->next) nodeCount++;
|
||||
return nodeCount;
|
||||
}
|
||||
|
||||
JsonVariant &JsonArray::at(int index) const {
|
||||
node_type *node = _firstNode;
|
||||
while (node && index--) node = node->next;
|
||||
|
@ -19,12 +19,6 @@ using namespace ArduinoJson::Internals;
|
||||
|
||||
JsonObject JsonObject::_invalid(NULL);
|
||||
|
||||
int JsonObject::size() const {
|
||||
int nodeCount = 0;
|
||||
for (node_type *node = _firstNode; node; node = node->next) nodeCount++;
|
||||
return nodeCount;
|
||||
}
|
||||
|
||||
JsonVariant &JsonObject::at(const char *key) {
|
||||
node_type *node = getNodeAt(key);
|
||||
return node ? node->content.value : JsonVariant::invalid();
|
||||
|
Reference in New Issue
Block a user