Commit | Line | Data |
---|---|---|
02761f85 MO |
1 | ;;; json.el --- JavaScript Object Notation parser / generator |
2 | ||
3 | ;; Copyright (C) 2006, 2007, 2008 Free Software Foundation, Inc. | |
4 | ||
5 | ;; Author: Edward O'Connor <ted@oconnor.cx> | |
6 | ;; Version: 1.2 | |
7 | ;; Keywords: convenience | |
8 | ||
9 | ;; This file is part of GNU Emacs. | |
10 | ||
eb3fa2cf | 11 | ;; GNU Emacs is free software: you can redistribute it and/or modify |
02761f85 | 12 | ;; it under the terms of the GNU General Public License as published by |
eb3fa2cf GM |
13 | ;; the Free Software Foundation, either version 3 of the License, or |
14 | ;; (at your option) any later version. | |
02761f85 MO |
15 | |
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. | |
20 | ||
21 | ;; You should have received a copy of the GNU General Public License | |
eb3fa2cf | 22 | ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. |
02761f85 MO |
23 | |
24 | ;;; Commentary: | |
25 | ||
26 | ;; This is a library for parsing and generating JSON (JavaScript Object | |
27 | ;; Notation). | |
28 | ||
29 | ;; Learn all about JSON here: <URL:http://json.org/>. | |
30 | ||
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'. | |
34 | ||
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'). | |
39 | ||
40 | ;; Similarly, since `false' and `null' are distinct in JSON, you can | |
41 | ;; distinguish them by binding `json-false' and `json-null' as desired. | |
42 | ||
43 | ;;; History: | |
44 | ||
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. | |
50 | ||
51 | ;;; Code: | |
52 | ||
53 | (eval-when-compile (require 'cl)) | |
54 | ||
55 | ;; Compatibility code | |
56 | ||
57 | (defalias 'json-encode-char0 'encode-char) | |
58 | (defalias 'json-decode-char0 'decode-char) | |
59 | ||
60 | ||
61 | ;; Parameters | |
62 | ||
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.") | |
67 | ||
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.") | |
72 | ||
73 | (defvar json-key-type nil | |
74 | "Type to convert JSON keys to. | |
75 | Must be one of `string', `symbol', `keyword', or nil. | |
76 | ||
77 | If nil, `json-read' will guess the type based on the value of | |
78 | `json-object-type': | |
79 | ||
80 | If `json-object-type' is: nil will be interpreted as: | |
81 | `hash-table' `string' | |
82 | `alist' `symbol' | |
83 | `plist' `keyword' | |
84 | ||
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.") | |
88 | ||
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.") | |
94 | ||
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.") | |
100 | ||
101 | \f | |
102 | ||
103 | ;;; Utilities | |
104 | ||
105 | (defun json-join (strings separator) | |
106 | "Join STRINGS with SEPARATOR." | |
107 | (mapconcat 'identity strings separator)) | |
108 | ||
109 | (defun json-alist-p (list) | |
398d17da | 110 | "Non-null if and only if LIST is an alist." |
02761f85 MO |
111 | (or (null list) |
112 | (and (consp (car list)) | |
113 | (json-alist-p (cdr list))))) | |
114 | ||
115 | (defun json-plist-p (list) | |
398d17da | 116 | "Non-null if and only if LIST is a plist." |
02761f85 MO |
117 | (or (null list) |
118 | (and (keywordp (car list)) | |
119 | (consp (cdr list)) | |
120 | (json-plist-p (cddr list))))) | |
121 | ||
122 | ;; Reader utilities | |
123 | ||
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)))) | |
131 | ||
132 | (defsubst json-peek () | |
133 | "Return the character at point." | |
134 | (let ((char (char-after (point)))) | |
135 | (or char :json-eof))) | |
136 | ||
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))) | |
144 | ||
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)))) | |
149 | ||
150 | \f | |
151 | ||
152 | ;; Error conditions | |
153 | ||
154 | (put 'json-error 'error-message "Unknown JSON error") | |
155 | (put 'json-error 'error-conditions '(json-error error)) | |
156 | ||
157 | (put 'json-readtable-error 'error-message "JSON readtable error") | |
158 | (put 'json-readtable-error 'error-conditions | |
159 | '(json-readtable-error json-error error)) | |
160 | ||
161 | (put 'json-unknown-keyword 'error-message "Unrecognized keyword") | |
162 | (put 'json-unknown-keyword 'error-conditions | |
163 | '(json-unknown-keyword json-error error)) | |
164 | ||
165 | (put 'json-number-format 'error-message "Invalid number format") | |
166 | (put 'json-number-format 'error-conditions | |
167 | '(json-number-format json-error error)) | |
168 | ||
169 | (put 'json-string-escape 'error-message "Bad unicode escape") | |
170 | (put 'json-string-escape 'error-conditions | |
171 | '(json-string-escape json-error error)) | |
172 | ||
173 | (put 'json-string-format 'error-message "Bad string format") | |
174 | (put 'json-string-format 'error-conditions | |
175 | '(json-string-format json-error error)) | |
176 | ||
177 | (put 'json-object-format 'error-message "Bad JSON object") | |
178 | (put 'json-object-format 'error-conditions | |
179 | '(json-object-format json-error error)) | |
180 | ||
181 | \f | |
182 | ||
183 | ;;; Keywords | |
184 | ||
185 | (defvar json-keywords '("true" "false" "null") | |
186 | "List of JSON keywords.") | |
187 | ||
188 | ;; Keyword parsing | |
189 | ||
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) | |
f62e3e0a | 200 | (thing-at-point 'word))))) |
02761f85 MO |
201 | (json-advance)) |
202 | keyword) | |
203 | (unless (looking-at "\\(\\s-\\|[],}]\\|$\\)") | |
204 | (signal 'json-unknown-keyword | |
205 | (list (save-excursion | |
206 | (backward-word 1) | |
f62e3e0a | 207 | (thing-at-point 'word))))) |
02761f85 MO |
208 | (cond ((string-equal keyword "true") t) |
209 | ((string-equal keyword "false") json-false) | |
210 | ((string-equal keyword "null") json-null))) | |
211 | ||
212 | ;; Keyword encoding | |
213 | ||
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"))) | |
219 | ||
220 | ;;; Numbers | |
221 | ||
222 | ;; Number parsing | |
223 | ||
7712319d CY |
224 | (defun json-read-number (&optional sign) |
225 | "Read the JSON number following point. | |
226 | The optional SIGN argument is for internal use. | |
227 | ||
02761f85 MO |
228 | N.B.: Only numbers which can fit in Emacs Lisp's native number |
229 | representation will be parsed correctly." | |
7712319d CY |
230 | ;; If SIGN is non-nil, the number is explicitly signed. |
231 | (let ((number-regexp | |
232 | "\\([0-9]+\\)?\\(\\.[0-9]+\\)?\\([Ee][+-]?[0-9]+\\)?")) | |
233 | (cond ((and (null sign) (char-equal (json-peek) ?-)) | |
234 | (json-advance) | |
235 | (- (json-read-number t))) | |
236 | ((and (null sign) (char-equal (json-peek) ?+)) | |
237 | (json-advance) | |
238 | (json-read-number t)) | |
239 | ((and (looking-at number-regexp) | |
240 | (or (match-beginning 1) | |
241 | (match-beginning 2))) | |
02761f85 MO |
242 | (goto-char (match-end 0)) |
243 | (string-to-number (match-string 0))) | |
7712319d | 244 | (t (signal 'json-number-format (list (point))))))) |
02761f85 MO |
245 | |
246 | ;; Number encoding | |
247 | ||
248 | (defun json-encode-number (number) | |
249 | "Return a JSON representation of NUMBER." | |
250 | (format "%s" number)) | |
251 | ||
252 | ;;; Strings | |
253 | ||
254 | (defvar json-special-chars | |
255 | '((?\" . ?\") | |
256 | (?\\ . ?\\) | |
257 | (?/ . ?/) | |
258 | (?b . ?\b) | |
259 | (?f . ?\f) | |
260 | (?n . ?\n) | |
261 | (?r . ?\r) | |
262 | (?t . ?\t)) | |
263 | "Characters which are escaped in JSON, with their elisp counterparts.") | |
264 | ||
265 | ;; String parsing | |
266 | ||
267 | (defun json-read-escaped-char () | |
268 | "Read the JSON string escaped character at point." | |
269 | ;; Skip over the '\' | |
270 | (json-advance) | |
271 | (let* ((char (json-pop)) | |
272 | (special (assq char json-special-chars))) | |
273 | (cond | |
274 | (special (cdr special)) | |
275 | ((not (eq char ?u)) char) | |
276 | ((looking-at "[0-9A-Fa-f][0-9A-Fa-f][0-9A-Fa-f][0-9A-Fa-f]") | |
277 | (let ((hex (match-string 0))) | |
278 | (json-advance 4) | |
279 | (json-decode-char0 'ucs (string-to-number hex 16)))) | |
280 | (t | |
281 | (signal 'json-string-escape (list (point))))))) | |
282 | ||
283 | (defun json-read-string () | |
284 | "Read the JSON string at point." | |
285 | (unless (char-equal (json-peek) ?\") | |
286 | (signal 'json-string-format (list "doesn't start with '\"'!"))) | |
287 | ;; Skip over the '"' | |
288 | (json-advance) | |
289 | (let ((characters '()) | |
290 | (char (json-peek))) | |
291 | (while (not (char-equal char ?\")) | |
292 | (push (if (char-equal char ?\\) | |
293 | (json-read-escaped-char) | |
294 | (json-pop)) | |
295 | characters) | |
296 | (setq char (json-peek))) | |
297 | ;; Skip over the '"' | |
298 | (json-advance) | |
299 | (if characters | |
300 | (apply 'string (nreverse characters)) | |
301 | ""))) | |
302 | ||
303 | ;; String encoding | |
304 | ||
305 | (defun json-encode-char (char) | |
306 | "Encode CHAR as a JSON string." | |
307 | (setq char (json-encode-char0 char 'ucs)) | |
308 | (let ((control-char (car (rassoc char json-special-chars)))) | |
309 | (cond | |
310 | ;; Special JSON character (\n, \r, etc.) | |
311 | (control-char | |
312 | (format "\\%c" control-char)) | |
313 | ;; ASCIIish printable character | |
314 | ((and (> char 31) (< char 161)) | |
315 | (format "%c" char)) | |
316 | ;; Fallback: UCS code point in \uNNNN form | |
317 | (t | |
318 | (format "\\u%04x" char))))) | |
319 | ||
320 | (defun json-encode-string (string) | |
321 | "Return a JSON representation of STRING." | |
322 | (format "\"%s\"" (mapconcat 'json-encode-char string ""))) | |
323 | ||
324 | ;;; JSON Objects | |
325 | ||
326 | (defun json-new-object () | |
327 | "Create a new Elisp object corresponding to a JSON object. | |
328 | Please see the documentation of `json-object-type'." | |
329 | (cond ((eq json-object-type 'hash-table) | |
330 | (make-hash-table :test 'equal)) | |
331 | (t | |
332 | (list)))) | |
333 | ||
334 | (defun json-add-to-object (object key value) | |
335 | "Add a new KEY -> VALUE association to OBJECT. | |
336 | Returns the updated object, which you should save, e.g.: | |
337 | (setq obj (json-add-to-object obj \"foo\" \"bar\")) | |
338 | Please see the documentation of `json-object-type' and `json-key-type'." | |
339 | (let ((json-key-type | |
340 | (if (eq json-key-type nil) | |
341 | (cdr (assq json-object-type '((hash-table . string) | |
342 | (alist . symbol) | |
343 | (plist . keyword)))) | |
344 | json-key-type))) | |
345 | (setq key | |
346 | (cond ((eq json-key-type 'string) | |
347 | key) | |
348 | ((eq json-key-type 'symbol) | |
349 | (intern key)) | |
350 | ((eq json-key-type 'keyword) | |
351 | (intern (concat ":" key))))) | |
352 | (cond ((eq json-object-type 'hash-table) | |
353 | (puthash key value object) | |
354 | object) | |
355 | ((eq json-object-type 'alist) | |
356 | (cons (cons key value) object)) | |
357 | ((eq json-object-type 'plist) | |
358 | (cons key (cons value object)))))) | |
359 | ||
360 | ;; JSON object parsing | |
361 | ||
362 | (defun json-read-object () | |
363 | "Read the JSON object at point." | |
364 | ;; Skip over the "{" | |
365 | (json-advance) | |
366 | (json-skip-whitespace) | |
367 | ;; read key/value pairs until "}" | |
368 | (let ((elements (json-new-object)) | |
369 | key value) | |
370 | (while (not (char-equal (json-peek) ?})) | |
371 | (json-skip-whitespace) | |
372 | (setq key (json-read-string)) | |
373 | (json-skip-whitespace) | |
374 | (if (char-equal (json-peek) ?:) | |
375 | (json-advance) | |
376 | (signal 'json-object-format (list ":" (json-peek)))) | |
377 | (setq value (json-read)) | |
378 | (setq elements (json-add-to-object elements key value)) | |
379 | (json-skip-whitespace) | |
380 | (unless (char-equal (json-peek) ?}) | |
381 | (if (char-equal (json-peek) ?,) | |
382 | (json-advance) | |
383 | (signal 'json-object-format (list "," (json-peek)))))) | |
384 | ;; Skip over the "}" | |
385 | (json-advance) | |
386 | elements)) | |
387 | ||
388 | ;; Hash table encoding | |
389 | ||
390 | (defun json-encode-hash-table (hash-table) | |
391 | "Return a JSON representation of HASH-TABLE." | |
392 | (format "{%s}" | |
393 | (json-join | |
394 | (let (r) | |
395 | (maphash | |
396 | (lambda (k v) | |
397 | (push (format "%s:%s" | |
398 | (json-encode k) | |
399 | (json-encode v)) | |
400 | r)) | |
401 | hash-table) | |
402 | r) | |
403 | ", "))) | |
404 | ||
405 | ;; List encoding (including alists and plists) | |
406 | ||
407 | (defun json-encode-alist (alist) | |
408 | "Return a JSON representation of ALIST." | |
409 | (format "{%s}" | |
410 | (json-join (mapcar (lambda (cons) | |
411 | (format "%s:%s" | |
412 | (json-encode (car cons)) | |
413 | (json-encode (cdr cons)))) | |
414 | alist) | |
415 | ", "))) | |
416 | ||
417 | (defun json-encode-plist (plist) | |
418 | "Return a JSON representation of PLIST." | |
419 | (let (result) | |
420 | (while plist | |
421 | (push (concat (json-encode (car plist)) | |
422 | ":" | |
423 | (json-encode (cadr plist))) | |
424 | result) | |
425 | (setq plist (cddr plist))) | |
426 | (concat "{" (json-join (nreverse result) ", ") "}"))) | |
427 | ||
428 | (defun json-encode-list (list) | |
429 | "Return a JSON representation of LIST. | |
430 | Tries to DWIM: simple lists become JSON arrays, while alists and plists | |
431 | become JSON objects." | |
432 | (cond ((null list) "null") | |
433 | ((json-alist-p list) (json-encode-alist list)) | |
434 | ((json-plist-p list) (json-encode-plist list)) | |
435 | ((listp list) (json-encode-array list)) | |
436 | (t | |
437 | (signal 'json-error (list list))))) | |
438 | ||
439 | ;;; Arrays | |
440 | ||
441 | ;; Array parsing | |
442 | ||
443 | (defun json-read-array () | |
444 | "Read the JSON array at point." | |
445 | ;; Skip over the "[" | |
446 | (json-advance) | |
447 | (json-skip-whitespace) | |
448 | ;; read values until "]" | |
449 | (let (elements) | |
450 | (while (not (char-equal (json-peek) ?\])) | |
451 | (push (json-read) elements) | |
452 | (json-skip-whitespace) | |
453 | (unless (char-equal (json-peek) ?\]) | |
454 | (if (char-equal (json-peek) ?,) | |
455 | (json-advance) | |
456 | (signal 'json-error (list 'bleah))))) | |
457 | ;; Skip over the "]" | |
458 | (json-advance) | |
459 | (apply json-array-type (nreverse elements)))) | |
460 | ||
461 | ;; Array encoding | |
462 | ||
463 | (defun json-encode-array (array) | |
464 | "Return a JSON representation of ARRAY." | |
465 | (concat "[" (mapconcat 'json-encode array ", ") "]")) | |
466 | ||
467 | \f | |
468 | ||
469 | ;;; JSON reader. | |
470 | ||
471 | (defvar json-readtable | |
472 | (let ((table | |
473 | '((?t json-read-keyword "true") | |
474 | (?f json-read-keyword "false") | |
475 | (?n json-read-keyword "null") | |
476 | (?{ json-read-object) | |
477 | (?\[ json-read-array) | |
478 | (?\" json-read-string)))) | |
479 | (mapc (lambda (char) | |
480 | (push (list char 'json-read-number) table)) | |
7712319d | 481 | '(?- ?+ ?. ?0 ?1 ?2 ?3 ?4 ?5 ?6 ?7 ?8 ?9)) |
02761f85 MO |
482 | table) |
483 | "Readtable for JSON reader.") | |
484 | ||
485 | (defun json-read () | |
486 | "Parse and return the JSON object following point. | |
487 | Advances point just past JSON object." | |
488 | (json-skip-whitespace) | |
489 | (let ((char (json-peek))) | |
490 | (if (not (eq char :json-eof)) | |
491 | (let ((record (cdr (assq char json-readtable)))) | |
492 | (if (functionp (car record)) | |
493 | (apply (car record) (cdr record)) | |
494 | (signal 'json-readtable-error record))) | |
495 | (signal 'end-of-file nil)))) | |
496 | ||
497 | ;; Syntactic sugar for the reader | |
498 | ||
499 | (defun json-read-from-string (string) | |
500 | "Read the JSON object contained in STRING and return it." | |
501 | (with-temp-buffer | |
502 | (insert string) | |
503 | (goto-char (point-min)) | |
504 | (json-read))) | |
505 | ||
506 | (defun json-read-file (file) | |
507 | "Read the first JSON object contained in FILE and return it." | |
508 | (with-temp-buffer | |
509 | (insert-file-contents file) | |
510 | (goto-char (point-min)) | |
511 | (json-read))) | |
512 | ||
513 | \f | |
514 | ||
515 | ;;; JSON encoder | |
516 | ||
517 | (defun json-encode (object) | |
518 | "Return a JSON representation of OBJECT as a string." | |
519 | (cond ((memq object (list t json-null json-false)) | |
520 | (json-encode-keyword object)) | |
521 | ((stringp object) (json-encode-string object)) | |
522 | ((keywordp object) (json-encode-string | |
523 | (substring (symbol-name object) 1))) | |
524 | ((symbolp object) (json-encode-string | |
525 | (symbol-name object))) | |
526 | ((numberp object) (json-encode-number object)) | |
527 | ((arrayp object) (json-encode-array object)) | |
528 | ((hash-table-p object) (json-encode-hash-table object)) | |
529 | ((listp object) (json-encode-list object)) | |
530 | (t (signal 'json-error (list object))))) | |
531 | ||
532 | (provide 'json) | |
533 | ||
28720621 | 534 | ;; arch-tag: 15f6e4c8-b831-4172-8749-bbc680c50ea1 |
02761f85 | 535 | ;;; json.el ends here |