Files
ArduinoJson/src/JsonArray.cpp

85 lines
1.6 KiB
C++
Raw Normal View History

2014-10-23 23:39:22 +02:00
// Copyright Benoit Blanchon 2014
// MIT License
//
// Arduino JSON library
// https://github.com/bblanchon/ArduinoJson
#include "ArduinoJson/JsonArray.hpp"
#include "ArduinoJson/JsonObject.hpp"
#include "ArduinoJson/JsonValue.hpp"
2014-10-16 16:25:42 +02:00
2014-10-18 23:05:54 +02:00
using namespace ArduinoJson;
using namespace ArduinoJson::Internals;
2014-10-23 19:54:00 +02:00
JsonValue JsonArray::operator[](int index) const {
2014-10-24 18:31:50 +02:00
for (const_iterator it = begin(); it != end(); ++it) {
if (!index) return *it;
2014-10-23 19:54:00 +02:00
index--;
}
return JsonValue();
2014-10-16 16:25:42 +02:00
}
2014-10-23 19:54:00 +02:00
void JsonArray::add(bool value) {
JsonNode *node = createNode();
if (!node) return;
2014-10-16 16:25:42 +02:00
2014-10-23 19:54:00 +02:00
node->setAsBoolean(value);
addChild(node);
2014-10-16 16:25:42 +02:00
}
2014-10-23 19:54:00 +02:00
void JsonArray::add(char const *value) {
JsonNode *node = createNode();
if (!node) return;
2014-10-16 16:25:42 +02:00
2014-10-23 19:54:00 +02:00
node->setAsString(value);
addChild(node);
2014-10-16 16:25:42 +02:00
}
2014-10-23 19:54:00 +02:00
void JsonArray::add(double value, int decimals) {
JsonNode *node = createNode();
if (!node) return;
2014-10-16 16:25:42 +02:00
2014-10-23 19:54:00 +02:00
node->setAsDouble(value, decimals);
addChild(node);
2014-10-16 16:25:42 +02:00
}
2014-10-23 19:54:00 +02:00
void JsonArray::add(long value) {
JsonNode *node = createNode();
if (!node) return;
2014-10-16 16:25:42 +02:00
2014-10-23 19:54:00 +02:00
node->setAsLong(value);
addChild(node);
2014-10-16 16:25:42 +02:00
}
// TODO: we should have the same issue as in JsonValue
2014-10-23 19:54:00 +02:00
void JsonArray::add(JsonContainer nestedContainer) {
JsonNode *node = createNode();
if (!node) return;
2014-10-16 16:25:42 +02:00
2014-10-23 19:54:00 +02:00
node->duplicate(nestedContainer._node);
addChild(node);
2014-10-16 16:25:42 +02:00
}
2014-10-23 19:54:00 +02:00
JsonArray JsonArray::createNestedArray() {
JsonNode *node = createNode();
if (node) {
node->setAsArray(_node->getContainerBuffer());
addChild(node);
}
return JsonArray(node);
2014-10-16 16:25:42 +02:00
}
2014-10-23 19:54:00 +02:00
JsonObject JsonArray::createNestedObject() {
JsonNode *node = createNode();
if (node) {
node->setAsObject(_node->getContainerBuffer());
addChild(node);
}
return JsonObject(node);
2014-10-22 17:59:59 +02:00
}