Files
ArduinoJson/src/JsonObject.cpp

130 lines
3.4 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
2014-11-03 18:35:22 +01:00
#include "../include/ArduinoJson/JsonObject.hpp"
2014-10-29 14:24:34 +01:00
#include <string.h> // for strcmp
2014-11-03 18:35:22 +01:00
#include "../include/ArduinoJson/JsonBuffer.hpp"
#include "../include/ArduinoJson/JsonArray.hpp"
2014-11-04 09:51:25 +01:00
#include "../include/ArduinoJson/JsonVariant.hpp"
2014-11-03 18:35:22 +01:00
#include "../include/ArduinoJson/Internals/PrettyJsonWriter.hpp"
#include "../include/ArduinoJson/Internals/StringBuilder.hpp"
#include "../include/ArduinoJson/Internals/PlacementNew.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-30 14:03:33 +01:00
JsonObject JsonObject::_invalid(NULL);
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-11-04 09:51:25 +01:00
JsonVariant &JsonObject::at(const char *key) {
JsonObjectNode *node = getNodeAt(key);
2014-11-04 09:51:25 +01:00
return node ? node->pair.value : JsonVariant::invalid();
}
2014-11-04 09:51:25 +01:00
const JsonVariant &JsonObject::at(const char *key) const {
JsonObjectNode *node = getNodeAt(key);
2014-11-04 09:51:25 +01:00
return node ? node->pair.value : JsonVariant::invalid();
}
2014-11-04 09:51:25 +01:00
JsonVariant &JsonObject::operator[](const char *key) {
2014-10-29 14:24:34 +01:00
JsonObjectNode *node = getOrCreateNodeAt(key);
2014-11-04 09:51:25 +01:00
return node ? node->pair.value : JsonVariant::invalid();
2014-10-29 14:24:34 +01:00
}
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) const {
2014-10-29 14:24:34 +01:00
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;
2014-10-30 14:03:33 +01:00
JsonObjectNode *newNode = createNode(key);
2014-10-29 14:24:34 +01:00
if (newNode) addNode(newNode);
return newNode;
}
2014-10-30 14:03:33 +01:00
JsonObjectNode *JsonObject::createNode(const char *key) {
if (!_buffer) return NULL;
void *ptr = _buffer->alloc(sizeof(JsonObjectNode));
return ptr ? new (ptr) JsonObjectNode(key) : NULL;
}
2014-10-29 14:24:34 +01:00
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) {
2014-10-30 21:51:59 +01:00
if (!nodeToRemove) return;
2014-10-29 14:24:34 +01:00
if (nodeToRemove == _firstNode) {
_firstNode = nodeToRemove->next;
} else {
for (JsonObjectNode *node = _firstNode; node; node = node->next)
if (node->next == nodeToRemove) node->next = nodeToRemove->next;
}
}
2014-11-03 12:32:47 +01:00
template <typename T>
void JsonObject::writeTo(T &writer) const {
2014-10-29 14:24:34 +01:00
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
}
2014-11-03 12:32:47 +01:00
2014-11-03 12:58:52 +01:00
template void JsonObject::writeTo(JsonWriter &writer) const;
2014-11-03 12:32:47 +01:00
template void JsonObject::writeTo(PrettyJsonWriter &writer) const;