//
// JSON data type and serializer for Citadel
//
-// Copyright (c) 1987-2018 by the citadel.org team
+// Copyright (c) 1987-2023 by the citadel.org team
//
// This program is open source software. Use, duplication, or disclosure
// is subject to the terms of the GNU General Public License, version 3.
Ret = (JsonValue*) malloc(sizeof(JsonValue));
memset(Ret, 0, sizeof(JsonValue));
Ret->Type = JSON_NULL;
- if (Key != NULL)
- {
+ if (Key != NULL) {
Ret->Name = NewStrBufPlain(Key, keylen);
}
Ret->Value = NewStrBufPlain(HKEY("nulll"));
const char *Key;
long keylen;
-
- switch (Val->Type)
- {
+ switch (Val->Type) {
case JSON_STRING:
StrBufAppendBufPlain(Target, HKEY("\""), 0);
- StrECMAEscAppend(Target, Val->Value, NULL);
+ int i;
+ char *plain = (char *)ChrPtr(Val->Value);
+ if (!IsEmptyStr(plain)) {
+ int len = strlen(plain);
+ for (i=0; i<len; ++i) {
+ // JSON escaped strings as per RFC 7159 section 7
+ if (plain[i] == '\r') {
+ StrBufAppendBufPlain(Target, HKEY("\\r"), 0);
+ }
+ else if (plain[i] == '\n') {
+ StrBufAppendBufPlain(Target, HKEY("\\n"), 0);
+ }
+ else if (plain[i] == '\"') {
+ StrBufAppendBufPlain(Target, HKEY("\\\""), 0);
+ }
+ else if (plain[i] == '\\') {
+ StrBufAppendBufPlain(Target, HKEY("\\\\"), 0);
+ }
+ else if (plain[i] < 32) {
+ StrBufAppendPrintf(Target, "\\u%04x", plain[i]);
+ }
+ else {
+ StrBufAppendBufPlain(Target, &plain[i], 1, 0);
+ }
+ }
+ }
StrBufAppendBufPlain(Target, HKEY("\""), 0);
break;
case JSON_NUM: