summaryrefslogtreecommitdiff
path: root/lisp/json.el
diff options
context:
space:
mode:
authorDamien Cassou <damien@cassou.me>2018-05-19 08:36:32 +0200
committerNicolas Petton <nicolas@petton.fr>2018-06-14 11:01:49 +0200
commit8cb9beb32163fa3ce3b052ced646fd673814ddc6 (patch)
tree57f2140a14a50c59d7b1caa875351e66448f4336 /lisp/json.el
parent967d2c55ef3908fd378e05b2a0070663ae45f6de (diff)
downloademacs-8cb9beb32163fa3ce3b052ced646fd673814ddc6.tar.gz
Fix pretty-printing empty objects as null
* lisp/json.el (json-pretty-print): Force distinction between empty objects and null. (json-encode-list): Remove responsibility to print "null" as this value is not a list. (json-encode): Give higher precedence to lists so that an empty list is printed as an empty object, not as "null". * test/lisp/json-tests.el (test-json-encode): Add many tests to check the behavior of pretty-printing.
Diffstat (limited to 'lisp/json.el')
-rw-r--r--lisp/json.el7
1 files changed, 4 insertions, 3 deletions
diff --git a/lisp/json.el b/lisp/json.el
index d374f452e6b..cd95ec28327 100644
--- a/lisp/json.el
+++ b/lisp/json.el
@@ -609,8 +609,7 @@ Please see the documentation of `json-object-type' and `json-key-type'."
"Return a JSON representation of LIST.
Tries to DWIM: simple lists become JSON arrays, while alists and plists
become JSON objects."
- (cond ((null list) "null")
- ((json-alist-p list) (json-encode-alist list))
+ (cond ((json-alist-p list) (json-encode-alist list))
((json-plist-p list) (json-encode-plist list))
((listp list) (json-encode-array list))
(t
@@ -723,12 +722,12 @@ Advances point just past JSON object."
((stringp object) (json-encode-string object))
((keywordp object) (json-encode-string
(substring (symbol-name object) 1)))
+ ((listp object) (json-encode-list object))
((symbolp object) (json-encode-string
(symbol-name object)))
((numberp object) (json-encode-number object))
((arrayp object) (json-encode-array object))
((hash-table-p object) (json-encode-hash-table object))
- ((listp object) (json-encode-list object))
(t (signal 'json-error (list object)))))
;; Pretty printing
@@ -743,6 +742,8 @@ Advances point just past JSON object."
(interactive "r")
(atomic-change-group
(let ((json-encoding-pretty-print t)
+ ;; Distinguish an empty objects from 'null'
+ (json-null :json-null)
;; Ensure that ordering is maintained
(json-object-type 'alist)
(txt (delete-and-extract-region begin end)))