[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 "JsonArray.hpp"
|
---|
| 11 | #include "JsonObject.hpp"
|
---|
| 12 | #include "JsonArraySubscript.hpp"
|
---|
| 13 |
|
---|
| 14 | namespace ArduinoJson {
|
---|
| 15 |
|
---|
| 16 | inline JsonArraySubscript JsonArray::operator[](size_t index) {
|
---|
| 17 | return JsonArraySubscript(*this, index);
|
---|
| 18 | }
|
---|
| 19 |
|
---|
| 20 | inline JsonVariant JsonArray::operator[](size_t index) const {
|
---|
| 21 | return get(index);
|
---|
| 22 | }
|
---|
| 23 |
|
---|
| 24 | template <typename TValue>
|
---|
| 25 | inline bool JsonArray::addNode(TValue value) {
|
---|
| 26 | node_type *node = addNewNode();
|
---|
| 27 | return node != NULL && setNodeValue<TValue>(node, value);
|
---|
| 28 | }
|
---|
| 29 |
|
---|
| 30 | template <typename TValue>
|
---|
| 31 | inline bool JsonArray::setNodeAt(size_t index, TValue value) {
|
---|
| 32 | node_type *node = getNodeAt(index);
|
---|
| 33 | return node != NULL && setNodeValue<TValue>(node, value);
|
---|
| 34 | }
|
---|
| 35 |
|
---|
| 36 | template <typename TValue>
|
---|
| 37 | inline bool JsonArray::setNodeValue(node_type *node, TValue value) {
|
---|
| 38 | node->content = value;
|
---|
| 39 | return true;
|
---|
| 40 | }
|
---|
| 41 |
|
---|
| 42 | template <>
|
---|
| 43 | inline bool JsonArray::setNodeValue(node_type *node, String &value) {
|
---|
| 44 | const char *copy = _buffer->strdup(value);
|
---|
| 45 | if (!copy) return false;
|
---|
| 46 | node->content = copy;
|
---|
| 47 | return true;
|
---|
| 48 | }
|
---|
| 49 |
|
---|
| 50 | inline JsonVariant JsonArray::get(size_t index) const {
|
---|
| 51 | node_type *node = getNodeAt(index);
|
---|
| 52 | return node ? node->content : JsonVariant();
|
---|
| 53 | }
|
---|
| 54 |
|
---|
| 55 | template <typename T>
|
---|
| 56 | inline T JsonArray::get(size_t index) const {
|
---|
| 57 | node_type *node = getNodeAt(index);
|
---|
| 58 | return node ? node->content.as<T>() : JsonVariant::invalid<T>();
|
---|
| 59 | }
|
---|
| 60 |
|
---|
| 61 | template <typename T>
|
---|
| 62 | inline bool JsonArray::is(size_t index) const {
|
---|
| 63 | node_type *node = getNodeAt(index);
|
---|
| 64 | return node ? node->content.is<T>() : false;
|
---|
| 65 | }
|
---|
| 66 |
|
---|
| 67 | template <typename TImplem>
|
---|
| 68 | inline const JsonArraySubscript JsonVariantBase<TImplem>::operator[](
|
---|
| 69 | int index) const {
|
---|
| 70 | return asArray()[index];
|
---|
| 71 | }
|
---|
| 72 |
|
---|
| 73 | template <>
|
---|
| 74 | inline JsonArray &JsonVariant::invalid<JsonArray &>() {
|
---|
| 75 | return JsonArray::invalid();
|
---|
| 76 | }
|
---|
| 77 |
|
---|
| 78 | template <>
|
---|
| 79 | inline JsonArray const &JsonVariant::invalid<JsonArray const &>() {
|
---|
| 80 | return JsonArray::invalid();
|
---|
| 81 | }
|
---|
| 82 |
|
---|
| 83 | inline JsonArray &JsonVariant::asArray() const {
|
---|
| 84 | if (_type == Internals::JSON_ARRAY) return *_content.asArray;
|
---|
| 85 | return JsonArray::invalid();
|
---|
| 86 | }
|
---|
| 87 |
|
---|
| 88 | inline JsonArray &JsonArray::createNestedArray() {
|
---|
| 89 | if (!_buffer) return JsonArray::invalid();
|
---|
| 90 | JsonArray &array = _buffer->createArray();
|
---|
| 91 | add(array);
|
---|
| 92 | return array;
|
---|
| 93 | }
|
---|
| 94 |
|
---|
| 95 | inline JsonArray &JsonObject::createNestedArray(JsonObjectKey key) {
|
---|
| 96 | if (!_buffer) return JsonArray::invalid();
|
---|
| 97 | JsonArray &array = _buffer->createArray();
|
---|
| 98 | setNodeAt<const JsonVariant &>(key, array);
|
---|
| 99 | return array;
|
---|
| 100 | }
|
---|
| 101 | }
|
---|