2017-11-07 20:42:50 +01:00
|
|
|
// ArduinoJson - arduinojson.org
|
2018-01-05 09:20:01 +01:00
|
|
|
// Copyright Benoit Blanchon 2014-2018
|
2014-10-23 23:39:22 +02:00
|
|
|
// MIT License
|
|
|
|
|
2014-10-16 00:11:23 +02:00
|
|
|
#pragma once
|
|
|
|
|
2018-09-11 16:05:56 +02:00
|
|
|
#include "Data/JsonVariantData.hpp"
|
2018-08-21 17:56:16 +02:00
|
|
|
#include "JsonArrayIterator.hpp"
|
2014-10-16 00:11:23 +02:00
|
|
|
|
2018-09-11 16:05:56 +02:00
|
|
|
// Returns the size (in bytes) of an array with n elements.
|
|
|
|
// Can be very handy to determine the size of a StaticMemoryPool.
|
|
|
|
#define JSON_ARRAY_SIZE(NUMBER_OF_ELEMENTS) \
|
2018-10-02 16:54:05 +02:00
|
|
|
((NUMBER_OF_ELEMENTS) * sizeof(ARDUINOJSON_NAMESPACE::Slot))
|
2018-09-11 16:05:56 +02:00
|
|
|
|
2018-10-02 16:54:05 +02:00
|
|
|
namespace ARDUINOJSON_NAMESPACE {
|
2014-10-30 14:03:33 +01:00
|
|
|
|
2014-10-31 12:27:33 +01:00
|
|
|
class JsonObject;
|
2015-05-23 15:32:50 +02:00
|
|
|
class JsonArraySubscript;
|
2014-10-31 12:27:33 +01:00
|
|
|
|
2018-07-02 09:35:21 +02:00
|
|
|
class JsonArray {
|
|
|
|
friend class JsonVariant;
|
2014-11-06 16:29:29 +01:00
|
|
|
|
2018-07-02 09:35:21 +02:00
|
|
|
public:
|
2018-08-21 17:56:16 +02:00
|
|
|
typedef JsonArrayIterator iterator;
|
2014-10-23 19:54:00 +02:00
|
|
|
|
2018-09-03 16:14:21 +02:00
|
|
|
FORCE_INLINE JsonArray() : _memoryPool(0), _data(0) {}
|
2018-10-02 16:54:05 +02:00
|
|
|
FORCE_INLINE JsonArray(MemoryPool* pool, JsonArrayData* arr)
|
2018-10-01 12:55:40 +02:00
|
|
|
: _memoryPool(pool), _data(arr) {}
|
|
|
|
|
|
|
|
operator JsonVariant() {
|
|
|
|
return JsonVariant(_memoryPool, getVariantData(_data));
|
|
|
|
}
|
2014-11-06 14:48:14 +01:00
|
|
|
|
|
|
|
// Adds the specified value at the end of the array.
|
2016-02-14 16:18:13 +01:00
|
|
|
//
|
2017-01-15 15:11:26 +01:00
|
|
|
// bool add(TValue);
|
2018-07-12 09:08:20 +02:00
|
|
|
// TValue = bool, long, int, short, float, double, serialized, JsonVariant,
|
2018-09-11 16:05:56 +02:00
|
|
|
// std::string, String, JsonObject
|
2015-09-28 22:14:50 +02:00
|
|
|
template <typename T>
|
2018-08-31 16:29:08 +02:00
|
|
|
FORCE_INLINE bool add(const T& value) {
|
2018-09-11 16:05:56 +02:00
|
|
|
return add().set(value);
|
|
|
|
}
|
|
|
|
// Adds the specified value at the end of the array.
|
|
|
|
FORCE_INLINE bool add(JsonArray value) {
|
|
|
|
return add().set(value);
|
2016-02-14 16:18:13 +01:00
|
|
|
}
|
2017-01-15 15:11:26 +01:00
|
|
|
//
|
|
|
|
// bool add(TValue);
|
2018-01-14 13:46:28 +01:00
|
|
|
// TValue = char*, const char*, const FlashStringHelper*
|
2017-01-15 15:11:26 +01:00
|
|
|
template <typename T>
|
2018-08-31 16:29:08 +02:00
|
|
|
FORCE_INLINE bool add(T* value) {
|
2018-09-11 16:05:56 +02:00
|
|
|
return add().set(value);
|
|
|
|
}
|
|
|
|
|
|
|
|
JsonVariant add() {
|
|
|
|
if (!_data) return JsonVariant();
|
|
|
|
|
2018-10-02 16:54:05 +02:00
|
|
|
Slot* slot = new (_memoryPool) Slot();
|
2018-09-11 16:05:56 +02:00
|
|
|
if (!slot) return JsonVariant();
|
|
|
|
|
|
|
|
slot->next = 0;
|
|
|
|
|
|
|
|
if (_data->tail) {
|
|
|
|
slot->prev = _data->tail;
|
|
|
|
_data->tail->next = slot;
|
|
|
|
_data->tail = slot;
|
|
|
|
} else {
|
|
|
|
slot->prev = 0;
|
|
|
|
_data->head = slot;
|
|
|
|
_data->tail = slot;
|
|
|
|
}
|
|
|
|
|
|
|
|
return JsonVariant(_memoryPool, &slot->value);
|
2016-02-14 16:18:13 +01:00
|
|
|
}
|
2014-10-25 21:02:13 +02:00
|
|
|
|
2018-08-31 16:29:08 +02:00
|
|
|
FORCE_INLINE iterator begin() const {
|
2018-07-02 09:35:21 +02:00
|
|
|
if (!_data) return iterator();
|
2018-09-11 16:05:56 +02:00
|
|
|
return iterator(_memoryPool, _data->head);
|
2016-06-22 21:41:19 +02:00
|
|
|
}
|
2014-11-06 14:48:14 +01:00
|
|
|
|
2018-08-31 16:29:08 +02:00
|
|
|
FORCE_INLINE iterator end() const {
|
2018-07-02 09:35:21 +02:00
|
|
|
return iterator();
|
2016-06-22 21:41:19 +02:00
|
|
|
}
|
2014-11-06 14:48:14 +01:00
|
|
|
|
2016-04-14 20:06:38 +02:00
|
|
|
// Imports a 1D array
|
|
|
|
template <typename T, size_t N>
|
2018-08-31 16:29:08 +02:00
|
|
|
FORCE_INLINE bool copyFrom(T (&array)[N]) {
|
2016-04-14 20:06:38 +02:00
|
|
|
return copyFrom(array, N);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Imports a 1D array
|
|
|
|
template <typename T>
|
2018-07-02 09:35:21 +02:00
|
|
|
bool copyFrom(T* array, size_t len) {
|
2016-04-14 20:06:38 +02:00
|
|
|
bool ok = true;
|
|
|
|
for (size_t i = 0; i < len; i++) {
|
|
|
|
ok &= add(array[i]);
|
|
|
|
}
|
|
|
|
return ok;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Imports a 2D array
|
|
|
|
template <typename T, size_t N1, size_t N2>
|
2016-11-06 17:48:32 +01:00
|
|
|
bool copyFrom(T (&array)[N1][N2]) {
|
2016-04-14 20:06:38 +02:00
|
|
|
bool ok = true;
|
|
|
|
for (size_t i = 0; i < N1; i++) {
|
2018-07-02 09:35:21 +02:00
|
|
|
JsonArray nestedArray = createNestedArray();
|
2016-04-14 20:06:38 +02:00
|
|
|
for (size_t j = 0; j < N2; j++) {
|
|
|
|
ok &= nestedArray.add(array[i][j]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ok;
|
|
|
|
}
|
|
|
|
|
2018-09-11 16:05:56 +02:00
|
|
|
// Copy a JsonArray
|
|
|
|
bool copyFrom(JsonArray src) {
|
|
|
|
bool ok = _data != 0;
|
|
|
|
for (iterator it = src.begin(); it != src.end(); ++it) {
|
|
|
|
ok &= add(*it);
|
|
|
|
}
|
|
|
|
return ok;
|
|
|
|
}
|
|
|
|
|
2016-04-14 20:06:38 +02:00
|
|
|
// Exports a 1D array
|
|
|
|
template <typename T, size_t N>
|
2018-08-31 16:29:08 +02:00
|
|
|
FORCE_INLINE size_t copyTo(T (&array)[N]) const {
|
2016-04-14 20:06:38 +02:00
|
|
|
return copyTo(array, N);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Exports a 1D array
|
|
|
|
template <typename T>
|
2018-07-02 09:35:21 +02:00
|
|
|
size_t copyTo(T* array, size_t len) const {
|
2016-04-14 20:06:38 +02:00
|
|
|
size_t i = 0;
|
2018-08-21 17:56:16 +02:00
|
|
|
for (iterator it = begin(); it != end() && i < len; ++it) array[i++] = *it;
|
2016-04-14 20:06:38 +02:00
|
|
|
return i;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Exports a 2D array
|
|
|
|
template <typename T, size_t N1, size_t N2>
|
2016-11-06 17:48:32 +01:00
|
|
|
void copyTo(T (&array)[N1][N2]) const {
|
2018-07-02 09:35:21 +02:00
|
|
|
if (!_data) return;
|
2016-04-14 20:06:38 +02:00
|
|
|
size_t i = 0;
|
2018-08-21 17:56:16 +02:00
|
|
|
for (iterator it = begin(); it != end() && i < N1; ++it) {
|
2017-01-22 17:24:17 +01:00
|
|
|
it->as<JsonArray>().copyTo(array[i++]);
|
2016-04-14 20:06:38 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-31 16:29:08 +02:00
|
|
|
FORCE_INLINE JsonArray createNestedArray();
|
|
|
|
FORCE_INLINE JsonObject createNestedObject();
|
2018-07-02 09:35:21 +02:00
|
|
|
|
2018-10-02 16:54:05 +02:00
|
|
|
FORCE_INLINE JsonArraySubscript operator[](size_t index);
|
2018-07-02 09:35:21 +02:00
|
|
|
|
2018-10-02 16:54:05 +02:00
|
|
|
FORCE_INLINE const JsonArraySubscript operator[](size_t index) const;
|
2018-07-02 09:35:21 +02:00
|
|
|
|
2018-09-11 16:05:56 +02:00
|
|
|
FORCE_INLINE bool operator==(JsonArray rhs) const {
|
|
|
|
iterator it1 = begin();
|
|
|
|
iterator it2 = rhs.begin();
|
|
|
|
for (;;) {
|
|
|
|
if (it1 == end() && it2 == rhs.end()) return true;
|
|
|
|
if (it1 == end()) return false;
|
|
|
|
if (it2 == end()) return false;
|
|
|
|
if (*it1 != *it2) return false;
|
|
|
|
++it1;
|
|
|
|
++it2;
|
|
|
|
}
|
2018-07-02 09:35:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Gets the value at the specified index.
|
|
|
|
template <typename T>
|
2018-10-02 16:54:05 +02:00
|
|
|
FORCE_INLINE typename JsonVariantAs<T>::type get(size_t index) const {
|
2018-08-21 17:56:16 +02:00
|
|
|
iterator it = begin() += index;
|
|
|
|
return it != end() ? it->as<T>() : T();
|
2018-07-02 09:35:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Check the type of the value at specified index.
|
|
|
|
template <typename T>
|
2018-08-31 16:29:08 +02:00
|
|
|
FORCE_INLINE bool is(size_t index) const {
|
2018-08-21 17:56:16 +02:00
|
|
|
iterator it = begin() += index;
|
2018-07-02 09:35:21 +02:00
|
|
|
return it != end() ? it->is<T>() : false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Removes element at specified position.
|
2018-08-31 16:29:08 +02:00
|
|
|
FORCE_INLINE void remove(iterator it) {
|
2018-07-02 09:35:21 +02:00
|
|
|
if (!_data) return;
|
2018-09-11 16:05:56 +02:00
|
|
|
|
2018-10-02 16:54:05 +02:00
|
|
|
Slot* slot = it.internal();
|
2018-09-11 16:05:56 +02:00
|
|
|
if (!slot) return;
|
|
|
|
|
|
|
|
if (slot->prev)
|
|
|
|
slot->prev->next = slot->next;
|
|
|
|
else
|
|
|
|
_data->head = slot->next;
|
|
|
|
if (slot->next)
|
|
|
|
slot->next->prev = slot->prev;
|
|
|
|
else
|
|
|
|
_data->tail = slot->prev;
|
2018-07-02 09:35:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Removes element at specified index.
|
2018-08-31 16:29:08 +02:00
|
|
|
FORCE_INLINE void remove(size_t index) {
|
2018-07-02 09:35:21 +02:00
|
|
|
remove(begin() += index);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Sets the value at specified index.
|
|
|
|
//
|
|
|
|
// bool add(size_t index, const TValue&);
|
2018-07-12 09:08:20 +02:00
|
|
|
// TValue = bool, long, int, short, float, double, serialized, JsonVariant,
|
2018-07-02 09:35:21 +02:00
|
|
|
// std::string, String, JsonArrayData, JsonObject
|
|
|
|
template <typename T>
|
2018-08-31 16:29:08 +02:00
|
|
|
FORCE_INLINE bool set(size_t index, const T& value) {
|
2018-07-02 09:35:21 +02:00
|
|
|
if (!_data) return false;
|
|
|
|
return set_impl<const T&>(index, value);
|
|
|
|
}
|
|
|
|
//
|
|
|
|
// bool add(size_t index, TValue);
|
|
|
|
// TValue = char*, const char*, const FlashStringHelper*
|
|
|
|
template <typename T>
|
2018-08-31 16:29:08 +02:00
|
|
|
FORCE_INLINE bool set(size_t index, T* value) {
|
2018-07-02 09:35:21 +02:00
|
|
|
if (!_data) return false;
|
|
|
|
return set_impl<T*>(index, value);
|
|
|
|
}
|
2018-09-11 16:05:56 +02:00
|
|
|
// Sets the value at specified index.
|
|
|
|
//
|
|
|
|
// bool add(size_t index, JsonArray);
|
|
|
|
template <typename T>
|
|
|
|
FORCE_INLINE bool set(size_t index, JsonArray value) {
|
|
|
|
if (!_data) return false;
|
|
|
|
return get<JsonVariant>(index).set(value);
|
|
|
|
}
|
2018-07-02 09:35:21 +02:00
|
|
|
|
2018-08-31 16:29:08 +02:00
|
|
|
FORCE_INLINE size_t size() const {
|
2018-07-02 09:35:21 +02:00
|
|
|
if (!_data) return 0;
|
2018-10-02 16:54:05 +02:00
|
|
|
Slot* slot = _data->head;
|
2018-09-11 16:05:56 +02:00
|
|
|
size_t n = 0;
|
|
|
|
while (slot) {
|
|
|
|
slot = slot->next;
|
|
|
|
n++;
|
|
|
|
}
|
|
|
|
return n;
|
2018-07-02 09:35:21 +02:00
|
|
|
}
|
|
|
|
|
2018-08-31 16:29:08 +02:00
|
|
|
FORCE_INLINE bool isNull() const {
|
2018-07-02 09:35:21 +02:00
|
|
|
return _data == 0;
|
|
|
|
}
|
|
|
|
|
2018-04-17 21:27:45 +02:00
|
|
|
template <typename Visitor>
|
2018-09-11 16:05:56 +02:00
|
|
|
FORCE_INLINE void accept(Visitor& visitor) const {
|
2018-07-02 09:35:21 +02:00
|
|
|
if (_data)
|
2018-09-11 16:05:56 +02:00
|
|
|
visitor.visitArray(*this);
|
2018-07-02 09:35:21 +02:00
|
|
|
else
|
2018-09-11 16:05:56 +02:00
|
|
|
visitor.visitNull();
|
2018-04-17 21:27:45 +02:00
|
|
|
}
|
|
|
|
|
2014-10-26 21:18:09 +01:00
|
|
|
private:
|
2016-11-06 17:48:32 +01:00
|
|
|
template <typename TValueRef>
|
2018-08-31 16:29:08 +02:00
|
|
|
FORCE_INLINE bool set_impl(size_t index, TValueRef value) {
|
2017-04-12 21:00:13 +02:00
|
|
|
iterator it = begin() += index;
|
|
|
|
if (it == end()) return false;
|
2018-08-21 17:56:16 +02:00
|
|
|
return it->set(value);
|
2016-06-22 21:41:19 +02:00
|
|
|
}
|
2015-07-25 15:38:12 +02:00
|
|
|
|
2016-11-06 17:48:32 +01:00
|
|
|
template <typename TValueRef>
|
2018-08-31 16:29:08 +02:00
|
|
|
FORCE_INLINE bool add_impl(TValueRef value) {
|
2018-09-11 16:05:56 +02:00
|
|
|
return add().set(value);
|
2016-06-22 21:41:19 +02:00
|
|
|
}
|
2016-12-03 22:19:11 +01:00
|
|
|
|
2018-10-02 16:54:05 +02:00
|
|
|
MemoryPool* _memoryPool;
|
|
|
|
JsonArrayData* _data;
|
2016-12-03 22:19:11 +01:00
|
|
|
};
|
2018-10-02 16:54:05 +02:00
|
|
|
} // namespace ARDUINOJSON_NAMESPACE
|