diff options
| author | Damien Cassou | 2018-05-19 08:36:32 +0200 |
|---|---|---|
| committer | Nicolas Petton | 2018-06-14 11:01:49 +0200 |
| commit | 8cb9beb32163fa3ce3b052ced646fd673814ddc6 (patch) | |
| tree | 57f2140a14a50c59d7b1caa875351e66448f4336 /lisp | |
| parent | 967d2c55ef3908fd378e05b2a0070663ae45f6de (diff) | |
| download | emacs-8cb9beb32163fa3ce3b052ced646fd673814ddc6.tar.gz emacs-8cb9beb32163fa3ce3b052ced646fd673814ddc6.zip | |
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')
| -rw-r--r-- | lisp/json.el | 7 |
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'." | |||
| 609 | "Return a JSON representation of LIST. | 609 | "Return a JSON representation of LIST. |
| 610 | Tries to DWIM: simple lists become JSON arrays, while alists and plists | 610 | Tries to DWIM: simple lists become JSON arrays, while alists and plists |
| 611 | become JSON objects." | 611 | become JSON objects." |
| 612 | (cond ((null list) "null") | 612 | (cond ((json-alist-p list) (json-encode-alist list)) |
| 613 | ((json-alist-p list) (json-encode-alist list)) | ||
| 614 | ((json-plist-p list) (json-encode-plist list)) | 613 | ((json-plist-p list) (json-encode-plist list)) |
| 615 | ((listp list) (json-encode-array list)) | 614 | ((listp list) (json-encode-array list)) |
| 616 | (t | 615 | (t |
| @@ -723,12 +722,12 @@ Advances point just past JSON object." | |||
| 723 | ((stringp object) (json-encode-string object)) | 722 | ((stringp object) (json-encode-string object)) |
| 724 | ((keywordp object) (json-encode-string | 723 | ((keywordp object) (json-encode-string |
| 725 | (substring (symbol-name object) 1))) | 724 | (substring (symbol-name object) 1))) |
| 725 | ((listp object) (json-encode-list object)) | ||
| 726 | ((symbolp object) (json-encode-string | 726 | ((symbolp object) (json-encode-string |
| 727 | (symbol-name object))) | 727 | (symbol-name object))) |
| 728 | ((numberp object) (json-encode-number object)) | 728 | ((numberp object) (json-encode-number object)) |
| 729 | ((arrayp object) (json-encode-array object)) | 729 | ((arrayp object) (json-encode-array object)) |
| 730 | ((hash-table-p object) (json-encode-hash-table object)) | 730 | ((hash-table-p object) (json-encode-hash-table object)) |
| 731 | ((listp object) (json-encode-list object)) | ||
| 732 | (t (signal 'json-error (list object))))) | 731 | (t (signal 'json-error (list object))))) |
| 733 | 732 | ||
| 734 | ;; Pretty printing | 733 | ;; Pretty printing |
| @@ -743,6 +742,8 @@ Advances point just past JSON object." | |||
| 743 | (interactive "r") | 742 | (interactive "r") |
| 744 | (atomic-change-group | 743 | (atomic-change-group |
| 745 | (let ((json-encoding-pretty-print t) | 744 | (let ((json-encoding-pretty-print t) |
| 745 | ;; Distinguish an empty objects from 'null' | ||
| 746 | (json-null :json-null) | ||
| 746 | ;; Ensure that ordering is maintained | 747 | ;; Ensure that ordering is maintained |
| 747 | (json-object-type 'alist) | 748 | (json-object-type 'alist) |
| 748 | (txt (delete-and-extract-region begin end))) | 749 | (txt (delete-and-extract-region begin end))) |