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

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

bug#21488: json-pretty-print-buffer works,but...


From: Simen Heggestøyl
Subject: bug#21488: json-pretty-print-buffer works,but...
Date: Sat, 03 Oct 2015 15:42:25 +0200
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/24.5 (gnu/linux)

Stefan Monnier <monnier@iro.umontreal.ca> writes:
>> See https://github.com/pashky/restclient.el/issues/97
>
> Please include a description of the problem right here, so it's archived
> here and is available no matter what github turns into.
>
>
>         Stefan

The reporter's concern seems to be that `json-pretty-print' and
`json-pretty-print-buffer' reverse the order of JSON object keys. So
for instance, when you pretty print the following object:

{
  "a": 1,
  "b": 2
}

It becomes:

{
  "b": 2,
  "a": 1
}

JSON objects are by definition unordered, but I agree that it would be
nicer if the prettification functions maintained the ordering by
default.

Please consider the following patch, which makes the prettification
functions maintain the original ordering:


>From f994203f746df3aa9fcf10592fd03d7aabe1f4f3 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Simen=20Heggest=C3=B8yl?= <simenheg@gmail.com>
Date: Sat, 3 Oct 2015 11:31:16 +0200
Subject: [PATCH] Maintain ordering of JSON object keys by default

* lisp/json.el (json-object-type): Mention order handling in doc-string.
(json-read-object): Maintain ordering for alists.
(json-pretty-print): Ensure that ordering is maintained.

* test/automated/json-tests.el (json-read-simple-alist): Update test to
accommodate for changes in `json-read-object'.

* etc/NEWS: Document the new behavior of the pretty printing functions.
---
 etc/NEWS                     | 5 +++++
 lisp/json.el                 | 9 +++++++--
 test/automated/json-tests.el | 7 ++++---
 3 files changed, 16 insertions(+), 5 deletions(-)

diff --git a/etc/NEWS b/etc/NEWS
index 26c478e..dbe0de3 100644
--- a/etc/NEWS
+++ b/etc/NEWS
@@ -314,6 +314,11 @@ standards.
 
 * Changes in Specialized Modes and Packages in Emacs 25.1

+** JSON
+---
+*** `json-pretty-print' and `json-pretty-print-buffer' now maintain
+the ordering of object keys by default.
+
 ** You can recompute the VC state of a file buffer with `M-x vc-refresh-state'
 ** Prog mode has some support for multi-mode indentation.
 See `prog-indentation-context' and `prog-widen'.
diff --git a/lisp/json.el b/lisp/json.el
index daa0c94..b0c7e9e 100644
--- a/lisp/json.el
+++ b/lisp/json.el
@@ -57,7 +57,8 @@
 (defvar json-object-type 'alist
   "Type to convert JSON objects to.
 Must be one of `alist', `plist', or `hash-table'.  Consider let-binding
-this around your call to `json-read' instead of `setq'ing it.")
+this around your call to `json-read' instead of `setq'ing it.  Ordering
+is maintained only for `alist'.")

 (defvar json-array-type 'vector
   "Type to convert JSON arrays to.
@@ -400,7 +401,9 @@ Please see the documentation of `json-object-type' and 
`json-key-type'."
           (signal 'json-object-format (list "," (json-peek))))))
     ;; Skip over the "}"
     (json-advance)
-    elements))
+    (if (eq json-object-type 'alist)
+        (nreverse elements)
+      elements)))

 ;; Hash table encoding

@@ -602,6 +605,8 @@ Advances point just past JSON object."
   (interactive "r")
   (atomic-change-group
     (let ((json-encoding-pretty-print t)
+          ;; Ensure that ordering is maintained
+          (json-object-type 'alist)
           (txt (delete-and-extract-region begin end)))
       (insert (json-encode (json-read-from-string txt))))))

diff --git a/test/automated/json-tests.el b/test/automated/json-tests.el
index fd89b7a..c5a9c6c 100644
--- a/test/automated/json-tests.el
+++ b/test/automated/json-tests.el
@@ -28,9 +28,10 @@
                  "{\"a\":1,\"b\":2}")))

 (ert-deftest json-read-simple-alist ()
-  (should (equal (json-read-from-string "{\"a\": 1, \"b\": 2}")
-                 '((b . 2)
-                   (a . 1)))))
+  (let ((json-object-type 'alist))
+    (should (equal (json-read-from-string "{\"a\": 1, \"b\": 2}")
+                   '((a . 1)
+                     (b . 2))))))

 (ert-deftest json-encode-string-with-special-chars ()
   (should (equal (json-encode-string "a\n\fb")
--
2.5.3






reply via email to

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