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 | |
| 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.
| -rw-r--r-- | lisp/json.el | 7 | ||||
| -rw-r--r-- | test/lisp/json-tests.el | 67 |
2 files changed, 71 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))) |
diff --git a/test/lisp/json-tests.el b/test/lisp/json-tests.el index ea562e8b134..84039c09cee 100644 --- a/test/lisp/json-tests.el +++ b/test/lisp/json-tests.el | |||
| @@ -325,5 +325,72 @@ Point is moved to beginning of the buffer." | |||
| 325 | (with-temp-buffer | 325 | (with-temp-buffer |
| 326 | (should-error (json-encode (current-buffer)) :type 'json-error))) | 326 | (should-error (json-encode (current-buffer)) :type 'json-error))) |
| 327 | 327 | ||
| 328 | ;;; Pretty-print | ||
| 329 | |||
| 330 | (defun json-tests-equal-pretty-print (original &optional expected) | ||
| 331 | "Abort current test if pretty-printing ORIGINAL does not yield EXPECTED. | ||
| 332 | |||
| 333 | Both ORIGINAL and EXPECTED should be strings. If EXPECTED is | ||
| 334 | nil, ORIGINAL should stay unchanged by pretty-printing." | ||
| 335 | (with-temp-buffer | ||
| 336 | (insert original) | ||
| 337 | (json-pretty-print-buffer) | ||
| 338 | (should (equal (buffer-string) (or expected original))))) | ||
| 339 | |||
| 340 | (ert-deftest test-json-pretty-print-string () | ||
| 341 | (json-tests-equal-pretty-print "\"\"") | ||
| 342 | (json-tests-equal-pretty-print "\"foo\"")) | ||
| 343 | |||
| 344 | (ert-deftest test-json-pretty-print-atom () | ||
| 345 | (json-tests-equal-pretty-print "true") | ||
| 346 | (json-tests-equal-pretty-print "false") | ||
| 347 | (json-tests-equal-pretty-print "null")) | ||
| 348 | |||
| 349 | (ert-deftest test-json-pretty-print-number () | ||
| 350 | (json-tests-equal-pretty-print "123") | ||
| 351 | (json-tests-equal-pretty-print "0.123")) | ||
| 352 | |||
| 353 | (ert-deftest test-json-pretty-print-object () | ||
| 354 | ;; empty (regression test for bug#24252) | ||
| 355 | (json-tests-equal-pretty-print | ||
| 356 | "{}" | ||
| 357 | "{\n}") | ||
| 358 | ;; one pair | ||
| 359 | (json-tests-equal-pretty-print | ||
| 360 | "{\"key\":1}" | ||
| 361 | "{\n \"key\": 1\n}") | ||
| 362 | ;; two pairs | ||
| 363 | (json-tests-equal-pretty-print | ||
| 364 | "{\"key1\":1,\"key2\":2}" | ||
| 365 | "{\n \"key1\": 1,\n \"key2\": 2\n}") | ||
| 366 | ;; embedded object | ||
| 367 | (json-tests-equal-pretty-print | ||
| 368 | "{\"foo\":{\"key\":1}}" | ||
| 369 | "{\n \"foo\": {\n \"key\": 1\n }\n}") | ||
| 370 | ;; embedded array | ||
| 371 | (json-tests-equal-pretty-print | ||
| 372 | "{\"key\":[1,2]}" | ||
| 373 | "{\n \"key\": [\n 1,\n 2\n ]\n}")) | ||
| 374 | |||
| 375 | (ert-deftest test-json-pretty-print-array () | ||
| 376 | ;; empty | ||
| 377 | (json-tests-equal-pretty-print "[]") | ||
| 378 | ;; one item | ||
| 379 | (json-tests-equal-pretty-print | ||
| 380 | "[1]" | ||
| 381 | "[\n 1\n]") | ||
| 382 | ;; two items | ||
| 383 | (json-tests-equal-pretty-print | ||
| 384 | "[1,2]" | ||
| 385 | "[\n 1,\n 2\n]") | ||
| 386 | ;; embedded object | ||
| 387 | (json-tests-equal-pretty-print | ||
| 388 | "[{\"key\":1}]" | ||
| 389 | "[\n {\n \"key\": 1\n }\n]") | ||
| 390 | ;; embedded array | ||
| 391 | (json-tests-equal-pretty-print | ||
| 392 | "[[1,2]]" | ||
| 393 | "[\n [\n 1,\n 2\n ]\n]")) | ||
| 394 | |||
| 328 | (provide 'json-tests) | 395 | (provide 'json-tests) |
| 329 | ;;; json-tests.el ends here | 396 | ;;; json-tests.el ends here |