2014-06-27 13:42:26 +02:00
|
|
|
/*
|
|
|
|
* Arduino JSON library
|
|
|
|
* Benoit Blanchon 2014 - MIT License
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "JsonValue.h"
|
2014-06-30 19:19:39 +02:00
|
|
|
#include <cstdio>
|
|
|
|
#include <cstring>
|
2014-06-27 13:42:26 +02:00
|
|
|
|
2014-07-02 12:54:22 +02:00
|
|
|
size_t JsonValue::printBoolTo(Print& p) const
|
2014-06-27 13:42:26 +02:00
|
|
|
{
|
2014-07-02 12:51:27 +02:00
|
|
|
return p.write(content.asBool ? "true" : "false");
|
2014-06-30 18:37:54 +02:00
|
|
|
}
|
2014-06-27 13:42:26 +02:00
|
|
|
|
2014-07-02 12:54:22 +02:00
|
|
|
size_t JsonValue::printDoubleTo(Print& p) const
|
2014-06-30 18:37:54 +02:00
|
|
|
{
|
2014-07-02 13:02:28 +02:00
|
|
|
char tmp[32];
|
2014-07-02 13:46:25 +02:00
|
|
|
sprintf(tmp, "%.17lg", content.asDouble);
|
|
|
|
return p.write(tmp);
|
|
|
|
}
|
2014-06-30 19:19:39 +02:00
|
|
|
|
2014-07-02 13:46:25 +02:00
|
|
|
size_t JsonValue::printFloatTo(Print& p) const
|
|
|
|
{
|
|
|
|
char tmp[16];
|
|
|
|
sprintf(tmp, "%.9g", content.asFloat);
|
2014-07-01 13:36:22 +02:00
|
|
|
return p.write(tmp);
|
2014-06-30 18:37:54 +02:00
|
|
|
}
|
2014-06-27 13:42:26 +02:00
|
|
|
|
2014-07-02 13:02:28 +02:00
|
|
|
size_t JsonValue::printLongTo(Print& p) const
|
|
|
|
{
|
|
|
|
char tmp[32];
|
|
|
|
sprintf(tmp, "%ld", content.asLong);
|
|
|
|
return p.write(tmp);
|
|
|
|
}
|
|
|
|
|
2014-07-02 12:54:22 +02:00
|
|
|
size_t JsonValue::printPrintableTo(Print& p) const
|
2014-06-30 18:37:54 +02:00
|
|
|
{
|
2014-07-02 12:51:27 +02:00
|
|
|
if (content.asPrintable)
|
|
|
|
return ((Printable*) content.asPrintable)->printTo(p);
|
2014-06-30 18:37:54 +02:00
|
|
|
else
|
2014-07-01 13:36:22 +02:00
|
|
|
return p.write("null");
|
2014-06-30 18:37:54 +02:00
|
|
|
}
|
2014-06-27 13:42:26 +02:00
|
|
|
|
2014-07-01 14:08:15 +02:00
|
|
|
size_t JsonValue::printStringTo(Print& p) const
|
2014-06-30 18:37:54 +02:00
|
|
|
{
|
2014-07-02 12:51:27 +02:00
|
|
|
const char* s = content.asString;
|
2014-06-30 19:19:39 +02:00
|
|
|
|
|
|
|
if (!s)
|
|
|
|
{
|
2014-07-01 13:36:22 +02:00
|
|
|
return p.write("null");
|
2014-06-30 19:19:39 +02:00
|
|
|
}
|
|
|
|
|
2014-07-01 13:15:50 +02:00
|
|
|
size_t n = 0;
|
2014-06-30 19:19:39 +02:00
|
|
|
|
2014-07-01 13:36:22 +02:00
|
|
|
n += p.write('\"');
|
2014-06-30 19:19:39 +02:00
|
|
|
|
|
|
|
while (*s)
|
|
|
|
{
|
|
|
|
switch (*s)
|
|
|
|
{
|
|
|
|
case '"':
|
2014-07-01 13:36:22 +02:00
|
|
|
n += p.write("\\\"");
|
2014-06-30 19:19:39 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
case '\\':
|
2014-07-01 13:36:22 +02:00
|
|
|
n += p.write("\\\\");
|
2014-06-30 19:19:39 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
case '\b':
|
2014-07-01 13:36:22 +02:00
|
|
|
n += p.write("\\b");
|
2014-06-30 19:19:39 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
case '\f':
|
2014-07-01 13:36:22 +02:00
|
|
|
n += p.write("\\f");
|
2014-06-30 19:19:39 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
case '\n':
|
2014-07-01 13:36:22 +02:00
|
|
|
n += p.write("\\n");
|
2014-06-30 19:19:39 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
case '\r':
|
2014-07-01 13:36:22 +02:00
|
|
|
n += p.write("\\r");
|
2014-06-30 19:19:39 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
case '\t':
|
2014-07-01 13:36:22 +02:00
|
|
|
n += p.write("\\t");
|
2014-06-30 19:19:39 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2014-07-01 13:36:22 +02:00
|
|
|
n += p.write(*s);
|
2014-06-30 19:19:39 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
s++;
|
|
|
|
}
|
|
|
|
|
2014-07-01 13:36:22 +02:00
|
|
|
n += p.write('\"');
|
2014-07-01 13:15:50 +02:00
|
|
|
|
|
|
|
return n;
|
2014-06-27 13:42:26 +02:00
|
|
|
}
|