Files
ArduinoJson/src/JsonObject.cpp

71 lines
1.6 KiB
C++
Raw Normal View History

#include "ArduinoJson/JsonObject.hpp"
2014-10-16 16:25:42 +02:00
#include <string.h> // for strcmp
#include "ArduinoJson/JsonBuffer.hpp"
#include "ArduinoJson/JsonValue.hpp"
#include "ArduinoJson/Internals/JsonNode.hpp"
#include "ArduinoJson/Internals/StringBuilder.hpp"
2014-10-16 16:25:42 +02:00
2014-10-18 23:05:54 +02:00
using namespace ArduinoJson;
2014-10-16 16:25:42 +02:00
using namespace ArduinoJson::Internals;
2014-10-23 19:54:00 +02:00
JsonValue JsonObject::operator[](char const *key) {
JsonNode *node = getOrCreateNodeAt(key);
return JsonValue(node);
2014-10-16 16:25:42 +02:00
}
2014-10-23 19:54:00 +02:00
void JsonObject::remove(char const *key) {
for (JsonNodeIterator it = beginChildren(); it != endChildren(); ++it) {
const char *childKey = it->getAsObjectKey();
2014-10-16 16:25:42 +02:00
2014-10-23 19:54:00 +02:00
if (!strcmp(childKey, key)) {
removeChild(*it);
2014-10-16 16:25:42 +02:00
}
2014-10-23 19:54:00 +02:00
}
2014-10-16 16:25:42 +02:00
}
2014-10-23 19:54:00 +02:00
JsonArray JsonObject::createNestedArray(char const *key) {
JsonNode *node = getOrCreateNodeAt(key);
2014-10-16 16:25:42 +02:00
2014-10-23 19:54:00 +02:00
if (node)
node->setAsArray(_node->getContainerBuffer());
2014-10-16 16:25:42 +02:00
2014-10-23 19:54:00 +02:00
return JsonArray(node);
2014-10-16 16:25:42 +02:00
}
2014-10-23 19:54:00 +02:00
JsonObject JsonObject::createNestedObject(char const *key) {
JsonNode *node = getOrCreateNodeAt(key);
2014-10-16 16:25:42 +02:00
2014-10-23 19:54:00 +02:00
if (node)
node->setAsObject(_node->getContainerBuffer());
return JsonObject(node);
2014-10-16 16:25:42 +02:00
}
2014-10-23 19:54:00 +02:00
JsonNode *JsonObject::getOrCreateNodeAt(const char *key) {
for (JsonNodeIterator it = beginChildren(); it != endChildren(); ++it) {
const char *childKey = it->getAsObjectKey();
2014-10-16 16:25:42 +02:00
2014-10-23 19:54:00 +02:00
if (!strcmp(childKey, key))
return it->getAsObjectValue();
}
JsonNode *newValueNode = createNode();
if (!newValueNode)
return 0;
JsonNode *newKeyNode = createNode();
if (!newKeyNode)
return 0;
2014-10-16 16:25:42 +02:00
2014-10-23 19:54:00 +02:00
newKeyNode->setAsObjectKeyValue(key, newValueNode);
2014-10-16 16:25:42 +02:00
2014-10-23 19:54:00 +02:00
addChild(newKeyNode);
2014-10-16 16:25:42 +02:00
2014-10-23 19:54:00 +02:00
return newValueNode;
2014-10-22 21:56:38 +02:00
}
2014-10-23 19:54:00 +02:00
JsonObjectIterator JsonObject::begin() {
return JsonObjectIterator(_node->getContainerChild());
2014-10-07 12:11:10 +02:00
}