json: strip spaces and special chars when copying objects

When turning an object into a string, strip out all special chars
and duplicate spaces.
This commit is contained in:
Wim Taymans 2021-02-06 20:20:26 +01:00
parent f1e56b2317
commit 0be2959f11

View file

@ -287,10 +287,23 @@ static inline int spa_json_parse_string(const char *val, int len, char *result)
{ {
const char *p; const char *p;
if (!spa_json_is_string(val, len)) { if (!spa_json_is_string(val, len)) {
strncpy(result, val, len); bool skip = false;
result[len] = '\0'; for (p = val; p < val + len; p++) {
return 1; switch (*p) {
case '\n': case '\r': case '\b': case '\t': case '\f':
break;
case ' ':
if (!skip)
*result++ = *p;
skip = true;
break;
default:
*result++ = *p;
skip = false;
break;
} }
}
} else {
for (p = val+1; p < val + len; p++) { for (p = val+1; p < val + len; p++) {
if (*p == '\\') { if (*p == '\\') {
p++; p++;
@ -311,7 +324,8 @@ static inline int spa_json_parse_string(const char *val, int len, char *result)
} else } else
*result++ = *p; *result++ = *p;
} }
*result++ = '\0'; }
*result = '\0';
return 1; return 1;
} }