Files
ArduinoJson/srcs/Internals/JsonNodeSerializer.cpp

70 lines
1.6 KiB
C++
Raw Normal View History

2014-10-01 12:28:30 +02:00
#include "JsonNodeSerializer.h"
#include "EscapedString.h"
#include "JsonNode.h"
using namespace ArduinoJson::Internals;
size_t JsonNodeSerializer::serialize(const JsonNode* node)
2014-10-01 12:28:30 +02:00
{
if (!node) return 0;
2014-10-01 12:28:30 +02:00
switch (node->type)
{
case JSON_OBJECT:
return serializeObject(node);
2014-10-01 12:28:30 +02:00
case JSON_STRING:
return EscapedString::printTo(node->content.asString, _sink);
2014-10-01 12:28:30 +02:00
case JSON_INTEGER:
return _sink.print(node->content.asInteger);
2014-10-01 12:28:30 +02:00
case JSON_BOOLEAN:
return _sink.print(node->content.asBoolean ? "true" : "false");
2014-10-01 15:47:32 +02:00
case JSON_PROXY:
return serialize(node->content.asProxy.target);
}
2014-10-01 12:28:30 +02:00
if (node->type >= JSON_DOUBLE_0_DECIMALS)
{
return _sink.print(node->content.asDouble, node->type - JSON_DOUBLE_0_DECIMALS);
}
2014-10-01 12:28:30 +02:00
return 0;
}
2014-10-01 12:28:30 +02:00
size_t JsonNodeSerializer::serializeObject(const JsonNode* node)
{
size_t n = 0;
2014-10-01 12:28:30 +02:00
n += _sink.write('{');
JsonNode* firstChild = node->content.asObject.child;
2014-10-01 12:28:30 +02:00
for (JsonNode* child = firstChild; child; child = child->next)
{
n += serializeKeyValue(child);
2014-10-01 12:28:30 +02:00
if (child->next)
{
n += _sink.write(',');
}
}
n += _sink.write('}');
return n;
}
size_t JsonNodeSerializer::serializeKeyValue(JsonNode const* node)
{
const char* childKey = node->content.asKey.key;
JsonNode* childValue = node->content.asKey.value;
return
EscapedString::printTo(childKey, _sink) +
_sink.write(':') +
serialize(childValue);
2014-10-01 12:28:30 +02:00
}