diff options
author | Yuta Hongo <yutago@gmail.com> | 2022-03-06 04:25:52 +0900 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-03-05 21:25:52 +0200 |
commit | e3ef73dc2a557232c60c732705e8e6ff2050eba9 (patch) | |
tree | 800f7f5d873caebefb0fe940935be59be6735cf3 /src/cli_common.c | |
parent | af6d5c5932fa4fc1a0461af5e3df50e9c284c4f9 (diff) | |
download | redis-e3ef73dc2a557232c60c732705e8e6ff2050eba9.tar.gz |
redis-cli: Better --json Unicode support and --quoted-json (#10286)
Normally, `redis-cli` escapes non-printable data received from Redis, using a custom scheme (which is also used to handle quoted input). When using `--json` this is not desired as it is not compatible with RFC 7159, which specifies JSON strings are assumed to be Unicode and how they should be escaped.
This commit changes `--json` to follow RFC 7159, which means that properly encoded Unicode strings in Redis will result with a valid Unicode JSON.
However, this introduces a new problem with `--json` and data that is not valid Unicode (e.g., random binary data, text that follows other encoding, etc.). To address this, we add `--quoted-json` which produces JSON strings that follow the original redis-cli quoting scheme.
For example, a value that consists of only null (0x00) bytes will show up as:
* `"\u0000\u0000\u0000"` when using `--json`
* `"\\x00\\x00\\x00"` when using `--quoted-json`
Diffstat (limited to 'src/cli_common.c')
-rw-r--r-- | src/cli_common.c | 25 |
1 files changed, 25 insertions, 0 deletions
diff --git a/src/cli_common.c b/src/cli_common.c index 7064a096b..6d627d2b4 100644 --- a/src/cli_common.c +++ b/src/cli_common.c @@ -371,3 +371,28 @@ void freeCliConnInfo(cliConnInfo connInfo){ if (connInfo.auth) sdsfree(connInfo.auth); if (connInfo.user) sdsfree(connInfo.user); } + +/* + * Escape a Unicode string for JSON output (--json), following RFC 7159: + * https://datatracker.ietf.org/doc/html/rfc7159#section-7 +*/ +sds escapeJsonString(sds s, const char *p, size_t len) { + s = sdscatlen(s,"\"",1); + while(len--) { + switch(*p) { + case '\\': + case '"': + s = sdscatprintf(s,"\\%c",*p); + break; + case '\n': s = sdscatlen(s,"\\n",2); break; + case '\f': s = sdscatlen(s,"\\f",2); break; + case '\r': s = sdscatlen(s,"\\r",2); break; + case '\t': s = sdscatlen(s,"\\t",2); break; + case '\b': s = sdscatlen(s,"\\b",2); break; + default: + s = sdscatprintf(s,(*p >= 0 && *p <= 0x1f) ? "\\u%04x" : "%c",*p); + } + p++; + } + return sdscatlen(s,"\"",1); +} |