The server is a device so it's written in C. Here's the code that does the escaping.
Code:
#define TRYPUTC(ch) \
{ \
if (putc((ch), cgiOut) == EOF) { \
return -1; \
} \
}
int jsonEscapeData(char *data, int len) {
while (len--) {
if(*data == '\"' || *data == '\\') {
TRYPUTC('\\');
TRYPUTC(*data);
}
else if(*data == '\r') {
TRYPUTC('\\');
TRYPUTC('\\');
TRYPUTC('r');
}
else if(*data == '\n') {
TRYPUTC('\\');
TRYPUTC('\\');
TRYPUTC('n');
}
else {
TRYPUTC(*data);
}
data++;
}
return 0;
}
int jsonEscape(char *s) {
return jsonEscapeData(s, (int) strlen(s));
}