2014-09-27 11:53:26 +02:00
|
|
|
#include "JsonObject.h"
|
2014-09-30 16:40:00 +02:00
|
|
|
|
|
|
|
#include <string.h> // for strcmp
|
|
|
|
|
|
|
|
#include "JsonBuffer.h"
|
2014-09-27 11:53:26 +02:00
|
|
|
#include "JsonValue.h"
|
2014-10-01 16:08:32 +02:00
|
|
|
#include "Internals/EscapedString.h"
|
|
|
|
#include "Internals/JsonNode.h"
|
|
|
|
#include "Internals/StringBuilder.h"
|
2014-09-30 16:40:00 +02:00
|
|
|
|
|
|
|
using namespace ArduinoJson::Internals;
|
2014-09-27 11:53:26 +02:00
|
|
|
|
|
|
|
JsonValue JsonObject::operator[](char const* key)
|
|
|
|
{
|
2014-09-27 14:43:19 +02:00
|
|
|
JsonNode* node = getOrCreateNodeAt(key);
|
2014-09-27 14:51:50 +02:00
|
|
|
return JsonValue(node);
|
2014-09-27 14:43:19 +02:00
|
|
|
}
|
|
|
|
|
2014-09-30 17:05:33 +02:00
|
|
|
void JsonObject::remove(char const* key)
|
|
|
|
{
|
|
|
|
JsonNode* lastChild = 0;
|
|
|
|
|
2014-10-01 16:56:22 +02:00
|
|
|
for (JsonNodeIterator it = beginChildren(); it != endChildren(); ++it)
|
2014-09-30 17:05:33 +02:00
|
|
|
{
|
2014-10-01 16:56:22 +02:00
|
|
|
const char* childKey = it->content.asKey.key;
|
2014-09-30 17:05:33 +02:00
|
|
|
|
|
|
|
if (!strcmp(childKey, key))
|
|
|
|
{
|
2014-10-01 16:56:22 +02:00
|
|
|
removeChildAfter(*it, lastChild);
|
|
|
|
}
|
2014-09-30 17:05:33 +02:00
|
|
|
|
2014-10-01 16:56:22 +02:00
|
|
|
lastChild = *it;
|
|
|
|
}
|
2014-09-28 21:04:59 +02:00
|
|
|
}
|
|
|
|
|
2014-10-07 12:11:10 +02:00
|
|
|
JsonArray JsonObject::createNestedArray(char const* key)
|
2014-10-07 11:58:59 +02:00
|
|
|
{
|
2014-10-07 12:11:10 +02:00
|
|
|
JsonNode* node = createContainerNodeAt(key, JSON_ARRAY);
|
|
|
|
return JsonArray(node);
|
|
|
|
}
|
2014-10-07 11:58:59 +02:00
|
|
|
|
2014-10-07 12:11:10 +02:00
|
|
|
JsonObject JsonObject::createNestedObject(char const* key)
|
|
|
|
{
|
|
|
|
JsonNode* node = createContainerNodeAt(key, JSON_OBJECT);
|
2014-10-07 11:58:59 +02:00
|
|
|
return JsonObject(node);
|
|
|
|
}
|
|
|
|
|
2014-10-01 16:56:22 +02:00
|
|
|
JsonNode* JsonObject::getOrCreateNodeAt(const char* key)
|
2014-09-27 14:43:19 +02:00
|
|
|
{
|
2014-10-01 16:56:22 +02:00
|
|
|
if (!checkNodeType(JSON_OBJECT)) return 0;
|
2014-09-27 14:43:19 +02:00
|
|
|
|
2014-10-01 16:56:22 +02:00
|
|
|
for (JsonNodeIterator it = beginChildren(); it != endChildren(); ++it)
|
2014-09-27 14:43:19 +02:00
|
|
|
{
|
2014-10-01 16:56:22 +02:00
|
|
|
const char* childKey = it->content.asKey.key;
|
2014-09-27 14:43:19 +02:00
|
|
|
|
|
|
|
if (!strcmp(childKey, key))
|
2014-10-01 16:56:22 +02:00
|
|
|
return it->content.asKey.value;
|
2014-09-27 14:43:19 +02:00
|
|
|
}
|
2014-10-01 16:56:22 +02:00
|
|
|
|
|
|
|
JsonNode* newValueNode = createNode(JSON_UNDEFINED);
|
2014-09-30 17:14:59 +02:00
|
|
|
if (!newValueNode) return 0;
|
2014-09-27 15:34:34 +02:00
|
|
|
|
2014-10-01 16:56:22 +02:00
|
|
|
JsonNode* newKeyNode = createNode(JSON_KEY);
|
2014-09-30 17:14:59 +02:00
|
|
|
if (!newKeyNode) return 0;
|
|
|
|
|
2014-09-27 14:43:19 +02:00
|
|
|
newKeyNode->content.asKey.key = key;
|
|
|
|
newKeyNode->content.asKey.value = newValueNode;
|
|
|
|
|
2014-10-05 14:48:19 +02:00
|
|
|
addChild(newKeyNode);
|
2014-09-27 14:43:19 +02:00
|
|
|
|
|
|
|
return newValueNode;
|
2014-09-30 16:31:22 +02:00
|
|
|
}
|
2014-10-07 12:11:10 +02:00
|
|
|
|
|
|
|
JsonNode* JsonObject::createContainerNodeAt(char const* key, JsonNodeType type)
|
|
|
|
{
|
|
|
|
JsonNode* node = getOrCreateNodeAt(key);
|
|
|
|
if (!node) return 0;
|
|
|
|
|
|
|
|
node->type = type;
|
|
|
|
node->content.asContainer.child = 0;
|
|
|
|
node->content.asContainer.buffer = _node->content.asContainer.buffer;
|
|
|
|
|
|
|
|
return node;
|
|
|
|
}
|