Files
ArduinoJson/srcs/JsonObject.cpp

75 lines
1.8 KiB
C++
Raw Normal View History

#include "JsonObject.h"
2014-09-30 16:40:00 +02:00
#include <string.h> // for strcmp
#include "JsonBuffer.h"
#include "JsonValue.h"
#include "Internals/EscapedString.h"
#include "Internals/JsonNode.h"
#include "Internals/StringBuilder.h"
2014-09-30 16:40:00 +02:00
using namespace ArduinoJson::Internals;
JsonValue JsonObject::operator[](char const* key)
{
JsonNode* node = getOrCreateNodeAt(key);
return JsonValue(node);
}
void JsonObject::remove(char const* key)
{
JsonNode* lastChild = 0;
for (JsonNodeIterator it = beginChildren(); it != endChildren(); ++it)
{
const char* childKey = it->content.asKey.key;
if (!strcmp(childKey, key))
{
removeChildAfter(*it, lastChild);
}
lastChild = *it;
}
2014-09-28 21:04:59 +02:00
}
2014-10-07 11:58:59 +02:00
JsonObject JsonObject::createNestedObject(char const* key)
{
JsonNode* node = getOrCreateNodeAt(key);
if (node)
{
node->type = JSON_OBJECT;
node->content.asContainer.child = 0;
node->content.asContainer.buffer = _node->content.asContainer.buffer;
}
return JsonObject(node);
}
JsonNode* JsonObject::getOrCreateNodeAt(const char* key)
{
if (!checkNodeType(JSON_OBJECT)) return 0;
for (JsonNodeIterator it = beginChildren(); it != endChildren(); ++it)
{
const char* childKey = it->content.asKey.key;
if (!strcmp(childKey, key))
return it->content.asKey.value;
}
JsonNode* newValueNode = createNode(JSON_UNDEFINED);
if (!newValueNode) return 0;
JsonNode* newKeyNode = createNode(JSON_KEY);
if (!newKeyNode) return 0;
newKeyNode->content.asKey.key = key;
newKeyNode->content.asKey.value = newValueNode;
addChild(newKeyNode);
return newValueNode;
2014-09-30 16:31:22 +02:00
}