forked from melpa/melpa
-
Notifications
You must be signed in to change notification settings - Fork 0
/
json-fix.el
36 lines (28 loc) · 1.14 KB
/
json-fix.el
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
;;; Fixes for json.el such that integer plist / alist keys are rendered as strings, in order to comply with the json spec
(require 'json)
(require 'cl-lib)
(defun json-encode-key-value-pair (pair)
"Encode a (key . value) PAIR as JSON, ensuring that key is encoded into a string."
(let ((encoded-key (json-encode (car pair))))
(format "%s:%s"
(if (string-match "^\"" encoded-key)
encoded-key
(json-encode-string encoded-key))
(json-encode (cdr pair)))))
(defun json-encode-hash-table (hash-table)
"Return a JSON representation of HASH-TABLE."
(json-encode-alist (maphash 'cons hash-table)))
;; List encoding (including alists and plists)
(defun json-encode-alist (alist)
"Return a JSON representation of ALIST."
(format "{%s}"
(json-join (mapcar 'json-encode-key-value-pair
alist) ", ")))
(defun json-encode-plist (plist)
"Return a JSON representation of PLIST."
(json-encode-alist
(cl-loop while plist
collect (cons (car plist) (cadr plist))
do (setf plist (cddr plist)))))
(provide 'json-fix)
;;; json-fix.el ends here