2014-09-27 11:53:26 +02:00
|
|
|
#include "JsonObject.h"
|
2014-09-30 16:40:00 +02:00
|
|
|
|
|
|
|
#include <string.h> // for strcmp
|
|
|
|
|
2014-09-30 16:59:44 +02:00
|
|
|
#include "EscapedString.h"
|
2014-09-30 16:40:00 +02:00
|
|
|
#include "JsonBuffer.h"
|
2014-09-27 11:53:26 +02:00
|
|
|
#include "JsonValue.h"
|
2014-09-27 14:43:19 +02:00
|
|
|
#include "JsonNode.h"
|
2014-09-30 16:40:00 +02:00
|
|
|
#include "StringBuilder.h"
|
|
|
|
|
|
|
|
using namespace ArduinoJson::Internals;
|
2014-09-27 11:53:26 +02:00
|
|
|
|
2014-09-27 14:43:19 +02:00
|
|
|
size_t JsonObject::size()
|
|
|
|
{
|
2014-09-27 15:34:34 +02:00
|
|
|
JsonNode* firstChild = _node->content.asObject.child;
|
2014-09-27 14:43:19 +02:00
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2014-09-30 17:05:33 +02:00
|
|
|
void JsonObject::remove(char const* key)
|
|
|
|
{
|
|
|
|
JsonNode* firstChild = _node->content.asObject.child;
|
|
|
|
JsonNode* lastChild = 0;
|
|
|
|
|
|
|
|
for (JsonNode* child = firstChild; child; child = child->next)
|
|
|
|
{
|
|
|
|
const char* childKey = child->content.asKey.key;
|
|
|
|
|
|
|
|
if (!strcmp(childKey, key))
|
|
|
|
{
|
|
|
|
if (lastChild)
|
|
|
|
lastChild->next = child->next;
|
|
|
|
else
|
|
|
|
_node->content.asObject.child = child->next;
|
|
|
|
}
|
|
|
|
lastChild = child;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-28 21:04:59 +02:00
|
|
|
bool JsonObject::operator==(JsonObject const& other) const
|
|
|
|
{
|
|
|
|
return _node == other._node;
|
|
|
|
}
|
|
|
|
|
2014-09-27 14:43:19 +02:00
|
|
|
JsonNode* JsonObject::getOrCreateNodeAt(char const* key)
|
|
|
|
{
|
|
|
|
if (!_node || _node->type != JSON_OBJECT) return 0;
|
|
|
|
|
2014-09-27 15:34:34 +02:00
|
|
|
JsonNode* firstChild = _node->content.asObject.child;
|
2014-09-27 14:43:19 +02:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2014-09-27 15:34:34 +02:00
|
|
|
JsonBuffer* buffer = _node->content.asObject.buffer;
|
2014-09-27 14:43:19 +02:00
|
|
|
|
2014-09-27 15:34:34 +02:00
|
|
|
JsonNode* newValueNode = buffer->createNode(JSON_UNDEFINED);
|
2014-09-30 17:14:59 +02:00
|
|
|
if (!newValueNode) return 0;
|
2014-09-27 15:34:34 +02:00
|
|
|
|
|
|
|
JsonNode* newKeyNode = buffer->createNode(JSON_KEY);
|
2014-09-30 17:14:59 +02:00
|
|
|
if (!newKeyNode) return 0;
|
|
|
|
|
2014-09-27 14:43:19 +02:00
|
|
|
newKeyNode->content.asKey.key = key;
|
|
|
|
newKeyNode->content.asKey.value = newValueNode;
|
|
|
|
|
|
|
|
if (lastChild)
|
|
|
|
lastChild->next = newKeyNode;
|
|
|
|
else
|
2014-09-27 15:34:34 +02:00
|
|
|
_node->content.asObject.child = newKeyNode;
|
2014-09-27 14:43:19 +02:00
|
|
|
|
|
|
|
return newValueNode;
|
2014-09-30 16:31:22 +02:00
|
|
|
}
|
|
|
|
|
2014-09-30 16:43:10 +02:00
|
|
|
size_t JsonObject::printTo(char* buffer, size_t bufferSize) const
|
2014-09-30 16:40:00 +02:00
|
|
|
{
|
|
|
|
StringBuilder sb(buffer, bufferSize);
|
2014-09-30 16:43:10 +02:00
|
|
|
return printTo(sb);
|
2014-09-30 16:40:00 +02:00
|
|
|
}
|
|
|
|
|
2014-09-30 16:43:10 +02:00
|
|
|
size_t JsonObject::printTo(Print& p) const
|
2014-09-30 16:31:22 +02:00
|
|
|
{
|
2014-09-30 16:59:44 +02:00
|
|
|
size_t n = 0;
|
|
|
|
|
|
|
|
n += p.write('{');
|
|
|
|
|
|
|
|
JsonNode* firstChild = _node->content.asObject.child;
|
|
|
|
|
|
|
|
for (JsonNode* child = firstChild; child; child = child->next)
|
|
|
|
{
|
|
|
|
const char* childKey = child->content.asKey.key;
|
|
|
|
const char* childValue = child->content.asKey.value->content.asString;
|
|
|
|
|
|
|
|
n += EscapedString::printTo(childKey, p);
|
|
|
|
n += p.write(':');
|
|
|
|
n += EscapedString::printTo(childValue, p);
|
|
|
|
|
|
|
|
if (child->next)
|
|
|
|
{
|
|
|
|
n += p.write(',');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
n += p.write('}');
|
|
|
|
|
|
|
|
return n;
|
2014-09-27 11:53:26 +02:00
|
|
|
}
|