[209] | 1 | // Copyright Benoit Blanchon 2014-2016
|
---|
| 2 | // MIT License
|
---|
| 3 | //
|
---|
| 4 | // Arduino JSON library
|
---|
| 5 | // https://github.com/bblanchon/ArduinoJson
|
---|
| 6 | // If you like this project, please add a star!
|
---|
| 7 |
|
---|
| 8 | #pragma once
|
---|
| 9 |
|
---|
| 10 | #include "Internals/JsonBufferAllocated.hpp"
|
---|
| 11 | #include "Internals/JsonPrintable.hpp"
|
---|
| 12 | #include "Internals/List.hpp"
|
---|
| 13 | #include "Internals/ReferenceType.hpp"
|
---|
| 14 | #include "JsonVariant.hpp"
|
---|
| 15 | #include "TypeTraits/EnableIf.hpp"
|
---|
| 16 | #include "TypeTraits/IsFloatingPoint.hpp"
|
---|
| 17 | #include "TypeTraits/IsReference.hpp"
|
---|
| 18 | #include "TypeTraits/IsSame.hpp"
|
---|
| 19 |
|
---|
| 20 | // Returns the size (in bytes) of an array with n elements.
|
---|
| 21 | // Can be very handy to determine the size of a StaticJsonBuffer.
|
---|
| 22 | #define JSON_ARRAY_SIZE(NUMBER_OF_ELEMENTS) \
|
---|
| 23 | (sizeof(JsonArray) + (NUMBER_OF_ELEMENTS) * sizeof(JsonArray::node_type))
|
---|
| 24 |
|
---|
| 25 | namespace ArduinoJson {
|
---|
| 26 |
|
---|
| 27 | // Forward declarations
|
---|
| 28 | class JsonObject;
|
---|
| 29 | class JsonBuffer;
|
---|
| 30 | class JsonArraySubscript;
|
---|
| 31 |
|
---|
| 32 | // An array of JsonVariant.
|
---|
| 33 | //
|
---|
| 34 | // The constructor is private, instances must be created via
|
---|
| 35 | // JsonBuffer::createArray() or JsonBuffer::parseArray().
|
---|
| 36 | // A JsonArray can be serialized to a JSON string via JsonArray::printTo().
|
---|
| 37 | // It can also be deserialized from a JSON string via JsonBuffer::parseArray().
|
---|
| 38 | class JsonArray : public Internals::JsonPrintable<JsonArray>,
|
---|
| 39 | public Internals::ReferenceType,
|
---|
| 40 | public Internals::List<JsonVariant>,
|
---|
| 41 | public Internals::JsonBufferAllocated {
|
---|
| 42 | public:
|
---|
| 43 | // A meta-function that returns true if type T can be used in
|
---|
| 44 | // JsonArray::set()
|
---|
| 45 | template <typename T>
|
---|
| 46 | struct CanSet {
|
---|
| 47 | static const bool value = JsonVariant::IsConstructibleFrom<T>::value ||
|
---|
| 48 | TypeTraits::IsSame<T, String &>::value ||
|
---|
| 49 | TypeTraits::IsSame<T, const String &>::value;
|
---|
| 50 | };
|
---|
| 51 |
|
---|
| 52 | // Create an empty JsonArray attached to the specified JsonBuffer.
|
---|
| 53 | // You should not call this constructor directly.
|
---|
| 54 | // Instead, use JsonBuffer::createArray() or JsonBuffer::parseArray().
|
---|
| 55 | explicit JsonArray(JsonBuffer *buffer)
|
---|
| 56 | : Internals::List<JsonVariant>(buffer) {}
|
---|
| 57 |
|
---|
| 58 | // Gets the value at the specified index
|
---|
| 59 | FORCE_INLINE JsonVariant operator[](size_t index) const;
|
---|
| 60 |
|
---|
| 61 | // Gets or sets the value at specified index
|
---|
| 62 | FORCE_INLINE JsonArraySubscript operator[](size_t index);
|
---|
| 63 |
|
---|
| 64 | // Adds the specified value at the end of the array.
|
---|
| 65 | //
|
---|
| 66 | // bool add(bool);
|
---|
| 67 | // bool add(char);
|
---|
| 68 | // bool add(long);
|
---|
| 69 | // bool add(int);
|
---|
| 70 | // bool add(short);
|
---|
| 71 | // bool add(float value);
|
---|
| 72 | // bool add(double value);
|
---|
| 73 | // bool add(const char*);
|
---|
| 74 | template <typename T>
|
---|
| 75 | FORCE_INLINE bool add(
|
---|
| 76 | T value,
|
---|
| 77 | typename TypeTraits::EnableIf<
|
---|
| 78 | CanSet<T>::value && !TypeTraits::IsReference<T>::value>::type * = 0) {
|
---|
| 79 | return addNode<T>(value);
|
---|
| 80 | }
|
---|
| 81 | // bool add(const String&)
|
---|
| 82 | // bool add(const JsonVariant&);
|
---|
| 83 | // bool add(JsonArray&);
|
---|
| 84 | // bool add(JsonObject&);
|
---|
| 85 | template <typename T>
|
---|
| 86 | FORCE_INLINE bool add(
|
---|
| 87 | const T &value,
|
---|
| 88 | typename TypeTraits::EnableIf<CanSet<T &>::value>::type * = 0) {
|
---|
| 89 | return addNode<T &>(const_cast<T &>(value));
|
---|
| 90 | }
|
---|
| 91 | // bool add(float value, uint8_t decimals);
|
---|
| 92 | // bool add(double value, uint8_t decimals);
|
---|
| 93 | template <typename T>
|
---|
| 94 | FORCE_INLINE bool add(
|
---|
| 95 | T value, uint8_t decimals,
|
---|
| 96 | typename TypeTraits::EnableIf<TypeTraits::IsFloatingPoint<T>::value>::type
|
---|
| 97 | * = 0) {
|
---|
| 98 | return addNode<JsonVariant>(JsonVariant(value, decimals));
|
---|
| 99 | }
|
---|
| 100 |
|
---|
| 101 | // Sets the value at specified index.
|
---|
| 102 | //
|
---|
| 103 | // bool set(size_t index, bool value);
|
---|
| 104 | // bool set(size_t index, long value);
|
---|
| 105 | // bool set(size_t index, int value);
|
---|
| 106 | // bool set(size_t index, short value);
|
---|
| 107 | template <typename T>
|
---|
| 108 | FORCE_INLINE bool set(
|
---|
| 109 | size_t index, T value,
|
---|
| 110 | typename TypeTraits::EnableIf<
|
---|
| 111 | CanSet<T>::value && !TypeTraits::IsReference<T>::value>::type * = 0) {
|
---|
| 112 | return setNodeAt<T>(index, value);
|
---|
| 113 | }
|
---|
| 114 | // bool set(size_t index, const String&)
|
---|
| 115 | // bool set(size_t index, const JsonVariant&);
|
---|
| 116 | // bool set(size_t index, JsonArray&);
|
---|
| 117 | // bool set(size_t index, JsonObject&);
|
---|
| 118 | template <typename T>
|
---|
| 119 | FORCE_INLINE bool set(
|
---|
| 120 | size_t index, const T &value,
|
---|
| 121 | typename TypeTraits::EnableIf<CanSet<T &>::value>::type * = 0) {
|
---|
| 122 | return setNodeAt<T &>(index, const_cast<T &>(value));
|
---|
| 123 | }
|
---|
| 124 | // bool set(size_t index, float value, uint8_t decimals = 2);
|
---|
| 125 | // bool set(size_t index, double value, uint8_t decimals = 2);
|
---|
| 126 | template <typename T>
|
---|
| 127 | FORCE_INLINE bool set(
|
---|
| 128 | size_t index, T value, uint8_t decimals,
|
---|
| 129 | typename TypeTraits::EnableIf<TypeTraits::IsFloatingPoint<T>::value>::type
|
---|
| 130 | * = 0) {
|
---|
| 131 | return setNodeAt<const JsonVariant &>(index, JsonVariant(value, decimals));
|
---|
| 132 | }
|
---|
| 133 |
|
---|
| 134 | // Gets the value at the specified index.
|
---|
| 135 | FORCE_INLINE JsonVariant get(size_t index) const;
|
---|
| 136 |
|
---|
| 137 | // Gets the value at the specified index.
|
---|
| 138 | template <typename T>
|
---|
| 139 | FORCE_INLINE T get(size_t index) const;
|
---|
| 140 |
|
---|
| 141 | // Check the type of the value at specified index.
|
---|
| 142 | template <typename T>
|
---|
| 143 | FORCE_INLINE bool is(size_t index) const;
|
---|
| 144 |
|
---|
| 145 | // Creates a JsonArray and adds a reference at the end of the array.
|
---|
| 146 | // It's a shortcut for JsonBuffer::createArray() and JsonArray::add()
|
---|
| 147 | JsonArray &createNestedArray();
|
---|
| 148 |
|
---|
| 149 | // Creates a JsonObject and adds a reference at the end of the array.
|
---|
| 150 | // It's a shortcut for JsonBuffer::createObject() and JsonArray::add()
|
---|
| 151 | JsonObject &createNestedObject();
|
---|
| 152 |
|
---|
| 153 | // Removes element at specified index.
|
---|
| 154 | void removeAt(size_t index);
|
---|
| 155 |
|
---|
| 156 | // Returns a reference an invalid JsonArray.
|
---|
| 157 | // This object is meant to replace a NULL pointer.
|
---|
| 158 | // This is used when memory allocation or JSON parsing fail.
|
---|
| 159 | static JsonArray &invalid() { return _invalid; }
|
---|
| 160 |
|
---|
| 161 | // Serialize the array to the specified JsonWriter.
|
---|
| 162 | void writeTo(Internals::JsonWriter &writer) const;
|
---|
| 163 |
|
---|
| 164 | private:
|
---|
| 165 | node_type *getNodeAt(size_t index) const;
|
---|
| 166 |
|
---|
| 167 | template <typename TValue>
|
---|
| 168 | bool setNodeAt(size_t index, TValue value);
|
---|
| 169 |
|
---|
| 170 | template <typename TValue>
|
---|
| 171 | bool addNode(TValue);
|
---|
| 172 |
|
---|
| 173 | template <typename T>
|
---|
| 174 | FORCE_INLINE bool setNodeValue(node_type *, T value);
|
---|
| 175 |
|
---|
| 176 | // The instance returned by JsonArray::invalid()
|
---|
| 177 | static JsonArray _invalid;
|
---|
| 178 | };
|
---|
| 179 | }
|
---|
| 180 |
|
---|
| 181 | #include "JsonArray.ipp"
|
---|