2016-01-07 22:35:12 +01:00
|
|
|
// Copyright Benoit Blanchon 2014-2016
|
2014-10-23 23:39:22 +02:00
|
|
|
// MIT License
|
|
|
|
//
|
|
|
|
// Arduino JSON library
|
|
|
|
// https://github.com/bblanchon/ArduinoJson
|
2016-01-07 22:35:12 +01:00
|
|
|
// 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
|
|
|
|
|
2015-08-10 17:22:22 +02:00
|
|
|
#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);
|
|
|
|
|
2015-05-25 15:38:58 +02:00
|
|
|
JsonObject::node_type *JsonObject::getOrCreateNodeAt(JsonObjectKey key) {
|
2015-05-23 15:32:50 +02:00
|
|
|
node_type *existingNode = getNodeAt(key);
|
|
|
|
if (existingNode) return existingNode;
|
2014-11-08 21:29:17 +01:00
|
|
|
|
2015-05-23 15:32:50 +02:00
|
|
|
node_type *newNode = addNewNode();
|
|
|
|
return newNode;
|
2014-10-29 14:24:34 +01:00
|
|
|
}
|
|
|
|
|
2015-07-25 15:38:12 +02: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();
|
2015-07-25 15:38:12 +02:00
|
|
|
setNodeAt<TKey, const JsonVariant &>(key, array);
|
2014-10-29 14:24:34 +01:00
|
|
|
return array;
|
2014-10-16 16:25:42 +02:00
|
|
|
}
|
2015-07-25 15:38:12 +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
|
|
|
|
2015-07-25 15:38:12 +02:00
|
|
|
template <typename TKey>
|
|
|
|
JsonObject &JsonObject::createObjectAt(TKey key) {
|
2014-10-29 14:24:34 +01:00
|
|
|
if (!_buffer) return JsonObject::invalid();
|
2015-07-25 15:38:12 +02:00
|
|
|
JsonObject &array = _buffer->createObject();
|
|
|
|
setNodeAt<TKey, const JsonVariant &>(key, array);
|
|
|
|
return array;
|
2014-10-29 14:24:34 +01:00
|
|
|
}
|
2015-07-25 15:38:12 +02: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
|
|
|
|
2015-05-25 15:38:58 +02:00
|
|
|
JsonObject::node_type *JsonObject::getNodeAt(JsonObjectKey key) const {
|
2014-11-05 11:09:48 +01:00
|
|
|
for (node_type *node = _firstNode; node; node = node->next) {
|
2014-11-05 09:24:15 +01:00
|
|
|
if (!strcmp(node->content.key, key)) return node;
|
2014-10-29 14:24:34 +01:00
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2014-11-08 19:40:07 +01:00
|
|
|
void JsonObject::writeTo(JsonWriter &writer) const {
|
2014-11-09 13:54:03 +01:00
|
|
|
writer.beginObject();
|
2014-10-29 14:24:34 +01:00
|
|
|
|
2014-11-09 13:54:03 +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
|
|
|
|
2014-11-09 13:54:03 +01:00
|
|
|
node = node->next;
|
|
|
|
if (!node) break;
|
2014-10-29 14:24:34 +01:00
|
|
|
|
2014-11-09 13:54:03 +01:00
|
|
|
writer.writeComma();
|
2014-10-29 14:24:34 +01:00
|
|
|
}
|
2014-11-09 13:54:03 +01:00
|
|
|
|
|
|
|
writer.endObject();
|
2014-10-22 21:56:38 +02:00
|
|
|
}
|