Files
ArduinoJson/JsonGenerator/JsonValue.cpp

100 lines
1.8 KiB
C++
Raw Normal View History

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-27 13:42:26 +02:00
2014-07-02 12:54:22 +02:00
size_t JsonValue::printDoubleTo(Print& p) const
{
char tmp[32];
sprintf(tmp, "%.17lg", content.asDouble);
return p.write(tmp);
}
2014-06-30 19:19:39 +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-27 13:42:26 +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-07-02 12:51:27 +02:00
if (content.asPrintable)
return ((Printable*) content.asPrintable)->printTo(p);
else
2014-07-01 13:36:22 +02:00
return p.write("null");
}
2014-06-27 13:42:26 +02:00
size_t JsonValue::printStringTo(Print& p) const
{
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
}
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('\"');
return n;
2014-06-27 13:42:26 +02:00
}