2014-09-27 12:16:20 +02:00
|
|
|
#include "JsonBuffer.h"
|
2014-09-27 11:53:26 +02:00
|
|
|
#include "JsonObject.h"
|
|
|
|
#include "JsonValue.h"
|
2014-09-27 14:43:19 +02:00
|
|
|
#include "JsonNode.h"
|
2014-09-27 14:59:02 +02:00
|
|
|
#include <string.h> // for strcmp
|
2014-09-27 11:53:26 +02:00
|
|
|
|
|
|
|
//JsonValue& JsonObject::operator[](char const* key)
|
|
|
|
//{
|
|
|
|
// addNodeAt(key, innerObject._node);
|
|
|
|
// return innerObject;
|
|
|
|
//}
|
|
|
|
//
|
|
|
|
//void JsonObject::addNodeAt(const char* key, JsonNode& node)
|
|
|
|
//{
|
|
|
|
// JsonNode& keyNode = _buffer.createNode();
|
|
|
|
// keyNode.becomeKey(key, node);
|
|
|
|
// appendChild(keyNode);
|
|
|
|
//}
|
|
|
|
//
|
|
|
|
//void JsonObject::appendChild(JsonNode& newChild)
|
|
|
|
//{
|
|
|
|
// JsonNode* lastChild = _node.asObjectNode.child;
|
|
|
|
// while (lastChild->next)
|
|
|
|
// {
|
|
|
|
// lastChild = lastChild->next;
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
// if (lastChild)
|
|
|
|
// lastChild->next = &newChild;
|
|
|
|
// else
|
|
|
|
// _node.asObjectNode.child = &newChild;
|
|
|
|
//}
|
|
|
|
|
2014-09-27 14:43:19 +02:00
|
|
|
size_t JsonObject::size()
|
|
|
|
{
|
|
|
|
JsonNode* firstChild = _node->content.asObjectNode.child;
|
|
|
|
|
|
|
|
int size = 0;
|
|
|
|
|
|
|
|
for (JsonNode* child = firstChild; child; child = child->next)
|
|
|
|
{
|
|
|
|
size++;
|
|
|
|
}
|
|
|
|
|
|
|
|
return size;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
JsonNode* JsonObject::getOrCreateNodeAt(char const* key)
|
|
|
|
{
|
|
|
|
if (!_node || _node->type != JSON_OBJECT) return 0;
|
|
|
|
|
|
|
|
JsonNode* firstChild = _node->content.asObjectNode.child;
|
|
|
|
JsonNode* lastChild = 0;
|
|
|
|
|
|
|
|
for (JsonNode* child = firstChild; child; child = child->next)
|
|
|
|
{
|
|
|
|
const char* childKey = child->content.asKey.key;
|
|
|
|
|
|
|
|
if (!strcmp(childKey, key))
|
|
|
|
return child->content.asKey.value;
|
|
|
|
|
|
|
|
lastChild = child;
|
|
|
|
}
|
|
|
|
|
|
|
|
JsonNode* newValueNode = _buffer->createNode(JSON_UNDEFINED);
|
|
|
|
|
|
|
|
JsonNode* newKeyNode = _buffer->createNode(JSON_KEY);
|
|
|
|
newKeyNode->content.asKey.key = key;
|
|
|
|
newKeyNode->content.asKey.value = newValueNode;
|
|
|
|
|
|
|
|
if (lastChild)
|
|
|
|
lastChild->next = newKeyNode;
|
|
|
|
else
|
|
|
|
_node->content.asObjectNode.child = newKeyNode;
|
|
|
|
|
|
|
|
return newValueNode;
|
2014-09-27 11:53:26 +02:00
|
|
|
}
|