summaryrefslogtreecommitdiff
path: root/lisp/json.el
diff options
context:
space:
mode:
authorK. Handa <handa@gnu.org>2015-10-05 22:56:26 +0900
committerK. Handa <handa@gnu.org>2015-10-05 22:56:26 +0900
commit47e9556c70a7009d7c750fd7bf10a0e6cf41cdce (patch)
tree4e944bd68080adee76291dd7d4f34103e2b55d50 /lisp/json.el
parent52beda922d2cb523a03661bf74b8678c8b45e440 (diff)
parentef171d1d0b42758b5f705847d558436e867372f4 (diff)
downloademacs-47e9556c70a7009d7c750fd7bf10a0e6cf41cdce.tar.gz
Merge branch 'master' of git.sv.gnu.org:/srv/git/emacs
Diffstat (limited to 'lisp/json.el')
-rw-r--r--lisp/json.el21
1 files changed, 19 insertions, 2 deletions
diff --git a/lisp/json.el b/lisp/json.el
index daa0c94da28..e2c7cc77222 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))))))