From: "Simen Heggestøyl" <simenheg@gmail.com>
To: Stefan Monnier <monnier@iro.umontreal.ca>
Cc: Junqi Cai <caijq4ever@163.com>, 21488@debbugs.gnu.org
Subject: bug#21488: json-pretty-print-buffer works,but...
Date: Sat, 03 Oct 2015 21:32:22 +0200 [thread overview]
Message-ID: <87twq7hho9.fsf@gmail.com> (raw)
In-Reply-To: <jwvh9m7etff.fsf-monnier+emacsbugs@gnu.org> (Stefan Monnier's message of "Sat, 03 Oct 2015 13:54:55 -0400")
Stefan Monnier <monnier@iro.umontreal.ca> writes:
> Don't we also need to reverse (tho differently) if json-object-type
> is `plist'?
>
>
> Stefan
Yes, updated:
From cbe22b3bab6770512c43c2214ed11447d2a5b120 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--plist-reverse): New utility function.
(json-read-object): Maintain ordering for alists and plists.
(json-pretty-print): Ensure that ordering is maintained.
* test/automated/json-tests.el (test-json-plist-reverse): New test for
`json--plist-reverse'.
(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 | 21 +++++++++++++++++++--
test/automated/json-tests.el | 13 ++++++++++---
3 files changed, 34 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.
\f
* 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..e2c7cc7 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 for `alist' and `plist', but not for `hash-table'.")
(defvar json-array-type 'vector
"Type to convert JSON arrays to.
@@ -136,6 +137,17 @@ without indentation.")
'not-plist)))
(null list))
+(defun json--plist-reverse (plist)
+ "Return a copy of PLIST in reverse order.
+Unlike `reverse', this keeps the property-value pairs intact."
+ (let (res)
+ (while plist
+ (let ((prop (pop plist))
+ (val (pop plist)))
+ (push val res)
+ (push prop res)))
+ res))
+
(defmacro json--with-indentation (body)
`(let ((json--encoding-current-indentation
(if json-encoding-pretty-print
@@ -400,7 +412,10 @@ 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))
+ (pcase json-object-type
+ (`alist (nreverse elements))
+ (`plist (json--plist-reverse elements))
+ (_ elements))))
;; Hash table encoding
@@ -602,6 +617,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..d1b7a2f 100644
--- a/test/automated/json-tests.el
+++ b/test/automated/json-tests.el
@@ -22,15 +22,22 @@
(require 'ert)
(require 'json)
+(ert-deftest test-json-plist-reverse ()
+ (should (equal (json--plist-reverse '()) '()))
+ (should (equal (json--plist-reverse '(:a 1)) '(:a 1)))
+ (should (equal (json--plist-reverse '(:a 1 :b 2 :c 3))
+ '(:c 3 :b 2 :a 1))))
+
(ert-deftest json-encode-simple-alist ()
(should (equal (json-encode '((a . 1)
(b . 2)))
"{\"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
next prev parent reply other threads:[~2015-10-03 19:32 UTC|newest]
Thread overview: 12+ messages / expand[flat|nested] mbox.gz Atom feed top
2015-09-15 16:00 bug#21488: json-pretty-print-buffer works,but Junqi Cai
2015-09-16 1:13 ` Stefan Monnier
2015-10-03 13:42 ` Simen Heggestøyl
2015-10-03 17:54 ` Stefan Monnier
2015-10-03 19:32 ` Simen Heggestøyl [this message]
2015-10-03 21:01 ` Stefan Monnier
2015-10-03 21:58 ` Simen Heggestøyl
2015-10-09 9:38 ` 蔡君琦
2015-10-13 17:49 ` Simen Heggestøyl
2015-10-14 10:16 ` 蔡君琦
2015-10-17 13:25 ` Simen Heggestøyl
2015-10-19 2:11 ` bug#21488: " 蔡君琦
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=87twq7hho9.fsf@gmail.com \
--to=simenheg@gmail.com \
--cc=21488@debbugs.gnu.org \
--cc=caijq4ever@163.com \
--cc=monnier@iro.umontreal.ca \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
Code repositories for project(s) associated with this external index
https://git.savannah.gnu.org/cgit/emacs.git
https://git.savannah.gnu.org/cgit/emacs/org-mode.git
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.