diff options
| author | Mattias Engdegård | 2024-09-08 20:02:34 +0200 |
|---|---|---|
| committer | Mattias Engdegård | 2024-09-08 20:02:34 +0200 |
| commit | e55e2e1c6baebbd105f930fa553ec65c74a3000d (patch) | |
| tree | c171c29f7bcfb8a4a52e74e641506aaf9a2b960d /test/src/json-tests.el | |
| parent | 89c99891b2b3ab087cd7e824cef391ef26800ab4 (diff) | |
| download | emacs-e55e2e1c6baebbd105f930fa553ec65c74a3000d.tar.gz emacs-e55e2e1c6baebbd105f930fa553ec65c74a3000d.zip | |
Make json-serialize always return a unibyte string (bug#70007)
The JSON format is defined as a byte sequence and will always be used as
such, so returning a multibyte string makes little sense.
* src/json.c (json_out_to_string): Remove.
(Fjson_serialize): Return unibyte string.
* test/src/json-tests.el (json-serialize/roundtrip)
(json-serialize/roundtrip-scalars, json-serialize/string):
Update tests.
* doc/lispref/text.texi (Parsing JSON): Document.
* etc/NEWS: Announce.
Diffstat (limited to 'test/src/json-tests.el')
| -rw-r--r-- | test/src/json-tests.el | 50 |
1 files changed, 26 insertions, 24 deletions
diff --git a/test/src/json-tests.el b/test/src/json-tests.el index ebac70fb1c7..1d7491a4593 100644 --- a/test/src/json-tests.el +++ b/test/src/json-tests.el | |||
| @@ -36,7 +36,7 @@ | |||
| 36 | (json | 36 | (json |
| 37 | "[null,false,true,0,123,-456,3.75,\"abc\uFFFFαβγ𝔸𝐁𝖢\\\"\\\\\"]") | 37 | "[null,false,true,0,123,-456,3.75,\"abc\uFFFFαβγ𝔸𝐁𝖢\\\"\\\\\"]") |
| 38 | (json-bytes (encode-coding-string json 'utf-8))) | 38 | (json-bytes (encode-coding-string json 'utf-8))) |
| 39 | (should (equal (json-serialize lisp) json)) ; or `json-bytes'? | 39 | (should (equal (json-serialize lisp) json-bytes)) |
| 40 | (with-temp-buffer | 40 | (with-temp-buffer |
| 41 | ;; multibyte buffer | 41 | ;; multibyte buffer |
| 42 | (json-insert lisp) | 42 | (json-insert lisp) |
| @@ -82,28 +82,29 @@ | |||
| 82 | "\"abc\uFFFFαβγ𝔸𝐁𝖢\\\"\\\\\""))) | 82 | "\"abc\uFFFFαβγ𝔸𝐁𝖢\\\"\\\\\""))) |
| 83 | (cl-destructuring-bind (lisp json) case | 83 | (cl-destructuring-bind (lisp json) case |
| 84 | (ert-info ((format "%S ↔ %S" lisp json)) | 84 | (ert-info ((format "%S ↔ %S" lisp json)) |
| 85 | (should (equal (json-serialize lisp) json)) | 85 | (let ((json-bytes (encode-coding-string json 'utf-8))) |
| 86 | (with-temp-buffer | 86 | (should (equal (json-serialize lisp) json-bytes)) |
| 87 | (json-insert lisp) | 87 | (with-temp-buffer |
| 88 | (should (equal (buffer-string) json)) | 88 | (json-insert lisp) |
| 89 | (should (eobp))) | 89 | (should (equal (buffer-string) json)) |
| 90 | (with-temp-buffer | 90 | (should (eobp))) |
| 91 | (set-buffer-multibyte nil) | 91 | (with-temp-buffer |
| 92 | (json-insert lisp) | 92 | (set-buffer-multibyte nil) |
| 93 | (should (equal (buffer-string) (encode-coding-string json 'utf-8))) | 93 | (json-insert lisp) |
| 94 | (should (eobp))) | 94 | (should (equal (buffer-string) (encode-coding-string json 'utf-8))) |
| 95 | (should (equal (json-parse-string json) lisp)) | 95 | (should (eobp))) |
| 96 | (with-temp-buffer | 96 | (should (equal (json-parse-string json) lisp)) |
| 97 | (insert json) | 97 | (with-temp-buffer |
| 98 | (goto-char 1) | 98 | (insert json) |
| 99 | (should (equal (json-parse-buffer) lisp)) | 99 | (goto-char 1) |
| 100 | (should (eobp))) | 100 | (should (equal (json-parse-buffer) lisp)) |
| 101 | (with-temp-buffer | 101 | (should (eobp))) |
| 102 | (set-buffer-multibyte nil) | 102 | (with-temp-buffer |
| 103 | (insert (encode-coding-string json 'utf-8)) | 103 | (set-buffer-multibyte nil) |
| 104 | (goto-char 1) | 104 | (insert (encode-coding-string json 'utf-8)) |
| 105 | (should (equal (json-parse-buffer) lisp)) | 105 | (goto-char 1) |
| 106 | (should (eobp))))))) | 106 | (should (equal (json-parse-buffer) lisp)) |
| 107 | (should (eobp)))))))) | ||
| 107 | 108 | ||
| 108 | (ert-deftest json-serialize/object () | 109 | (ert-deftest json-serialize/object () |
| 109 | (let ((table (make-hash-table :test #'equal))) | 110 | (let ((table (make-hash-table :test #'equal))) |
| @@ -226,7 +227,8 @@ | |||
| 226 | (should (equal (json-serialize ["foo"]) "[\"foo\"]")) | 227 | (should (equal (json-serialize ["foo"]) "[\"foo\"]")) |
| 227 | (should (equal (json-serialize ["a\n\fb"]) "[\"a\\n\\fb\"]")) | 228 | (should (equal (json-serialize ["a\n\fb"]) "[\"a\\n\\fb\"]")) |
| 228 | (should (equal (json-serialize ["\nasdфыв\u001f\u007ffgh\t"]) | 229 | (should (equal (json-serialize ["\nasdфыв\u001f\u007ffgh\t"]) |
| 229 | "[\"\\nasdфыв\\u001F\u007ffgh\\t\"]")) | 230 | (encode-coding-string "[\"\\nasdфыв\\u001F\u007ffgh\\t\"]" |
| 231 | 'utf-8))) | ||
| 230 | (should (equal (json-serialize ["a\0b"]) "[\"a\\u0000b\"]")) | 232 | (should (equal (json-serialize ["a\0b"]) "[\"a\\u0000b\"]")) |
| 231 | (should-error (json-serialize ["\xC3\x84"])) | 233 | (should-error (json-serialize ["\xC3\x84"])) |
| 232 | (should-error (json-serialize ["\u00C4\xC3\x84"]))) | 234 | (should-error (json-serialize ["\u00C4\xC3\x84"]))) |