diff options
| author | Mattias Engdegård | 2024-03-26 16:44:09 +0100 |
|---|---|---|
| committer | Mattias Engdegård | 2024-03-30 14:45:53 +0100 |
| commit | 890edfd2bb8fd79730919972cc82811b73c7f572 (patch) | |
| tree | 3860e8c46a0f44f88eb93c07a2e0faab99556351 /test | |
| parent | ab016657e7b1bd32c775da271ffb7127f86d5a23 (diff) | |
| download | emacs-890edfd2bb8fd79730919972cc82811b73c7f572.tar.gz emacs-890edfd2bb8fd79730919972cc82811b73c7f572.zip | |
New JSON encoder (bug#70007)
It is in general at least 2x faster than the old encoder and does not
depend on any external library. Using our own code also gives us
control over translation details: for example, we now have full
bignum support and tighter float formatting.
* src/json.c (json_delete, json_initialized, init_json_functions)
(json_malloc, json_free, init_json, json_out_of_memory)
(json_releae_object, check_string_without_embedded_nulls, json_check)
(json_check_utf8, lisp_to_json_nonscalar_1, lisp_to_json_nonscalar)
(lisp_to_json, json_available_p, ensure_json_available, json_insert)
(json_handle_nonlocal_exit, json_insert_callback):
Remove. Remaining uses updated.
* src/json.c (json_out_t, symset_t, struct symset_tbl)
(symset_size, make_symset_table, push_symset, pop_symset)
(cleanup_symset_tables, symset_hash, symset_expand, symset_add)
(json_out_grow_buf, cleanup_json_out, json_make_room, JSON_OUT_STR)
(json_out_str, json_out_byte, json_out_fixnum, string_not_unicode)
(json_plain_char, json_out_string, json_out_nest, json_out_unnest)
(json_out_object_cons, json_out_object_hash), json_out_array)
(json_out_float, json_out_bignum, json_out_something)
(json_out_to_string, json_serialize): New.
(Fjson_serialize, Fjson_insert):
New JSON encoder implementation.
* test/src/json-tests.el (json-serialize/object-with-duplicate-keys)
(json-serialize/string): Update tests.
Diffstat (limited to 'test')
| -rw-r--r-- | test/src/json-tests.el | 41 |
1 files changed, 34 insertions, 7 deletions
diff --git a/test/src/json-tests.el b/test/src/json-tests.el index dffc6291ca1..e5cbe8bff5c 100644 --- a/test/src/json-tests.el +++ b/test/src/json-tests.el | |||
| @@ -126,11 +126,38 @@ | |||
| 126 | 126 | ||
| 127 | (ert-deftest json-serialize/object-with-duplicate-keys () | 127 | (ert-deftest json-serialize/object-with-duplicate-keys () |
| 128 | (skip-unless (fboundp 'json-serialize)) | 128 | (skip-unless (fboundp 'json-serialize)) |
| 129 | (let ((table (make-hash-table :test #'eq))) | 129 | |
| 130 | (puthash (copy-sequence "abc") [1 2 t] table) | 130 | (dolist (n '(1 5 20 100)) |
| 131 | (puthash (copy-sequence "abc") :null table) | 131 | (let ((symbols (mapcar (lambda (i) (make-symbol (format "s%d" i))) |
| 132 | (should (equal (hash-table-count table) 2)) | 132 | (number-sequence 1 n))) |
| 133 | (should-error (json-serialize table) :type 'wrong-type-argument))) | 133 | (expected (concat "{" |
| 134 | (mapconcat (lambda (i) (format "\"s%d\":%d" i i)) | ||
| 135 | (number-sequence 1 n) ",") | ||
| 136 | "}"))) | ||
| 137 | ;; alist | ||
| 138 | (should (equal (json-serialize | ||
| 139 | (append | ||
| 140 | (cl-mapcar #'cons | ||
| 141 | symbols (number-sequence 1 n)) | ||
| 142 | (cl-mapcar #'cons | ||
| 143 | symbols (number-sequence 1001 (+ 1000 n))))) | ||
| 144 | expected)) | ||
| 145 | ;; plist | ||
| 146 | (should (equal (json-serialize | ||
| 147 | (append | ||
| 148 | (cl-mapcan #'list | ||
| 149 | symbols (number-sequence 1 n)) | ||
| 150 | (cl-mapcan #'list | ||
| 151 | symbols (number-sequence 1001 (+ 1000 n))))) | ||
| 152 | expected)))) | ||
| 153 | |||
| 154 | ;; We don't check for duplicated keys in hash tables. | ||
| 155 | ;; (let ((table (make-hash-table :test #'eq))) | ||
| 156 | ;; (puthash (copy-sequence "abc") [1 2 t] table) | ||
| 157 | ;; (puthash (copy-sequence "abc") :null table) | ||
| 158 | ;; (should (equal (hash-table-count table) 2)) | ||
| 159 | ;; (should-error (json-serialize table) :type 'wrong-type-argument)) | ||
| 160 | ) | ||
| 134 | 161 | ||
| 135 | (ert-deftest json-parse-string/object () | 162 | (ert-deftest json-parse-string/object () |
| 136 | (skip-unless (fboundp 'json-parse-string)) | 163 | (skip-unless (fboundp 'json-parse-string)) |
| @@ -173,8 +200,8 @@ | |||
| 173 | (should (equal (json-serialize ["\nasdфыв\u001f\u007ffgh\t"]) | 200 | (should (equal (json-serialize ["\nasdфыв\u001f\u007ffgh\t"]) |
| 174 | "[\"\\nasdфыв\\u001F\u007ffgh\\t\"]")) | 201 | "[\"\\nasdфыв\\u001F\u007ffgh\\t\"]")) |
| 175 | (should (equal (json-serialize ["a\0b"]) "[\"a\\u0000b\"]")) | 202 | (should (equal (json-serialize ["a\0b"]) "[\"a\\u0000b\"]")) |
| 176 | ;; FIXME: Is this the right behavior? | 203 | (should-error (json-serialize ["\xC3\x84"])) |
| 177 | (should (equal (json-serialize ["\u00C4\xC3\x84"]) "[\"\u00C4\u00C4\"]"))) | 204 | (should-error (json-serialize ["\u00C4\xC3\x84"]))) |
| 178 | 205 | ||
| 179 | (ert-deftest json-serialize/invalid-unicode () | 206 | (ert-deftest json-serialize/invalid-unicode () |
| 180 | (skip-unless (fboundp 'json-serialize)) | 207 | (skip-unless (fboundp 'json-serialize)) |