| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374 |
- #include "JsonObject.h"
- #include <string.h> // for strcmp
- #include "JsonBuffer.h"
- #include "JsonValue.h"
- #include "Internals/EscapedString.h"
- #include "Internals/JsonNode.h"
- #include "Internals/StringBuilder.h"
- 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;
- }
- }
- 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;
- }
|