json.el 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535
  1. ;;; json.el --- JavaScript Object Notation parser / generator
  2. ;; Copyright (C) 2006-2012 Free Software Foundation, Inc.
  3. ;; Author: Edward O'Connor <ted@oconnor.cx>
  4. ;; Version: 1.3
  5. ;; Keywords: convenience
  6. ;; This file is part of GNU Emacs.
  7. ;; GNU Emacs is free software: you can redistribute it and/or modify
  8. ;; it under the terms of the GNU General Public License as published by
  9. ;; the Free Software Foundation, either version 3 of the License, or
  10. ;; (at your option) any later version.
  11. ;; GNU Emacs is distributed in the hope that it will be useful,
  12. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. ;; GNU General Public License for more details.
  15. ;; You should have received a copy of the GNU General Public License
  16. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  17. ;;; Commentary:
  18. ;; This is a library for parsing and generating JSON (JavaScript Object
  19. ;; Notation).
  20. ;; Learn all about JSON here: <URL:http://json.org/>.
  21. ;; The user-serviceable entry points for the parser are the functions
  22. ;; `json-read' and `json-read-from-string'. The encoder has a single
  23. ;; entry point, `json-encode'.
  24. ;; Since there are several natural representations of key-value pair
  25. ;; mappings in elisp (alist, plist, hash-table), `json-read' allows you
  26. ;; to specify which you'd prefer (see `json-object-type' and
  27. ;; `json-array-type').
  28. ;; Similarly, since `false' and `null' are distinct in JSON, you can
  29. ;; distinguish them by binding `json-false' and `json-null' as desired.
  30. ;;; History:
  31. ;; 2006-03-11 - Initial version.
  32. ;; 2006-03-13 - Added JSON generation in addition to parsing. Various
  33. ;; other cleanups, bugfixes, and improvements.
  34. ;; 2006-12-29 - XEmacs support, from Aidan Kehoe <kehoea@parhasard.net>.
  35. ;; 2008-02-21 - Installed in GNU Emacs.
  36. ;; 2011-10-17 - Patch `json-alist-p' and `json-plist-p' to avoid recursion -tzz
  37. ;;; Code:
  38. (eval-when-compile (require 'cl))
  39. ;; Compatibility code
  40. (defalias 'json-encode-char0 'encode-char)
  41. (defalias 'json-decode-char0 'decode-char)
  42. ;; Parameters
  43. (defvar json-object-type 'alist
  44. "Type to convert JSON objects to.
  45. Must be one of `alist', `plist', or `hash-table'. Consider let-binding
  46. this around your call to `json-read' instead of `setq'ing it.")
  47. (defvar json-array-type 'vector
  48. "Type to convert JSON arrays to.
  49. Must be one of `vector' or `list'. Consider let-binding this around
  50. your call to `json-read' instead of `setq'ing it.")
  51. (defvar json-key-type nil
  52. "Type to convert JSON keys to.
  53. Must be one of `string', `symbol', `keyword', or nil.
  54. If nil, `json-read' will guess the type based on the value of
  55. `json-object-type':
  56. If `json-object-type' is: nil will be interpreted as:
  57. `hash-table' `string'
  58. `alist' `symbol'
  59. `plist' `keyword'
  60. Note that values other than `string' might behave strangely for
  61. Sufficiently Weird keys. Consider let-binding this around your call to
  62. `json-read' instead of `setq'ing it.")
  63. (defvar json-false :json-false
  64. "Value to use when reading JSON `false'.
  65. If this has the same value as `json-null', you might not be able to tell
  66. the difference between `false' and `null'. Consider let-binding this
  67. around your call to `json-read' instead of `setq'ing it.")
  68. (defvar json-null nil
  69. "Value to use when reading JSON `null'.
  70. If this has the same value as `json-false', you might not be able to
  71. tell the difference between `false' and `null'. Consider let-binding
  72. this around your call to `json-read' instead of `setq'ing it.")
  73. ;;; Utilities
  74. (defun json-join (strings separator)
  75. "Join STRINGS with SEPARATOR."
  76. (mapconcat 'identity strings separator))
  77. (defun json-alist-p (list)
  78. "Non-null if and only if LIST is an alist."
  79. (while (consp list)
  80. (setq list (if (consp (car list))
  81. (cdr list)
  82. 'not-alist)))
  83. (null list))
  84. (defun json-plist-p (list)
  85. "Non-null if and only if LIST is a plist."
  86. (while (consp list)
  87. (setq list (if (and (keywordp (car list))
  88. (consp (cdr list)))
  89. (cddr list)
  90. 'not-plist)))
  91. (null list))
  92. ;; Reader utilities
  93. (defsubst json-advance (&optional n)
  94. "Skip past the following N characters."
  95. (forward-char n))
  96. (defsubst json-peek ()
  97. "Return the character at point."
  98. (let ((char (char-after (point))))
  99. (or char :json-eof)))
  100. (defsubst json-pop ()
  101. "Advance past the character at point, returning it."
  102. (let ((char (json-peek)))
  103. (if (eq char :json-eof)
  104. (signal 'end-of-file nil)
  105. (json-advance)
  106. char)))
  107. (defun json-skip-whitespace ()
  108. "Skip past the whitespace at point."
  109. (skip-chars-forward "\t\r\n\f\b "))
  110. ;; Error conditions
  111. (put 'json-error 'error-message "Unknown JSON error")
  112. (put 'json-error 'error-conditions '(json-error error))
  113. (put 'json-readtable-error 'error-message "JSON readtable error")
  114. (put 'json-readtable-error 'error-conditions
  115. '(json-readtable-error json-error error))
  116. (put 'json-unknown-keyword 'error-message "Unrecognized keyword")
  117. (put 'json-unknown-keyword 'error-conditions
  118. '(json-unknown-keyword json-error error))
  119. (put 'json-number-format 'error-message "Invalid number format")
  120. (put 'json-number-format 'error-conditions
  121. '(json-number-format json-error error))
  122. (put 'json-string-escape 'error-message "Bad Unicode escape")
  123. (put 'json-string-escape 'error-conditions
  124. '(json-string-escape json-error error))
  125. (put 'json-string-format 'error-message "Bad string format")
  126. (put 'json-string-format 'error-conditions
  127. '(json-string-format json-error error))
  128. (put 'json-object-format 'error-message "Bad JSON object")
  129. (put 'json-object-format 'error-conditions
  130. '(json-object-format json-error error))
  131. ;;; Keywords
  132. (defvar json-keywords '("true" "false" "null")
  133. "List of JSON keywords.")
  134. ;; Keyword parsing
  135. (defun json-read-keyword (keyword)
  136. "Read a JSON keyword at point.
  137. KEYWORD is the keyword expected."
  138. (unless (member keyword json-keywords)
  139. (signal 'json-unknown-keyword (list keyword)))
  140. (mapc (lambda (char)
  141. (unless (char-equal char (json-peek))
  142. (signal 'json-unknown-keyword
  143. (list (save-excursion
  144. (backward-word 1)
  145. (thing-at-point 'word)))))
  146. (json-advance))
  147. keyword)
  148. (unless (looking-at "\\(\\s-\\|[],}]\\|$\\)")
  149. (signal 'json-unknown-keyword
  150. (list (save-excursion
  151. (backward-word 1)
  152. (thing-at-point 'word)))))
  153. (cond ((string-equal keyword "true") t)
  154. ((string-equal keyword "false") json-false)
  155. ((string-equal keyword "null") json-null)))
  156. ;; Keyword encoding
  157. (defun json-encode-keyword (keyword)
  158. "Encode KEYWORD as a JSON value."
  159. (cond ((eq keyword t) "true")
  160. ((eq keyword json-false) "false")
  161. ((eq keyword json-null) "null")))
  162. ;;; Numbers
  163. ;; Number parsing
  164. (defun json-read-number (&optional sign)
  165. "Read the JSON number following point.
  166. The optional SIGN argument is for internal use.
  167. N.B.: Only numbers which can fit in Emacs Lisp's native number
  168. representation will be parsed correctly."
  169. ;; If SIGN is non-nil, the number is explicitly signed.
  170. (let ((number-regexp
  171. "\\([0-9]+\\)?\\(\\.[0-9]+\\)?\\([Ee][+-]?[0-9]+\\)?"))
  172. (cond ((and (null sign) (char-equal (json-peek) ?-))
  173. (json-advance)
  174. (- (json-read-number t)))
  175. ((and (null sign) (char-equal (json-peek) ?+))
  176. (json-advance)
  177. (json-read-number t))
  178. ((and (looking-at number-regexp)
  179. (or (match-beginning 1)
  180. (match-beginning 2)))
  181. (goto-char (match-end 0))
  182. (string-to-number (match-string 0)))
  183. (t (signal 'json-number-format (list (point)))))))
  184. ;; Number encoding
  185. (defun json-encode-number (number)
  186. "Return a JSON representation of NUMBER."
  187. (format "%s" number))
  188. ;;; Strings
  189. (defvar json-special-chars
  190. '((?\" . ?\")
  191. (?\\ . ?\\)
  192. (?/ . ?/)
  193. (?b . ?\b)
  194. (?f . ?\f)
  195. (?n . ?\n)
  196. (?r . ?\r)
  197. (?t . ?\t))
  198. "Characters which are escaped in JSON, with their elisp counterparts.")
  199. ;; String parsing
  200. (defun json-read-escaped-char ()
  201. "Read the JSON string escaped character at point."
  202. ;; Skip over the '\'
  203. (json-advance)
  204. (let* ((char (json-pop))
  205. (special (assq char json-special-chars)))
  206. (cond
  207. (special (cdr special))
  208. ((not (eq char ?u)) char)
  209. ((looking-at "[0-9A-Fa-f][0-9A-Fa-f][0-9A-Fa-f][0-9A-Fa-f]")
  210. (let ((hex (match-string 0)))
  211. (json-advance 4)
  212. (json-decode-char0 'ucs (string-to-number hex 16))))
  213. (t
  214. (signal 'json-string-escape (list (point)))))))
  215. (defun json-read-string ()
  216. "Read the JSON string at point."
  217. (unless (char-equal (json-peek) ?\")
  218. (signal 'json-string-format (list "doesn't start with '\"'!")))
  219. ;; Skip over the '"'
  220. (json-advance)
  221. (let ((characters '())
  222. (char (json-peek)))
  223. (while (not (char-equal char ?\"))
  224. (push (if (char-equal char ?\\)
  225. (json-read-escaped-char)
  226. (json-pop))
  227. characters)
  228. (setq char (json-peek)))
  229. ;; Skip over the '"'
  230. (json-advance)
  231. (if characters
  232. (apply 'string (nreverse characters))
  233. "")))
  234. ;; String encoding
  235. (defun json-encode-char (char)
  236. "Encode CHAR as a JSON string."
  237. (setq char (json-encode-char0 char 'ucs))
  238. (let ((control-char (car (rassoc char json-special-chars))))
  239. (cond
  240. ;; Special JSON character (\n, \r, etc.)
  241. (control-char
  242. (format "\\%c" control-char))
  243. ;; ASCIIish printable character
  244. ((and (> char 31) (< char 161))
  245. (format "%c" char))
  246. ;; Fallback: UCS code point in \uNNNN form
  247. (t
  248. (format "\\u%04x" char)))))
  249. (defun json-encode-string (string)
  250. "Return a JSON representation of STRING."
  251. (format "\"%s\"" (mapconcat 'json-encode-char string "")))
  252. ;;; JSON Objects
  253. (defun json-new-object ()
  254. "Create a new Elisp object corresponding to a JSON object.
  255. Please see the documentation of `json-object-type'."
  256. (cond ((eq json-object-type 'hash-table)
  257. (make-hash-table :test 'equal))
  258. (t
  259. (list))))
  260. (defun json-add-to-object (object key value)
  261. "Add a new KEY -> VALUE association to OBJECT.
  262. Returns the updated object, which you should save, e.g.:
  263. (setq obj (json-add-to-object obj \"foo\" \"bar\"))
  264. Please see the documentation of `json-object-type' and `json-key-type'."
  265. (let ((json-key-type
  266. (if (eq json-key-type nil)
  267. (cdr (assq json-object-type '((hash-table . string)
  268. (alist . symbol)
  269. (plist . keyword))))
  270. json-key-type)))
  271. (setq key
  272. (cond ((eq json-key-type 'string)
  273. key)
  274. ((eq json-key-type 'symbol)
  275. (intern key))
  276. ((eq json-key-type 'keyword)
  277. (intern (concat ":" key)))))
  278. (cond ((eq json-object-type 'hash-table)
  279. (puthash key value object)
  280. object)
  281. ((eq json-object-type 'alist)
  282. (cons (cons key value) object))
  283. ((eq json-object-type 'plist)
  284. (cons key (cons value object))))))
  285. ;; JSON object parsing
  286. (defun json-read-object ()
  287. "Read the JSON object at point."
  288. ;; Skip over the "{"
  289. (json-advance)
  290. (json-skip-whitespace)
  291. ;; read key/value pairs until "}"
  292. (let ((elements (json-new-object))
  293. key value)
  294. (while (not (char-equal (json-peek) ?}))
  295. (json-skip-whitespace)
  296. (setq key (json-read-string))
  297. (json-skip-whitespace)
  298. (if (char-equal (json-peek) ?:)
  299. (json-advance)
  300. (signal 'json-object-format (list ":" (json-peek))))
  301. (setq value (json-read))
  302. (setq elements (json-add-to-object elements key value))
  303. (json-skip-whitespace)
  304. (unless (char-equal (json-peek) ?})
  305. (if (char-equal (json-peek) ?,)
  306. (json-advance)
  307. (signal 'json-object-format (list "," (json-peek))))))
  308. ;; Skip over the "}"
  309. (json-advance)
  310. elements))
  311. ;; Hash table encoding
  312. (defun json-encode-hash-table (hash-table)
  313. "Return a JSON representation of HASH-TABLE."
  314. (format "{%s}"
  315. (json-join
  316. (let (r)
  317. (maphash
  318. (lambda (k v)
  319. (push (format "%s:%s"
  320. (json-encode k)
  321. (json-encode v))
  322. r))
  323. hash-table)
  324. r)
  325. ", ")))
  326. ;; List encoding (including alists and plists)
  327. (defun json-encode-alist (alist)
  328. "Return a JSON representation of ALIST."
  329. (format "{%s}"
  330. (json-join (mapcar (lambda (cons)
  331. (format "%s:%s"
  332. (json-encode (car cons))
  333. (json-encode (cdr cons))))
  334. alist)
  335. ", ")))
  336. (defun json-encode-plist (plist)
  337. "Return a JSON representation of PLIST."
  338. (let (result)
  339. (while plist
  340. (push (concat (json-encode (car plist))
  341. ":"
  342. (json-encode (cadr plist)))
  343. result)
  344. (setq plist (cddr plist)))
  345. (concat "{" (json-join (nreverse result) ", ") "}")))
  346. (defun json-encode-list (list)
  347. "Return a JSON representation of LIST.
  348. Tries to DWIM: simple lists become JSON arrays, while alists and plists
  349. become JSON objects."
  350. (cond ((null list) "null")
  351. ((json-alist-p list) (json-encode-alist list))
  352. ((json-plist-p list) (json-encode-plist list))
  353. ((listp list) (json-encode-array list))
  354. (t
  355. (signal 'json-error (list list)))))
  356. ;;; Arrays
  357. ;; Array parsing
  358. (defun json-read-array ()
  359. "Read the JSON array at point."
  360. ;; Skip over the "["
  361. (json-advance)
  362. (json-skip-whitespace)
  363. ;; read values until "]"
  364. (let (elements)
  365. (while (not (char-equal (json-peek) ?\]))
  366. (push (json-read) elements)
  367. (json-skip-whitespace)
  368. (unless (char-equal (json-peek) ?\])
  369. (if (char-equal (json-peek) ?,)
  370. (json-advance)
  371. (signal 'json-error (list 'bleah)))))
  372. ;; Skip over the "]"
  373. (json-advance)
  374. (apply json-array-type (nreverse elements))))
  375. ;; Array encoding
  376. (defun json-encode-array (array)
  377. "Return a JSON representation of ARRAY."
  378. (concat "[" (mapconcat 'json-encode array ", ") "]"))
  379. ;;; JSON reader.
  380. (defvar json-readtable
  381. (let ((table
  382. '((?t json-read-keyword "true")
  383. (?f json-read-keyword "false")
  384. (?n json-read-keyword "null")
  385. (?{ json-read-object)
  386. (?\[ json-read-array)
  387. (?\" json-read-string))))
  388. (mapc (lambda (char)
  389. (push (list char 'json-read-number) table))
  390. '(?- ?+ ?. ?0 ?1 ?2 ?3 ?4 ?5 ?6 ?7 ?8 ?9))
  391. table)
  392. "Readtable for JSON reader.")
  393. (defun json-read ()
  394. "Parse and return the JSON object following point.
  395. Advances point just past JSON object."
  396. (json-skip-whitespace)
  397. (let ((char (json-peek)))
  398. (if (not (eq char :json-eof))
  399. (let ((record (cdr (assq char json-readtable))))
  400. (if (functionp (car record))
  401. (apply (car record) (cdr record))
  402. (signal 'json-readtable-error record)))
  403. (signal 'end-of-file nil))))
  404. ;; Syntactic sugar for the reader
  405. (defun json-read-from-string (string)
  406. "Read the JSON object contained in STRING and return it."
  407. (with-temp-buffer
  408. (insert string)
  409. (goto-char (point-min))
  410. (json-read)))
  411. (defun json-read-file (file)
  412. "Read the first JSON object contained in FILE and return it."
  413. (with-temp-buffer
  414. (insert-file-contents file)
  415. (goto-char (point-min))
  416. (json-read)))
  417. ;;; JSON encoder
  418. (defun json-encode (object)
  419. "Return a JSON representation of OBJECT as a string."
  420. (cond ((memq object (list t json-null json-false))
  421. (json-encode-keyword object))
  422. ((stringp object) (json-encode-string object))
  423. ((keywordp object) (json-encode-string
  424. (substring (symbol-name object) 1)))
  425. ((symbolp object) (json-encode-string
  426. (symbol-name object)))
  427. ((numberp object) (json-encode-number object))
  428. ((arrayp object) (json-encode-array object))
  429. ((hash-table-p object) (json-encode-hash-table object))
  430. ((listp object) (json-encode-list object))
  431. (t (signal 'json-error (list object)))))
  432. (provide 'json)
  433. ;;; json.el ends here