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
|
|
|
|
|
2016-12-21 22:09:13 +01:00
|
|
|
#include "Data/List.hpp"
|
|
|
|
#include "Data/ReferenceType.hpp"
|
2018-01-14 13:46:28 +01:00
|
|
|
#include "Data/ValueSaver.hpp"
|
2014-11-05 11:09:48 +01:00
|
|
|
#include "JsonVariant.hpp"
|
2018-03-15 09:56:00 +01:00
|
|
|
#include "Memory/JsonBufferAllocated.hpp"
|
2018-05-17 13:46:23 +02:00
|
|
|
#include "Polyfills/type_traits.hpp"
|
2018-04-10 17:43:27 +02:00
|
|
|
#include "Strings/StringTraits.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;
|
2018-01-19 08:32:15 +01:00
|
|
|
namespace Internals {
|
2015-05-23 15:32:50 +02:00
|
|
|
class JsonArraySubscript;
|
2018-01-19 08:32:15 +01:00
|
|
|
}
|
2014-10-31 12:27:33 +01:00
|
|
|
|
2018-03-01 09:24:58 +01:00
|
|
|
class JsonArray : public Internals::ReferenceType,
|
2017-06-17 14:17:01 +02:00
|
|
|
public Internals::NonCopyable,
|
2015-02-01 20:59:31 +01:00
|
|
|
public Internals::List<JsonVariant>,
|
|
|
|
public Internals::JsonBufferAllocated {
|
2014-10-23 23:13:13 +02:00
|
|
|
public:
|
2018-03-15 09:56:00 +01:00
|
|
|
explicit JsonArray(Internals::JsonBuffer *buf) throw()
|
2018-02-26 16:05:16 +01:00
|
|
|
: Internals::List<JsonVariant>(buf) {}
|
2014-11-06 16:29:29 +01:00
|
|
|
|
2015-05-23 15:32:50 +02:00
|
|
|
// Gets the value at the specified index
|
2018-01-19 08:32:15 +01:00
|
|
|
const Internals::JsonArraySubscript operator[](size_t index) const;
|
2014-10-23 19:54:00 +02:00
|
|
|
|
2015-05-23 15:32:50 +02:00
|
|
|
// Gets or sets the value at specified index
|
2018-01-19 08:32:15 +01:00
|
|
|
Internals::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
|
|
|
//
|
2017-01-15 15:11:26 +01:00
|
|
|
// bool add(TValue);
|
|
|
|
// TValue = bool, long, int, short, float, double, RawJson, JsonVariant,
|
2018-01-14 13:46:28 +01:00
|
|
|
// std::string, String, JsonArray, JsonObject
|
2015-09-28 22:14:50 +02:00
|
|
|
template <typename T>
|
2018-01-14 13:46:28 +01:00
|
|
|
bool add(const T &value) {
|
2017-01-15 15:11:26 +01:00
|
|
|
return add_impl<const T &>(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-01-14 13:46:28 +01:00
|
|
|
bool add(T *value) {
|
|
|
|
return add_impl<T *>(value);
|
2017-01-15 15:11:26 +01:00
|
|
|
}
|
|
|
|
//
|
|
|
|
// bool add(TValue value, uint8_t decimals);
|
|
|
|
// TValue = float, double
|
2016-02-14 16:18:13 +01:00
|
|
|
template <typename T>
|
2017-05-20 09:06:53 +02:00
|
|
|
DEPRECATED("Second argument is not supported anymore")
|
|
|
|
bool add(T value, uint8_t) {
|
|
|
|
return add_impl<const JsonVariant &>(JsonVariant(value));
|
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
|
|
|
//
|
2018-01-14 13:46:28 +01:00
|
|
|
// bool add(size_t index, const TValue&);
|
2017-01-15 15:11:26 +01:00
|
|
|
// TValue = bool, long, int, short, float, double, RawJson, JsonVariant,
|
2018-01-14 13:46:28 +01:00
|
|
|
// std::string, String, JsonArray, JsonObject
|
2016-02-14 16:18:13 +01:00
|
|
|
template <typename T>
|
2018-01-14 13:46:28 +01:00
|
|
|
bool set(size_t index, const T &value) {
|
2017-01-15 15:11:26 +01:00
|
|
|
return set_impl<const T &>(index, value);
|
2016-02-14 16:18:13 +01:00
|
|
|
}
|
2017-01-15 15:11:26 +01:00
|
|
|
//
|
|
|
|
// bool add(size_t index, 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-01-14 13:46:28 +01:00
|
|
|
bool set(size_t index, T *value) {
|
|
|
|
return set_impl<T *>(index, value);
|
2017-01-15 15:11:26 +01:00
|
|
|
}
|
|
|
|
//
|
|
|
|
// bool set(size_t index, TValue value, uint8_t decimals);
|
|
|
|
// TValue = float, double
|
2015-09-28 22:14:50 +02:00
|
|
|
template <typename T>
|
2018-05-17 13:46:23 +02:00
|
|
|
typename Internals::enable_if<Internals::is_floating_point<T>::value,
|
|
|
|
bool>::type
|
2016-11-06 17:48:32 +01:00
|
|
|
set(size_t index, T value, uint8_t decimals) {
|
2017-01-15 15:11:26 +01:00
|
|
|
return set_impl<const JsonVariant &>(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 {
|
2017-04-12 21:00:13 +02:00
|
|
|
const_iterator it = begin() += index;
|
|
|
|
return it != end() ? it->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 {
|
2017-04-12 21:00:13 +02:00
|
|
|
const_iterator it = begin() += index;
|
|
|
|
return it != end() ? it->is<T>() : false;
|
2016-06-22 21:41:19 +02:00
|
|
|
}
|
2014-11-06 14:48:14 +01:00
|
|
|
|
|
|
|
// Creates a JsonArray and adds a reference at the end of the array.
|
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.
|
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.
|
2017-04-12 17:03:33 +02:00
|
|
|
void remove(size_t index) {
|
2017-04-12 21:00:13 +02:00
|
|
|
remove(begin() += index);
|
2016-06-22 21:41:19 +02:00
|
|
|
}
|
2017-04-12 21:00:13 +02:00
|
|
|
using Internals::List<JsonVariant>::remove;
|
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) {
|
2017-01-22 17:24:17 +01:00
|
|
|
it->as<JsonArray>().copyTo(array[i++]);
|
2016-04-14 20:06:38 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-17 21:27:45 +02:00
|
|
|
template <typename Visitor>
|
2018-05-29 08:31:17 +02:00
|
|
|
void visit(Visitor &visitor) const {
|
2018-04-17 21:27:45 +02:00
|
|
|
return visitor.acceptArray(*this);
|
|
|
|
}
|
|
|
|
|
2014-10-26 21:18:09 +01:00
|
|
|
private:
|
2016-11-06 17:48:32 +01:00
|
|
|
template <typename TValueRef>
|
2017-01-15 15:11:26 +01:00
|
|
|
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-01-14 13:46:28 +01:00
|
|
|
return Internals::ValueSaver<TValueRef>::save(_buffer, *it, 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>
|
2017-01-15 15:11:26 +01:00
|
|
|
bool add_impl(TValueRef value) {
|
2017-04-12 21:00:13 +02:00
|
|
|
iterator it = Internals::List<JsonVariant>::add();
|
|
|
|
if (it == end()) return false;
|
2018-01-14 13:46:28 +01:00
|
|
|
return Internals::ValueSaver<TValueRef>::save(_buffer, *it, 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();
|
|
|
|
}
|
|
|
|
};
|
2018-02-26 16:05:16 +01:00
|
|
|
} // namespace Internals
|
|
|
|
} // namespace ArduinoJson
|