Pulled up code from JsonObject to JsonContainer

This commit is contained in:
Benoit Blanchon
2014-10-01 16:56:22 +02:00
parent b0e43f7538
commit 4d2d535a03
9 changed files with 138 additions and 53 deletions

View File

@ -11,20 +11,6 @@
using namespace ArduinoJson::Internals;
size_t JsonObject::size()
{
JsonNode* firstChild = _node->content.asContainer.child;
int size = 0;
for (JsonNode* child = firstChild; child; child = child->next)
{
size++;
}
return size;
}
JsonValue JsonObject::operator[](char const* key)
{
JsonNode* node = getOrCreateNodeAt(key);
@ -33,61 +19,47 @@ JsonValue JsonObject::operator[](char const* key)
void JsonObject::remove(char const* key)
{
JsonNode* firstChild = _node->content.asContainer.child;
JsonNode* lastChild = 0;
for (JsonNode* child = firstChild; child; child = child->next)
for (JsonNodeIterator it = beginChildren(); it != endChildren(); ++it)
{
const char* childKey = child->content.asKey.key;
const char* childKey = it->content.asKey.key;
if (!strcmp(childKey, key))
{
if (lastChild)
lastChild->next = child->next;
else
_node->content.asContainer.child = child->next;
}
lastChild = child;
removeChildAfter(*it, lastChild);
}
lastChild = *it;
}
}
bool JsonObject::operator==(JsonObject const& other) const
JsonNode* JsonObject::getOrCreateNodeAt(const char* key)
{
return _node == other._node;
}
if (!checkNodeType(JSON_OBJECT)) return 0;
JsonNode* JsonObject::getOrCreateNodeAt(char const* key)
{
if (!_node || _node->type != JSON_OBJECT) return 0;
JsonNode* firstChild = _node->content.asContainer.child;
JsonNode* lastChild = 0;
for (JsonNode* child = firstChild; child; child = child->next)
for (JsonNodeIterator it = beginChildren(); it != endChildren(); ++it)
{
const char* childKey = child->content.asKey.key;
const char* childKey = it->content.asKey.key;
if (!strcmp(childKey, key))
return child->content.asKey.value;
return it->content.asKey.value;
lastChild = child;
lastChild = *it;
}
JsonBuffer* buffer = _node->content.asContainer.buffer;
JsonNode* newValueNode = buffer->createNode(JSON_UNDEFINED);
JsonNode* newValueNode = createNode(JSON_UNDEFINED);
if (!newValueNode) return 0;
JsonNode* newKeyNode = buffer->createNode(JSON_KEY);
JsonNode* newKeyNode = createNode(JSON_KEY);
if (!newKeyNode) return 0;
newKeyNode->content.asKey.key = key;
newKeyNode->content.asKey.value = newValueNode;
if (lastChild)
lastChild->next = newKeyNode;
else
_node->content.asContainer.child = newKeyNode;
insertChildAfter(newKeyNode, lastChild);
return newValueNode;
}