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