2014-10-23 23:39:22 +02:00
|
|
|
// Copyright Benoit Blanchon 2014
|
|
|
|
// MIT License
|
|
|
|
//
|
|
|
|
// Arduino JSON library
|
|
|
|
// https://github.com/bblanchon/ArduinoJson
|
|
|
|
|
2014-10-19 15:46:36 +02:00
|
|
|
#include "ArduinoJson/JsonObject.hpp"
|
2014-10-29 14:24:34 +01:00
|
|
|
|
|
|
|
#include <string.h> // for strcmp
|
|
|
|
|
|
|
|
#include "ArduinoJson/JsonBuffer.hpp"
|
|
|
|
#include "ArduinoJson/JsonArray.hpp"
|
2014-10-27 22:50:50 +01:00
|
|
|
#include "ArduinoJson/JsonValue.hpp"
|
2014-10-29 14:24:34 +01:00
|
|
|
#include "ArduinoJson/Internals/JsonWriter.hpp"
|
|
|
|
#include "ArduinoJson/Internals/StringBuilder.hpp"
|
2014-10-16 16:25:42 +02:00
|
|
|
|
2014-10-18 23:05:54 +02:00
|
|
|
using namespace ArduinoJson;
|
2014-10-29 14:24:34 +01:00
|
|
|
using namespace ArduinoJson::Internals;
|
2014-10-16 16:25:42 +02:00
|
|
|
|
2014-10-29 14:24:34 +01:00
|
|
|
int JsonObject::size() const {
|
|
|
|
int nodeCount = 0;
|
|
|
|
for (JsonObjectNode *node = _firstNode; node; node = node->next) nodeCount++;
|
|
|
|
return nodeCount;
|
2014-10-16 16:25:42 +02:00
|
|
|
}
|
|
|
|
|
2014-10-29 14:24:34 +01:00
|
|
|
JsonValue &JsonObject::operator[](const char *key) {
|
|
|
|
JsonObjectNode *node = getOrCreateNodeAt(key);
|
|
|
|
return node ? node->pair.value : JsonValue::invalid();
|
|
|
|
}
|
|
|
|
|
|
|
|
void JsonObject::remove(char const *key) { removeNode(getNodeAt(key)); }
|
|
|
|
|
|
|
|
JsonArray &JsonObject::createNestedArray(char const *key) {
|
|
|
|
if (!_buffer) return JsonArray::invalid();
|
|
|
|
JsonArray &array = _buffer->createArray();
|
|
|
|
add(key, array);
|
|
|
|
return array;
|
2014-10-16 16:25:42 +02:00
|
|
|
}
|
|
|
|
|
2014-10-29 14:24:34 +01:00
|
|
|
JsonObject &JsonObject::createNestedObject(const char *key) {
|
|
|
|
if (!_buffer) return JsonObject::invalid();
|
|
|
|
JsonObject &object = _buffer->createObject();
|
|
|
|
add(key, object);
|
|
|
|
return object;
|
|
|
|
}
|
|
|
|
|
|
|
|
JsonObjectNode *JsonObject::getNodeAt(const char *key) {
|
|
|
|
for (JsonObjectNode *node = _firstNode; node; node = node->next) {
|
|
|
|
if (!strcmp(node->pair.key, key)) return node;
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
JsonObjectNode *JsonObject::getOrCreateNodeAt(const char *key) {
|
|
|
|
JsonObjectNode *existingNode = getNodeAt(key);
|
|
|
|
if (existingNode) return existingNode;
|
|
|
|
|
|
|
|
JsonObjectNode *newNode = JsonObjectNode::createFrom(_buffer, key);
|
|
|
|
|
|
|
|
if (newNode) addNode(newNode);
|
|
|
|
|
|
|
|
return newNode;
|
|
|
|
}
|
|
|
|
|
|
|
|
void JsonObject::addNode(JsonObjectNode *nodeToAdd) {
|
|
|
|
if (!_firstNode) {
|
|
|
|
_firstNode = nodeToAdd;
|
|
|
|
} else {
|
|
|
|
JsonObjectNode *lastNode = _firstNode;
|
|
|
|
while (lastNode->next) lastNode = lastNode->next;
|
|
|
|
lastNode->next = nodeToAdd;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void JsonObject::removeNode(JsonObjectNode *nodeToRemove) {
|
|
|
|
if (nodeToRemove == _firstNode) {
|
|
|
|
_firstNode = nodeToRemove->next;
|
|
|
|
} else {
|
|
|
|
for (JsonObjectNode *node = _firstNode; node; node = node->next)
|
|
|
|
if (node->next == nodeToRemove) node->next = nodeToRemove->next;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void JsonObject::writeTo(JsonWriter &writer) const {
|
|
|
|
JsonObjectNode *node = _firstNode;
|
|
|
|
|
|
|
|
if (node) {
|
|
|
|
writer.beginObject();
|
|
|
|
|
|
|
|
for (;;) {
|
|
|
|
writer.writeString(node->pair.key);
|
|
|
|
writer.writeColon();
|
|
|
|
node->pair.value.writeTo(writer);
|
|
|
|
|
|
|
|
node = node->next;
|
|
|
|
if (!node) break;
|
|
|
|
|
|
|
|
writer.writeComma();
|
|
|
|
}
|
|
|
|
|
|
|
|
writer.endObject();
|
|
|
|
} else {
|
|
|
|
writer.writeEmptyObject();
|
|
|
|
}
|
2014-10-22 21:56:38 +02:00
|
|
|
}
|