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>
|
template <typename T>
|
||||||
class List {
|
class List {
|
||||||
public:
|
public:
|
||||||
|
typedef T value_type;
|
||||||
typedef Node<T> node_type;
|
typedef Node<T> node_type;
|
||||||
typedef NodeIterator<T> iterator;
|
typedef NodeIterator<T> iterator;
|
||||||
typedef NodeConstIterator<T> const_iterator;
|
typedef NodeConstIterator<T> const_iterator;
|
||||||
@ -23,6 +24,7 @@ class List {
|
|||||||
List(JsonBuffer *buffer) : _buffer(buffer), _firstNode(NULL) {}
|
List(JsonBuffer *buffer) : _buffer(buffer), _firstNode(NULL) {}
|
||||||
|
|
||||||
bool success() const { return _buffer != NULL; }
|
bool success() const { return _buffer != NULL; }
|
||||||
|
int size() const;
|
||||||
|
|
||||||
iterator begin() { return iterator(_firstNode); }
|
iterator begin() { return iterator(_firstNode); }
|
||||||
iterator end() { return iterator(NULL); }
|
iterator end() { return iterator(NULL); }
|
||||||
|
@ -7,9 +7,7 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include "Internals/JsonPrintable.hpp"
|
#include "Internals/JsonPrintable.hpp"
|
||||||
#include "Internals/Node.hpp"
|
#include "Internals/List.hpp"
|
||||||
#include "Internals/NodeConstIterator.hpp"
|
|
||||||
#include "Internals/NodeIterator.hpp"
|
|
||||||
#include "Internals/ReferenceType.hpp"
|
#include "Internals/ReferenceType.hpp"
|
||||||
#include "JsonVariant.hpp"
|
#include "JsonVariant.hpp"
|
||||||
|
|
||||||
@ -22,19 +20,11 @@ class JsonObject;
|
|||||||
class JsonBuffer;
|
class JsonBuffer;
|
||||||
|
|
||||||
class JsonArray : public Internals::JsonPrintable<JsonArray>,
|
class JsonArray : public Internals::JsonPrintable<JsonArray>,
|
||||||
public Internals::ReferenceType {
|
public Internals::ReferenceType,
|
||||||
|
public Internals::List<JsonVariant> {
|
||||||
friend class JsonBuffer;
|
friend class JsonBuffer;
|
||||||
|
|
||||||
public:
|
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 &operator[](int index) const { return at(index); }
|
||||||
value_type &at(int index) const;
|
value_type &at(int index) const;
|
||||||
|
|
||||||
@ -51,12 +41,6 @@ class JsonArray : public Internals::JsonPrintable<JsonArray>,
|
|||||||
JsonArray &createNestedArray();
|
JsonArray &createNestedArray();
|
||||||
JsonObject &createNestedObject();
|
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; }
|
static JsonArray &invalid() { return _invalid; }
|
||||||
|
|
||||||
template <typename T>
|
template <typename T>
|
||||||
@ -64,13 +48,11 @@ class JsonArray : public Internals::JsonPrintable<JsonArray>,
|
|||||||
|
|
||||||
private:
|
private:
|
||||||
// constructor is private: instance must be created via a JsonBuffer
|
// 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();
|
node_type *createNode();
|
||||||
inline void addNode(node_type *node);
|
inline void addNode(node_type *node);
|
||||||
|
|
||||||
JsonBuffer *_buffer;
|
|
||||||
node_type *_firstNode;
|
|
||||||
static JsonArray _invalid;
|
static JsonArray _invalid;
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
@ -8,7 +8,6 @@
|
|||||||
|
|
||||||
#include "Internals/JsonPrintable.hpp"
|
#include "Internals/JsonPrintable.hpp"
|
||||||
#include "Internals/List.hpp"
|
#include "Internals/List.hpp"
|
||||||
#include "Internals/Node.hpp"
|
|
||||||
#include "Internals/ReferenceType.hpp"
|
#include "Internals/ReferenceType.hpp"
|
||||||
#include "JsonPair.hpp"
|
#include "JsonPair.hpp"
|
||||||
|
|
||||||
@ -29,8 +28,6 @@ class JsonObject : public Internals::JsonPrintable<JsonObject>,
|
|||||||
typedef const char *key_type;
|
typedef const char *key_type;
|
||||||
typedef JsonPair value_type;
|
typedef JsonPair value_type;
|
||||||
|
|
||||||
int size() const;
|
|
||||||
|
|
||||||
JsonVariant &at(key_type key);
|
JsonVariant &at(key_type key);
|
||||||
const JsonVariant &at(key_type key) const;
|
const JsonVariant &at(key_type key) const;
|
||||||
JsonVariant &operator[](key_type key);
|
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);
|
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 {
|
JsonVariant &JsonArray::at(int index) const {
|
||||||
node_type *node = _firstNode;
|
node_type *node = _firstNode;
|
||||||
while (node && index--) node = node->next;
|
while (node && index--) node = node->next;
|
||||||
|
@ -19,12 +19,6 @@ using namespace ArduinoJson::Internals;
|
|||||||
|
|
||||||
JsonObject JsonObject::_invalid(NULL);
|
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) {
|
JsonVariant &JsonObject::at(const char *key) {
|
||||||
node_type *node = getNodeAt(key);
|
node_type *node = getNodeAt(key);
|
||||||
return node ? node->content.value : JsonVariant::invalid();
|
return node ? node->content.value : JsonVariant::invalid();
|
||||||
|
Reference in New Issue
Block a user