Files
ArduinoJson/srcs/JsonObject.cpp

73 lines
1.7 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)
{
for (JsonNodeIterator it = beginChildren(); it != endChildren(); ++it)
{
2014-10-09 12:14:10 +02:00
const char* childKey = it->getAsObjectKey();
if (!strcmp(childKey, key))
{
2014-10-09 12:14:10 +02:00
removeChild(*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-09 12:14:10 +02:00
JsonNode* node = getOrCreateNodeAt(key);
if (node)
node->setAsArray(_node->getContainerBuffer());
2014-10-07 12:11:10 +02:00
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)
{
2014-10-09 12:14:10 +02:00
JsonNode* node = getOrCreateNodeAt(key);
if (node)
node->setAsObject(_node->getContainerBuffer());
2014-10-07 11:58:59 +02:00
return JsonObject(node);
}
JsonNode* JsonObject::getOrCreateNodeAt(const char* key)
{
for (JsonNodeIterator it = beginChildren(); it != endChildren(); ++it)
{
2014-10-09 12:14:10 +02:00
const char* childKey = it->getAsObjectKey();
if (!strcmp(childKey, key))
2014-10-09 12:14:10 +02:00
return it->getAsObjectValue();
}
2014-10-09 14:17:09 +02:00
JsonNode* newValueNode = createNode();
if (!newValueNode) return 0;
2014-10-09 14:17:09 +02:00
JsonNode* newKeyNode = createNode();
if (!newKeyNode) return 0;
2014-10-09 12:14:10 +02:00
newKeyNode->setAsObjectKeyValue(key, newValueNode);
addChild(newKeyNode);
return newValueNode;
2014-10-07 12:11:10 +02:00
}