2016-01-07 22:35:12 +01:00
|
|
|
// Copyright Benoit Blanchon 2014-2016
|
2014-10-23 23:39:22 +02:00
|
|
|
// MIT License
|
|
|
|
//
|
|
|
|
// Arduino JSON library
|
|
|
|
// https://github.com/bblanchon/ArduinoJson
|
2016-01-07 22:35:12 +01:00
|
|
|
// If you like this project, please add a star!
|
2014-10-23 23:39:22 +02:00
|
|
|
|
2014-10-16 00:11:23 +02:00
|
|
|
#pragma once
|
|
|
|
|
2015-02-01 20:59:31 +01:00
|
|
|
#include "Internals/JsonBufferAllocated.hpp"
|
2014-11-05 08:53:41 +01:00
|
|
|
#include "Internals/JsonPrintable.hpp"
|
2014-11-05 11:38:36 +01:00
|
|
|
#include "Internals/List.hpp"
|
2014-10-31 12:27:33 +01:00
|
|
|
#include "Internals/ReferenceType.hpp"
|
2016-11-06 17:48:32 +01:00
|
|
|
#include "Internals/StringFuncs.hpp"
|
|
|
|
#include "Internals/ValueSetter.hpp"
|
2014-11-05 11:09:48 +01:00
|
|
|
#include "JsonVariant.hpp"
|
2016-11-06 17:48:32 +01:00
|
|
|
#include "TypeTraits/ConstRefOrConstPtr.hpp"
|
2016-02-14 16:18:13 +01:00
|
|
|
#include "TypeTraits/EnableIf.hpp"
|
|
|
|
#include "TypeTraits/IsFloatingPoint.hpp"
|
|
|
|
#include "TypeTraits/IsSame.hpp"
|
2014-10-30 21:51:59 +01:00
|
|
|
|
2014-11-06 14:48:14 +01:00
|
|
|
// Returns the size (in bytes) of an array with n elements.
|
|
|
|
// Can be very handy to determine the size of a StaticJsonBuffer.
|
2014-10-30 21:51:59 +01:00
|
|
|
#define JSON_ARRAY_SIZE(NUMBER_OF_ELEMENTS) \
|
2014-11-05 11:09:48 +01:00
|
|
|
(sizeof(JsonArray) + (NUMBER_OF_ELEMENTS) * sizeof(JsonArray::node_type))
|
2014-10-16 00:11:23 +02:00
|
|
|
|
2014-10-23 19:54:00 +02:00
|
|
|
namespace ArduinoJson {
|
2014-10-30 14:03:33 +01:00
|
|
|
|
2014-11-06 16:29:29 +01:00
|
|
|
// Forward declarations
|
2014-10-31 12:27:33 +01:00
|
|
|
class JsonObject;
|
|
|
|
class JsonBuffer;
|
2015-05-23 15:32:50 +02:00
|
|
|
class JsonArraySubscript;
|
2014-10-31 12:27:33 +01:00
|
|
|
|
2014-11-06 14:48:14 +01:00
|
|
|
// An array of JsonVariant.
|
2014-11-06 16:29:29 +01:00
|
|
|
//
|
|
|
|
// The constructor is private, instances must be created via
|
|
|
|
// JsonBuffer::createArray() or JsonBuffer::parseArray().
|
|
|
|
// A JsonArray can be serialized to a JSON string via JsonArray::printTo().
|
|
|
|
// It can also be deserialized from a JSON string via JsonBuffer::parseArray().
|
2014-11-05 08:53:41 +01:00
|
|
|
class JsonArray : public Internals::JsonPrintable<JsonArray>,
|
2014-11-05 11:38:36 +01:00
|
|
|
public Internals::ReferenceType,
|
2015-02-01 20:59:31 +01:00
|
|
|
public Internals::List<JsonVariant>,
|
|
|
|
public Internals::JsonBufferAllocated {
|
2014-10-23 23:13:13 +02:00
|
|
|
public:
|
2015-05-23 15:32:50 +02:00
|
|
|
// Create an empty JsonArray attached to the specified JsonBuffer.
|
|
|
|
// You should not call this constructor directly.
|
|
|
|
// Instead, use JsonBuffer::createArray() or JsonBuffer::parseArray().
|
|
|
|
explicit JsonArray(JsonBuffer *buffer)
|
|
|
|
: Internals::List<JsonVariant>(buffer) {}
|
2014-11-06 16:29:29 +01:00
|
|
|
|
2015-05-23 15:32:50 +02:00
|
|
|
// Gets the value at the specified index
|
2016-06-22 21:41:19 +02:00
|
|
|
JsonVariant operator[](size_t index) const {
|
2016-11-06 17:48:32 +01:00
|
|
|
return get<JsonVariant>(index);
|
2016-06-22 21:41:19 +02:00
|
|
|
}
|
2014-10-23 19:54:00 +02:00
|
|
|
|
2015-05-23 15:32:50 +02:00
|
|
|
// Gets or sets the value at specified index
|
2016-06-22 21:41:19 +02:00
|
|
|
JsonArraySubscript operator[](size_t index);
|
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
|
|
|
//
|
|
|
|
// bool add(bool);
|
|
|
|
// bool add(char);
|
|
|
|
// bool add(long);
|
|
|
|
// bool add(int);
|
|
|
|
// bool add(short);
|
|
|
|
// bool add(float value);
|
|
|
|
// bool add(double value);
|
|
|
|
// bool add(const char*);
|
2016-11-06 17:48:32 +01:00
|
|
|
// bool add(const char[]);
|
|
|
|
// bool add(const char[N]);
|
|
|
|
// bool add(RawJson);
|
|
|
|
// bool add(const std::string&)
|
2016-02-14 16:18:13 +01:00
|
|
|
// bool add(const String&)
|
|
|
|
// bool add(const JsonVariant&);
|
|
|
|
// bool add(JsonArray&);
|
|
|
|
// bool add(JsonObject&);
|
2015-09-28 22:14:50 +02:00
|
|
|
template <typename T>
|
2016-11-06 17:48:32 +01:00
|
|
|
bool add(const T &value) {
|
|
|
|
// reduce the number of template function instanciation to reduce code size
|
|
|
|
return addNodeImpl<typename TypeTraits::ConstRefOrConstPtr<T>::type>(value);
|
2016-02-14 16:18:13 +01:00
|
|
|
}
|
|
|
|
// bool add(float value, uint8_t decimals);
|
|
|
|
// bool add(double value, uint8_t decimals);
|
|
|
|
template <typename T>
|
2016-11-06 17:48:32 +01:00
|
|
|
bool add(T value, uint8_t decimals) {
|
|
|
|
return add(JsonVariant(value, decimals));
|
2016-02-14 16:18:13 +01:00
|
|
|
}
|
2014-10-25 21:02:13 +02:00
|
|
|
|
2015-05-23 15:32:50 +02:00
|
|
|
// Sets the value at specified index.
|
2016-02-14 16:18:13 +01:00
|
|
|
//
|
|
|
|
// bool set(size_t index, bool value);
|
|
|
|
// bool set(size_t index, long value);
|
|
|
|
// bool set(size_t index, int value);
|
|
|
|
// bool set(size_t index, short value);
|
2016-11-06 17:48:32 +01:00
|
|
|
// bool set(size_t index, const std::string&)
|
2016-02-14 16:18:13 +01:00
|
|
|
// bool set(size_t index, const String&)
|
|
|
|
// bool set(size_t index, const JsonVariant&);
|
|
|
|
// bool set(size_t index, JsonArray&);
|
|
|
|
// bool set(size_t index, JsonObject&);
|
|
|
|
template <typename T>
|
2016-11-06 17:48:32 +01:00
|
|
|
bool set(size_t index, const T &value) {
|
|
|
|
// reduce the number of template function instanciation to reduce code size
|
|
|
|
return setNodeAt<typename TypeTraits::ConstRefOrConstPtr<T>::type>(index,
|
|
|
|
value);
|
2016-02-14 16:18:13 +01:00
|
|
|
}
|
|
|
|
// bool set(size_t index, float value, uint8_t decimals = 2);
|
|
|
|
// bool set(size_t index, double value, uint8_t decimals = 2);
|
2015-09-28 22:14:50 +02:00
|
|
|
template <typename T>
|
2016-11-06 17:48:32 +01:00
|
|
|
typename TypeTraits::EnableIf<TypeTraits::IsFloatingPoint<T>::value,
|
|
|
|
bool>::type
|
|
|
|
set(size_t index, T value, uint8_t decimals) {
|
|
|
|
return set(index, JsonVariant(value, decimals));
|
2016-06-22 21:41:19 +02:00
|
|
|
}
|
2014-11-06 14:48:14 +01:00
|
|
|
|
2015-05-23 15:32:50 +02:00
|
|
|
// Gets the value at the specified index.
|
|
|
|
template <typename T>
|
2016-07-17 17:22:58 +02:00
|
|
|
typename Internals::JsonVariantAs<T>::type get(size_t index) const {
|
2016-06-22 21:41:19 +02:00
|
|
|
node_type *node = getNodeAt(index);
|
2016-11-06 17:48:32 +01:00
|
|
|
return node ? node->content.as<T>()
|
|
|
|
: Internals::JsonVariantDefault<T>::get();
|
|
|
|
;
|
2016-06-22 21:41:19 +02:00
|
|
|
}
|
2015-05-23 15:32:50 +02:00
|
|
|
|
|
|
|
// Check the type of the value at specified index.
|
|
|
|
template <typename T>
|
2016-06-22 21:41:19 +02:00
|
|
|
bool is(size_t index) const {
|
|
|
|
node_type *node = getNodeAt(index);
|
|
|
|
return node ? node->content.is<T>() : false;
|
|
|
|
}
|
2014-11-06 14:48:14 +01:00
|
|
|
|
|
|
|
// Creates a JsonArray and adds a reference at the end of the array.
|
|
|
|
// It's a shortcut for JsonBuffer::createArray() and JsonArray::add()
|
2014-10-29 14:24:34 +01:00
|
|
|
JsonArray &createNestedArray();
|
2014-11-06 14:48:14 +01:00
|
|
|
|
|
|
|
// Creates a JsonObject and adds a reference at the end of the array.
|
|
|
|
// It's a shortcut for JsonBuffer::createObject() and JsonArray::add()
|
2014-10-29 14:24:34 +01:00
|
|
|
JsonObject &createNestedObject();
|
2014-10-23 19:54:00 +02:00
|
|
|
|
2015-05-02 15:16:18 +02:00
|
|
|
// Removes element at specified index.
|
2016-06-22 21:41:19 +02:00
|
|
|
void removeAt(size_t index) {
|
|
|
|
removeNode(getNodeAt(index));
|
|
|
|
}
|
2015-05-02 15:16:18 +02:00
|
|
|
|
2014-11-06 14:48:14 +01:00
|
|
|
// Returns a reference an invalid JsonArray.
|
2014-11-06 16:29:29 +01:00
|
|
|
// This object is meant to replace a NULL pointer.
|
2014-11-06 14:48:14 +01:00
|
|
|
// This is used when memory allocation or JSON parsing fail.
|
2016-06-22 21:41:19 +02:00
|
|
|
static JsonArray &invalid() {
|
|
|
|
static JsonArray instance(NULL);
|
|
|
|
return instance;
|
|
|
|
}
|
2014-10-27 22:50:50 +01:00
|
|
|
|
2016-04-14 20:06:38 +02:00
|
|
|
// Imports a 1D array
|
|
|
|
template <typename T, size_t N>
|
2016-11-06 17:48:32 +01:00
|
|
|
bool copyFrom(T (&array)[N]) {
|
2016-04-14 20:06:38 +02:00
|
|
|
return copyFrom(array, N);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Imports a 1D array
|
|
|
|
template <typename T>
|
|
|
|
bool copyFrom(T *array, size_t len) {
|
|
|
|
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++) {
|
|
|
|
JsonArray &nestedArray = createNestedArray();
|
|
|
|
for (size_t j = 0; j < N2; j++) {
|
|
|
|
ok &= nestedArray.add(array[i][j]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ok;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Exports a 1D array
|
|
|
|
template <typename T, size_t N>
|
2016-11-06 17:48:32 +01:00
|
|
|
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>
|
|
|
|
size_t copyTo(T *array, size_t len) const {
|
|
|
|
size_t i = 0;
|
|
|
|
for (const_iterator it = begin(); it != end() && i < len; ++it)
|
|
|
|
array[i++] = *it;
|
|
|
|
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 {
|
2016-04-14 20:06:38 +02:00
|
|
|
size_t i = 0;
|
|
|
|
for (const_iterator it = begin(); it != end() && i < N1; ++it) {
|
|
|
|
it->asArray().copyTo(array[i++]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-10-26 21:18:09 +01:00
|
|
|
private:
|
2016-06-22 21:41:19 +02:00
|
|
|
node_type *getNodeAt(size_t index) const {
|
|
|
|
node_type *node = _firstNode;
|
|
|
|
while (node && index--) node = node->next;
|
|
|
|
return node;
|
|
|
|
}
|
2015-05-02 15:16:18 +02:00
|
|
|
|
2016-11-06 17:48:32 +01:00
|
|
|
template <typename TValueRef>
|
|
|
|
bool setNodeAt(size_t index, TValueRef value) {
|
2016-06-22 21:41:19 +02:00
|
|
|
node_type *node = getNodeAt(index);
|
2016-11-06 17:48:32 +01:00
|
|
|
if (!node) return false;
|
|
|
|
|
|
|
|
return Internals::ValueSetter<TValueRef>::set(_buffer, node->content,
|
|
|
|
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>
|
|
|
|
bool addNodeImpl(TValueRef value) {
|
2016-06-22 21:41:19 +02:00
|
|
|
node_type *node = addNewNode();
|
2016-11-06 17:48:32 +01:00
|
|
|
if (!node) return false;
|
2015-07-25 15:38:12 +02:00
|
|
|
|
2016-11-06 17:48:32 +01:00
|
|
|
return Internals::ValueSetter<TValueRef>::set(_buffer, node->content,
|
|
|
|
value);
|
2016-06-22 21:41:19 +02:00
|
|
|
}
|
2014-10-23 19:54:00 +02:00
|
|
|
};
|
2016-12-03 22:19:11 +01:00
|
|
|
|
|
|
|
namespace Internals {
|
|
|
|
template <>
|
|
|
|
struct JsonVariantDefault<JsonArray> {
|
|
|
|
static JsonArray &get() {
|
|
|
|
return JsonArray::invalid();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
2014-10-18 23:05:54 +02:00
|
|
|
}
|