Files
ArduinoJson/JsonGenerator/EscapedString.cpp

73 lines
1.0 KiB
C++
Raw Normal View History

2014-07-07 13:38:35 +02:00
/*
* Arduino JSON library
* Benoit Blanchon 2014 - MIT License
*/
#include "EscapedString.h"
using namespace ArduinoJson::Internals;
2014-07-07 13:38:35 +02:00
2014-07-09 12:50:03 +02:00
static inline char getSpecialChar(char c)
{
switch (c)
{
case '"':
return '"';
case '\\':
return '\\';
case '\b':
return 'b';
case '\f':
return 'f';
case '\n':
return 'n';
case '\r':
return 'r';
case '\t':
return 't';
default:
return 0;
}
}
2014-07-07 13:38:35 +02:00
size_t EscapedString::printTo(Print& p) const
{
const char* s = rawString;
if (!s)
{
return p.print("null");
}
size_t n = 0;
n += p.write('\"');
while (*s)
{
2014-07-09 12:50:03 +02:00
char specialChar = getSpecialChar(*s);
2014-07-07 13:38:35 +02:00
2014-07-09 12:50:03 +02:00
if (specialChar)
{
n += p.write('\\');
n += p.write(specialChar);
}
else
{
2014-07-07 13:38:35 +02:00
n += p.write(*s);
}
2014-07-09 12:50:03 +02:00
s++;
2014-07-07 13:38:35 +02:00
}
n += p.write('\"');
return n;
}