2014-10-16 00:11:23 +02:00
|
|
|
#include "ArduinoJson/JsonObject.h"
|
2014-09-30 16:40:00 +02:00
|
|
|
|
|
|
|
#include <string.h> // for strcmp
|
|
|
|
|
2014-10-16 00:11:23 +02:00
|
|
|
#include "ArduinoJson/JsonBuffer.h"
|
|
|
|
#include "ArduinoJson/JsonValue.h"
|
|
|
|
#include "ArduinoJson/Internals/EscapedString.h"
|
|
|
|
#include "ArduinoJson/Internals/JsonNode.h"
|
|
|
|
#include "ArduinoJson/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)
|
|
|
|
{
|
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-09 12:14:10 +02:00
|
|
|
const char* childKey = it->getAsObjectKey();
|
2014-09-30 17:05:33 +02:00
|
|
|
|
|
|
|
if (!strcmp(childKey, key))
|
|
|
|
{
|
2014-10-09 12:14:10 +02:00
|
|
|
removeChild(*it);
|
|
|
|
}
|
2014-10-01 16:56:22 +02:00
|
|
|
}
|
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);
|
|
|
|
}
|
|
|
|
|
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
|
|
|
for (JsonNodeIterator it = beginChildren(); it != endChildren(); ++it)
|
2014-09-27 14:43:19 +02:00
|
|
|
{
|
2014-10-09 12:14:10 +02:00
|
|
|
const char* childKey = it->getAsObjectKey();
|
2014-09-27 14:43:19 +02:00
|
|
|
|
|
|
|
if (!strcmp(childKey, key))
|
2014-10-09 12:14:10 +02:00
|
|
|
return it->getAsObjectValue();
|
2014-09-27 14:43:19 +02:00
|
|
|
}
|
2014-10-01 16:56:22 +02:00
|
|
|
|
2014-10-09 14:17:09 +02:00
|
|
|
JsonNode* newValueNode = createNode();
|
2014-09-30 17:14:59 +02:00
|
|
|
if (!newValueNode) return 0;
|
2014-09-27 15:34:34 +02:00
|
|
|
|
2014-10-09 14:17:09 +02:00
|
|
|
JsonNode* newKeyNode = createNode();
|
2014-09-30 17:14:59 +02:00
|
|
|
if (!newKeyNode) return 0;
|
|
|
|
|
2014-10-09 12:14:10 +02:00
|
|
|
newKeyNode->setAsObjectKeyValue(key, newValueNode);
|
2014-09-27 14:43:19 +02:00
|
|
|
|
2014-10-05 14:48:19 +02:00
|
|
|
addChild(newKeyNode);
|
2014-09-27 14:43:19 +02:00
|
|
|
|
|
|
|
return newValueNode;
|
2014-10-07 12:11:10 +02:00
|
|
|
}
|