Files
ArduinoJson/JsonGenerator/JsonValue.cpp

89 lines
1.6 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-07-03 13:54:27 +02:00
using namespace ArduinoJson::Generator;
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-03 13:38:58 +02:00
return p.print(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
{
return p.print(content.asDouble.value, content.asDouble.digits);
}
2014-06-30 19:19:39 +02:00
size_t JsonValue::printLongTo(Print& p) const
{
2014-07-03 13:21:40 +02:00
return p.print(content.asLong);
}
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-03 13:38:58 +02:00
return p.print("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-03 13:38:58 +02:00
return p.print("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-03 13:38:58 +02:00
n += p.print("\\\"");
2014-06-30 19:19:39 +02:00
break;
case '\\':
2014-07-03 13:38:58 +02:00
n += p.print("\\\\");
2014-06-30 19:19:39 +02:00
break;
case '\b':
2014-07-03 13:38:58 +02:00
n += p.print("\\b");
2014-06-30 19:19:39 +02:00
break;
case '\f':
2014-07-03 13:38:58 +02:00
n += p.print("\\f");
2014-06-30 19:19:39 +02:00
break;
case '\n':
2014-07-03 13:38:58 +02:00
n += p.print("\\n");
2014-06-30 19:19:39 +02:00
break;
case '\r':
2014-07-03 13:38:58 +02:00
n += p.print("\\r");
2014-06-30 19:19:39 +02:00
break;
case '\t':
2014-07-03 13:38:58 +02:00
n += p.print("\\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
}