aboutsummaryrefslogtreecommitdiffstats
path: root/test
diff options
context:
space:
mode:
authorSimen Heggestøyl2015-11-12 18:30:37 +0100
committerSimen Heggestøyl2015-11-12 18:30:37 +0100
commit1e363a8ea5ac09455f3a44fbb646b5af32bca51c (patch)
tree7b9ab75057951d32892cb50a31c5880c941caf23 /test
parent9dd7da9945c16aa343080a535ed74eeecf769fd1 (diff)
downloademacs-1e363a8ea5ac09455f3a44fbb646b5af32bca51c.tar.gz
emacs-1e363a8ea5ac09455f3a44fbb646b5af32bca51c.zip
Enable sorting of JSON object keys when encoding
* lisp/json.el (json-encoding-object-sort-predicate): New variable for specifying a sorting predicate for JSON objects during encoding. (json--plist-to-alist): New utility function. (json-encode-hash-table): Re-use `json-encode-alist' when object keys are to be sorted. (json-encode-alist): Sort output by `json-encoding-object-sort-predicate, when set. (json-encode-plist): Re-use `json-encode-alist' when object keys are to be sorted. (json-pretty-print-buffer-ordered): New command to pretty print the buffer with object keys sorted alphabetically. (json-pretty-print-ordered): New command to pretty print the region with object keys sorted alphabetically. * test/automated/json-tests.el (test-json-plist-to-alist) (test-json-encode-plist, test-json-encode-hash-table) (test-json-encode-alist-with-sort-predicate) (test-json-encode-plist-with-sort-predicate): New tests. * etc/NEWS: Add an entry for the new commands.
Diffstat (limited to 'test')
-rw-r--r--test/automated/json-tests.el29
1 files changed, 29 insertions, 0 deletions
diff --git a/test/automated/json-tests.el b/test/automated/json-tests.el
index fa1f5484eec..8f0cd6f0857 100644
--- a/test/automated/json-tests.el
+++ b/test/automated/json-tests.el
@@ -28,11 +28,40 @@
28 (should (equal (json--plist-reverse '(:a 1 :b 2 :c 3)) 28 (should (equal (json--plist-reverse '(:a 1 :b 2 :c 3))
29 '(:c 3 :b 2 :a 1)))) 29 '(:c 3 :b 2 :a 1))))
30 30
31(ert-deftest test-json-plist-to-alist ()
32 (should (equal (json--plist-to-alist '()) '()))
33 (should (equal (json--plist-to-alist '(:a 1)) '((:a . 1))))
34 (should (equal (json--plist-to-alist '(:a 1 :b 2 :c 3))
35 '((:a . 1) (:b . 2) (:c . 3)))))
36
37(ert-deftest test-json-encode-plist ()
38 (let ((plist '(:a 1 :b 2)))
39 (should (equal (json-encode plist) "{\"a\":1,\"b\":2}"))))
40
31(ert-deftest json-encode-simple-alist () 41(ert-deftest json-encode-simple-alist ()
32 (should (equal (json-encode '((a . 1) 42 (should (equal (json-encode '((a . 1)
33 (b . 2))) 43 (b . 2)))
34 "{\"a\":1,\"b\":2}"))) 44 "{\"a\":1,\"b\":2}")))
35 45
46(ert-deftest test-json-encode-hash-table ()
47 (let ((hash-table (make-hash-table))
48 (json-encoding-object-sort-predicate 'string<))
49 (puthash :a 1 hash-table)
50 (puthash :b 2 hash-table)
51 (puthash :c 3 hash-table)
52 (should (equal (json-encode hash-table)
53 "{\"a\":1,\"b\":2,\"c\":3}"))))
54
55(ert-deftest test-json-encode-alist-with-sort-predicate ()
56 (let ((alist '((:c . 3) (:a . 1) (:b . 2)))
57 (json-encoding-object-sort-predicate 'string<))
58 (should (equal (json-encode alist) "{\"a\":1,\"b\":2,\"c\":3}"))))
59
60(ert-deftest test-json-encode-plist-with-sort-predicate ()
61 (let ((plist '(:c 3 :a 1 :b 2))
62 (json-encoding-object-sort-predicate 'string<))
63 (should (equal (json-encode plist) "{\"a\":1,\"b\":2,\"c\":3}"))))
64
36(ert-deftest json-read-simple-alist () 65(ert-deftest json-read-simple-alist ()
37 (let ((json-object-type 'alist)) 66 (let ((json-object-type 'alist))
38 (should (equal (json-read-from-string "{\"a\": 1, \"b\": 2}") 67 (should (equal (json-read-from-string "{\"a\": 1, \"b\": 2}")