Files
ArduinoJson/src/JsonObject.cpp

73 lines
2.0 KiB
C++
Raw Normal View History

// Copyright Benoit Blanchon 2014-2016
2014-10-23 23:39:22 +02:00
// MIT License
//
// Arduino JSON library
// https://github.com/bblanchon/ArduinoJson
// If you like this project, please add a star!
2014-10-23 23:39:22 +02:00
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
#include "../include/ArduinoJson/Internals/StaticStringBuilder.hpp"
2014-11-04 10:01:21 +01:00
#include "../include/ArduinoJson/JsonArray.hpp"
#include "../include/ArduinoJson/JsonBuffer.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);
JsonObject::node_type *JsonObject::getOrCreateNodeAt(JsonObjectKey key) {
node_type *existingNode = getNodeAt(key);
if (existingNode) return existingNode;
node_type *newNode = addNewNode();
return newNode;
2014-10-29 14:24:34 +01:00
}
template <typename TKey>
JsonArray &JsonObject::createArrayAt(TKey key) {
2014-10-29 14:24:34 +01:00
if (!_buffer) return JsonArray::invalid();
JsonArray &array = _buffer->createArray();
setNodeAt<TKey, const JsonVariant &>(key, array);
2014-10-29 14:24:34 +01:00
return array;
2014-10-16 16:25:42 +02:00
}
template JsonArray &JsonObject::createArrayAt<const char *>(const char *);
template JsonArray &JsonObject::createArrayAt<const String &>(const String &);
2014-10-16 16:25:42 +02:00
template <typename TKey>
JsonObject &JsonObject::createObjectAt(TKey key) {
2014-10-29 14:24:34 +01:00
if (!_buffer) return JsonObject::invalid();
JsonObject &array = _buffer->createObject();
setNodeAt<TKey, const JsonVariant &>(key, array);
return array;
2014-10-29 14:24:34 +01:00
}
template JsonObject &JsonObject::createObjectAt<const char *>(const char *);
template JsonObject &JsonObject::createObjectAt<const String &>(const String &);
2014-10-29 14:24:34 +01:00
JsonObject::node_type *JsonObject::getNodeAt(JsonObjectKey key) const {
for (node_type *node = _firstNode; node; node = node->next) {
if (!strcmp(node->content.key, key)) return node;
2014-10-29 14:24:34 +01:00
}
return NULL;
}
void JsonObject::writeTo(JsonWriter &writer) const {
writer.beginObject();
2014-10-29 14:24:34 +01:00
const node_type *node = _firstNode;
while (node) {
writer.writeString(node->content.key);
writer.writeColon();
node->content.value.writeTo(writer);
2014-10-29 14:24:34 +01:00
node = node->next;
if (!node) break;
2014-10-29 14:24:34 +01:00
writer.writeComma();
2014-10-29 14:24:34 +01:00
}
writer.endObject();
2014-10-22 21:56:38 +02:00
}