diff options
| author | João Távora | 2018-06-02 00:23:38 +0100 |
|---|---|---|
| committer | João Távora | 2018-06-07 12:30:39 +0100 |
| commit | 3509aaaefe1996ea46b038850629b6d2f7a726fe (patch) | |
| tree | 651fec98d47d8c77863f6f0b08c9b3c9c6ac2362 /test/src/json-tests.el | |
| parent | 2e2f61efa66b69fbd12c83bbd5370a4be2374f66 (diff) | |
| download | emacs-3509aaaefe1996ea46b038850629b6d2f7a726fe.tar.gz emacs-3509aaaefe1996ea46b038850629b6d2f7a726fe.zip | |
Accept plists when serializing and parsing JSON
* doc/lispref/text.texi (Parsing JSON): Mention plist support.
* src/json.c (lisp_to_json_toplevel_1): Serialize plists to json.
(Fjson_serialize): Mention plists in docstring.
(enum json_object_type): Add json_object_plist.
(json_to_lisp): Parse JSON into plists.
(json_parse_object_type): Consider plists.
(Fjson_parse_string): Mention plists in docstring.
(syms_of_json): New Qplist sym_of_json.
(lisp_to_json): Update comment.
* test/src/json-tests.el (json-serialize/object)
(json-parse-string/object): New plist tests.
Diffstat (limited to 'test/src/json-tests.el')
| -rw-r--r-- | test/src/json-tests.el | 30 |
1 files changed, 28 insertions, 2 deletions
diff --git a/test/src/json-tests.el b/test/src/json-tests.el index 09067bad8c8..7a193545b1a 100644 --- a/test/src/json-tests.el +++ b/test/src/json-tests.el | |||
| @@ -69,7 +69,31 @@ | |||
| 69 | (should-error (json-serialize '((1 . 2))) :type 'wrong-type-argument) | 69 | (should-error (json-serialize '((1 . 2))) :type 'wrong-type-argument) |
| 70 | (should-error (json-serialize '((a . 1) . b)) :type 'wrong-type-argument) | 70 | (should-error (json-serialize '((a . 1) . b)) :type 'wrong-type-argument) |
| 71 | (should-error (json-serialize '#1=((a . 1) . #1#)) :type 'circular-list) | 71 | (should-error (json-serialize '#1=((a . 1) . #1#)) :type 'circular-list) |
| 72 | (should-error (json-serialize '(#1=(a #1#))))) | 72 | (should-error (json-serialize '(#1=(a #1#)))) |
| 73 | |||
| 74 | (should (equal (json-serialize '(:abc [1 2 t] :def :null)) | ||
| 75 | "{\"abc\":[1,2,true],\"def\":null}")) | ||
| 76 | (should (equal (json-serialize '(abc [1 2 t] :def :null)) | ||
| 77 | "{\"abc\":[1,2,true],\"def\":null}")) | ||
| 78 | (should-error (json-serialize '#1=(:a 1 . #1#)) :type 'circular-list) | ||
| 79 | (should-error (json-serialize '#1=(:a 1 :b . #1#)) :type 'circular-list) | ||
| 80 | (should-error (json-serialize '(:foo "bar" (unexpected-alist-key . 1))) | ||
| 81 | :type 'wrong-type-argument) | ||
| 82 | (should-error (json-serialize '((abc . "abc") :unexpected-plist-key "key")) | ||
| 83 | :type 'wrong-type-argument) | ||
| 84 | (should-error (json-serialize '(:foo bar :odd-numbered)) | ||
| 85 | :type 'wrong-type-argument) | ||
| 86 | (should (equal | ||
| 87 | (json-serialize | ||
| 88 | (list :detect-hash-table #s(hash-table test equal data ("bla" "ble")) | ||
| 89 | :detect-alist `((bla . "ble")) | ||
| 90 | :detect-plist `(:bla "ble"))) | ||
| 91 | "\ | ||
| 92 | {\ | ||
| 93 | \"detect-hash-table\":{\"bla\":\"ble\"},\ | ||
| 94 | \"detect-alist\":{\"bla\":\"ble\"},\ | ||
| 95 | \"detect-plist\":{\"bla\":\"ble\"}\ | ||
| 96 | }"))) | ||
| 73 | 97 | ||
| 74 | (ert-deftest json-serialize/object-with-duplicate-keys () | 98 | (ert-deftest json-serialize/object-with-duplicate-keys () |
| 75 | (skip-unless (fboundp 'json-serialize)) | 99 | (skip-unless (fboundp 'json-serialize)) |
| @@ -89,7 +113,9 @@ | |||
| 89 | (should (equal (cl-sort (map-pairs actual) #'string< :key #'car) | 113 | (should (equal (cl-sort (map-pairs actual) #'string< :key #'car) |
| 90 | '(("abc" . [9 :false]) ("def" . :null))))) | 114 | '(("abc" . [9 :false]) ("def" . :null))))) |
| 91 | (should (equal (json-parse-string input :object-type 'alist) | 115 | (should (equal (json-parse-string input :object-type 'alist) |
| 92 | '((abc . [9 :false]) (def . :null)))))) | 116 | '((abc . [9 :false]) (def . :null)))) |
| 117 | (should (equal (json-parse-string input :object-type 'plist) | ||
| 118 | '(:abc [9 :false] :def :null))))) | ||
| 93 | 119 | ||
| 94 | (ert-deftest json-parse-string/string () | 120 | (ert-deftest json-parse-string/string () |
| 95 | (skip-unless (fboundp 'json-parse-string)) | 121 | (skip-unless (fboundp 'json-parse-string)) |