bug-gnu-emacs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

bug#40693: 28.0.50; json-encode-alist changes alist


From: Basil L. Contovounesios
Subject: bug#40693: 28.0.50; json-encode-alist changes alist
Date: Sun, 19 Apr 2020 01:34:38 +0100
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/28.0.50 (gnu/linux)

Dmitry Gutov <dgutov@yandex.ru> writes:

> How about this one?
>
> diff --git a/lisp/json.el b/lisp/json.el
> index 18d7fda882..b65884f913 100644
> --- a/lisp/json.el
> +++ b/lisp/json.el
> @@ -564,9 +564,10 @@ json-encode-alist
>    "Return a JSON representation of ALIST."
>    (when json-encoding-object-sort-predicate
>      (setq alist
> -          (sort alist (lambda (a b)
> -                        (funcall json-encoding-object-sort-predicate
> -                                 (car a) (car b))))))
> +          (sort (copy-sequence alist)
> +                (lambda (a b)
> +                  (funcall json-encoding-object-sort-predicate
> +                           (car a) (car b))))))
>    (format "{%s%s}"
>            (json-join
>             (json--with-indentation

LGTM.  Perhaps add a test as well:

diff --git a/test/lisp/json-tests.el b/test/lisp/json-tests.el
index 05837e83f9..9d7ffd5feb 100644
--- a/test/lisp/json-tests.el
+++ b/test/lisp/json-tests.el
@@ -269,10 +269,13 @@ test-json-encode-plist-with-sort-predicate
     (should (equal (json-encode plist) "{\"a\":1,\"b\":2,\"c\":3}"))))
 
 (ert-deftest test-json-encode-alist-with-sort-predicate ()
-  (let ((alist '((:c . 3) (:a . 1) (:b . 2)))
-        (json-encoding-object-sort-predicate 'string<)
-        (json-encoding-pretty-print nil))
-    (should (equal (json-encode alist) "{\"a\":1,\"b\":2,\"c\":3}"))))
+  (let* ((alist '((:c . 3) (:a . 1) (:b . 2)))
+         (clone (copy-sequence alist))
+         (json-encoding-object-sort-predicate #'string<)
+         (json-encoding-pretty-print nil))
+    (should (equal (json-encode alist) "{\"a\":1,\"b\":2,\"c\":3}"))
+    ;; Ensure sorting isn't destructive (bug#40693).
+    (should (equal alist clone))))
 
 (ert-deftest test-json-encode-list ()
   (let ((json-encoding-pretty-print nil))
Eli, would this be okay for emacs-27?

Thanks,

-- 
Basil

reply via email to

[Prev in Thread] Current Thread [Next in Thread]