Prefer value< with new 'sort' in json.el

Now that json.el uses the new sort calling convention, there's no
need to prefer string< over value<, which may even run faster.
Suggested by Mattias Engdegård <mattias.engdegard@gmail.com>.

* lisp/json.el (json-encoding-object-sort-predicate): Mention value<
in docstring.
(json-pretty-print-buffer-ordered, json-pretty-print-ordered): Use
value< in place of string<.
* test/lisp/json-tests.el (test-json-encode-hash-table-sort): Use
value< as json-encoding-object-sort-predicate in at least one test.
This commit is contained in:
Basil L. Contovounesios
2026-02-14 18:56:44 +01:00
parent ee8b2d8ae2
commit 973d5d457b
2 changed files with 4 additions and 4 deletions

View File

@@ -142,7 +142,7 @@ Used only when `json-encoding-pretty-print' is non-nil.")
"Sorting predicate for JSON object keys during encoding.
If nil, no sorting is performed. Else, JSON object keys are
ordered by the specified sort predicate during encoding. For
instance, setting this to `string<' will have JSON object keys
instance, setting this to `value<' will have JSON object keys
ordered alphabetically.")
(defvar json-pre-element-read-function nil
@@ -840,14 +840,14 @@ With prefix argument MINIMIZE, minimize it instead."
"Pretty-print current buffer with object keys ordered.
With prefix argument MINIMIZE, minimize it instead."
(interactive "P")
(let ((json-encoding-object-sort-predicate #'string<))
(let ((json-encoding-object-sort-predicate #'value<))
(json-pretty-print-buffer minimize)))
(defun json-pretty-print-ordered (begin end &optional minimize)
"Pretty-print the region with object keys ordered.
With prefix argument MINIMIZE, minimize it instead."
(interactive "r\nP")
(let ((json-encoding-object-sort-predicate #'string<))
(let ((json-encoding-object-sort-predicate #'value<))
(json-pretty-print begin end minimize)))
(provide 'json)

View File

@@ -657,7 +657,7 @@ Point is moved to beginning of the buffer."
"{\n \"c\": 3,\n \"b\": 2,\n \"a\": 1}")))))
(ert-deftest test-json-encode-hash-table-sort ()
(let ((json-encoding-object-sort-predicate #'string<)
(let ((json-encoding-object-sort-predicate #'value<)
(json-encoding-pretty-print nil))
(pcase-dolist (`(,in . ,out)
'((#s(hash-table) . "{}")