mirror of
git://git.sv.gnu.org/emacs.git
synced 2025-12-15 10:30:25 -08:00
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.
This commit is contained in:
parent
9a05f0ac95
commit
6b66375133
3 changed files with 34 additions and 5 deletions
21
lisp/json.el
21
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))))))
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue