Commit | Line | Data |
---|---|---|
02761f85 MO |
1 | ;;; json.el --- JavaScript Object Notation parser / generator |
2 | ||
acaf905b | 3 | ;; Copyright (C) 2006-2012 Free Software Foundation, Inc. |
02761f85 MO |
4 | |
5 | ;; Author: Edward O'Connor <ted@oconnor.cx> | |
0bc06380 | 6 | ;; Version: 1.3 |
02761f85 MO |
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. | |
0bc06380 | 50 | ;; 2011-10-17 - Patch `json-alist-p' and `json-plist-p' to avoid recursion -tzz |
02761f85 MO |
51 | |
52 | ;;; Code: | |
53 | ||
02761f85 MO |
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. | |
c8de140b | 65 | Must be one of `alist', `plist', or `hash-table'. Consider let-binding |
02761f85 MO |
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. | |
c8de140b | 70 | Must be one of `vector' or `list'. Consider let-binding this around |
02761f85 MO |
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 | |
c8de140b | 86 | Sufficiently Weird keys. Consider let-binding this around your call to |
02761f85 MO |
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 | |
c8de140b | 92 | the difference between `false' and `null'. Consider let-binding this |
02761f85 MO |
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 | |
c8de140b | 98 | tell the difference between `false' and `null'. Consider let-binding |
02761f85 MO |
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." |
0bc06380 TZ |
111 | (while (consp list) |
112 | (setq list (if (consp (car list)) | |
113 | (cdr list) | |
114 | 'not-alist))) | |
115 | (null list)) | |
02761f85 MO |
116 | |
117 | (defun json-plist-p (list) | |
398d17da | 118 | "Non-null if and only if LIST is a plist." |
0bc06380 TZ |
119 | (while (consp list) |
120 | (setq list (if (and (keywordp (car list)) | |
121 | (consp (cdr list))) | |
122 | (cddr list) | |
123 | 'not-plist))) | |
124 | (null list)) | |
02761f85 MO |
125 | |
126 | ;; Reader utilities | |
127 | ||
128 | (defsubst json-advance (&optional n) | |
129 | "Skip past the following N characters." | |
ac611f4f | 130 | (forward-char n)) |
02761f85 MO |
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." | |
b111d5d0 | 147 | (skip-chars-forward "\t\r\n\f\b ")) |
02761f85 MO |
148 | |
149 | \f | |
150 | ||
151 | ;; Error conditions | |
152 | ||
153 | (put 'json-error 'error-message "Unknown JSON error") | |
154 | (put 'json-error 'error-conditions '(json-error error)) | |
155 | ||
156 | (put 'json-readtable-error 'error-message "JSON readtable error") | |
157 | (put 'json-readtable-error 'error-conditions | |
158 | '(json-readtable-error json-error error)) | |
159 | ||
160 | (put 'json-unknown-keyword 'error-message "Unrecognized keyword") | |
161 | (put 'json-unknown-keyword 'error-conditions | |
162 | '(json-unknown-keyword json-error error)) | |
163 | ||
164 | (put 'json-number-format 'error-message "Invalid number format") | |
165 | (put 'json-number-format 'error-conditions | |
166 | '(json-number-format json-error error)) | |
167 | ||
fe7a3057 | 168 | (put 'json-string-escape 'error-message "Bad Unicode escape") |
02761f85 MO |
169 | (put 'json-string-escape 'error-conditions |
170 | '(json-string-escape json-error error)) | |
171 | ||
172 | (put 'json-string-format 'error-message "Bad string format") | |
173 | (put 'json-string-format 'error-conditions | |
174 | '(json-string-format json-error error)) | |
175 | ||
94e0e559 EC |
176 | (put 'json-key-format 'error-message "Bad JSON object key") |
177 | (put 'json-key-format 'error-conditions | |
178 | '(json-key-format json-error error)) | |
179 | ||
02761f85 MO |
180 | (put 'json-object-format 'error-message "Bad JSON object") |
181 | (put 'json-object-format 'error-conditions | |
182 | '(json-object-format json-error error)) | |
183 | ||
184 | \f | |
185 | ||
186 | ;;; Keywords | |
187 | ||
188 | (defvar json-keywords '("true" "false" "null") | |
189 | "List of JSON keywords.") | |
190 | ||
191 | ;; Keyword parsing | |
192 | ||
193 | (defun json-read-keyword (keyword) | |
194 | "Read a JSON keyword at point. | |
195 | KEYWORD is the keyword expected." | |
196 | (unless (member keyword json-keywords) | |
197 | (signal 'json-unknown-keyword (list keyword))) | |
198 | (mapc (lambda (char) | |
199 | (unless (char-equal char (json-peek)) | |
200 | (signal 'json-unknown-keyword | |
201 | (list (save-excursion | |
202 | (backward-word 1) | |
f62e3e0a | 203 | (thing-at-point 'word))))) |
02761f85 MO |
204 | (json-advance)) |
205 | keyword) | |
206 | (unless (looking-at "\\(\\s-\\|[],}]\\|$\\)") | |
207 | (signal 'json-unknown-keyword | |
208 | (list (save-excursion | |
209 | (backward-word 1) | |
f62e3e0a | 210 | (thing-at-point 'word))))) |
02761f85 MO |
211 | (cond ((string-equal keyword "true") t) |
212 | ((string-equal keyword "false") json-false) | |
213 | ((string-equal keyword "null") json-null))) | |
214 | ||
215 | ;; Keyword encoding | |
216 | ||
217 | (defun json-encode-keyword (keyword) | |
218 | "Encode KEYWORD as a JSON value." | |
219 | (cond ((eq keyword t) "true") | |
220 | ((eq keyword json-false) "false") | |
221 | ((eq keyword json-null) "null"))) | |
222 | ||
223 | ;;; Numbers | |
224 | ||
225 | ;; Number parsing | |
226 | ||
7712319d CY |
227 | (defun json-read-number (&optional sign) |
228 | "Read the JSON number following point. | |
c8de140b | 229 | The optional SIGN argument is for internal use. |
7712319d | 230 | |
02761f85 MO |
231 | N.B.: Only numbers which can fit in Emacs Lisp's native number |
232 | representation will be parsed correctly." | |
7712319d CY |
233 | ;; If SIGN is non-nil, the number is explicitly signed. |
234 | (let ((number-regexp | |
235 | "\\([0-9]+\\)?\\(\\.[0-9]+\\)?\\([Ee][+-]?[0-9]+\\)?")) | |
236 | (cond ((and (null sign) (char-equal (json-peek) ?-)) | |
237 | (json-advance) | |
238 | (- (json-read-number t))) | |
239 | ((and (null sign) (char-equal (json-peek) ?+)) | |
240 | (json-advance) | |
241 | (json-read-number t)) | |
242 | ((and (looking-at number-regexp) | |
243 | (or (match-beginning 1) | |
244 | (match-beginning 2))) | |
02761f85 MO |
245 | (goto-char (match-end 0)) |
246 | (string-to-number (match-string 0))) | |
7712319d | 247 | (t (signal 'json-number-format (list (point))))))) |
02761f85 MO |
248 | |
249 | ;; Number encoding | |
250 | ||
251 | (defun json-encode-number (number) | |
252 | "Return a JSON representation of NUMBER." | |
253 | (format "%s" number)) | |
254 | ||
255 | ;;; Strings | |
256 | ||
257 | (defvar json-special-chars | |
258 | '((?\" . ?\") | |
259 | (?\\ . ?\\) | |
260 | (?/ . ?/) | |
261 | (?b . ?\b) | |
262 | (?f . ?\f) | |
263 | (?n . ?\n) | |
264 | (?r . ?\r) | |
265 | (?t . ?\t)) | |
266 | "Characters which are escaped in JSON, with their elisp counterparts.") | |
267 | ||
268 | ;; String parsing | |
269 | ||
270 | (defun json-read-escaped-char () | |
271 | "Read the JSON string escaped character at point." | |
272 | ;; Skip over the '\' | |
273 | (json-advance) | |
274 | (let* ((char (json-pop)) | |
275 | (special (assq char json-special-chars))) | |
276 | (cond | |
277 | (special (cdr special)) | |
278 | ((not (eq char ?u)) char) | |
279 | ((looking-at "[0-9A-Fa-f][0-9A-Fa-f][0-9A-Fa-f][0-9A-Fa-f]") | |
280 | (let ((hex (match-string 0))) | |
281 | (json-advance 4) | |
282 | (json-decode-char0 'ucs (string-to-number hex 16)))) | |
283 | (t | |
284 | (signal 'json-string-escape (list (point))))))) | |
285 | ||
286 | (defun json-read-string () | |
287 | "Read the JSON string at point." | |
288 | (unless (char-equal (json-peek) ?\") | |
289 | (signal 'json-string-format (list "doesn't start with '\"'!"))) | |
290 | ;; Skip over the '"' | |
291 | (json-advance) | |
292 | (let ((characters '()) | |
293 | (char (json-peek))) | |
294 | (while (not (char-equal char ?\")) | |
295 | (push (if (char-equal char ?\\) | |
296 | (json-read-escaped-char) | |
297 | (json-pop)) | |
298 | characters) | |
299 | (setq char (json-peek))) | |
300 | ;; Skip over the '"' | |
301 | (json-advance) | |
302 | (if characters | |
303 | (apply 'string (nreverse characters)) | |
304 | ""))) | |
305 | ||
306 | ;; String encoding | |
307 | ||
308 | (defun json-encode-char (char) | |
309 | "Encode CHAR as a JSON string." | |
310 | (setq char (json-encode-char0 char 'ucs)) | |
311 | (let ((control-char (car (rassoc char json-special-chars)))) | |
312 | (cond | |
9cad61d6 | 313 | ;; Special JSON character (\n, \r, etc.). |
02761f85 MO |
314 | (control-char |
315 | (format "\\%c" control-char)) | |
9cad61d6 | 316 | ;; ASCIIish printable character. |
e28e67b3 | 317 | ((and (> char 31) (< char 127)) |
02761f85 | 318 | (format "%c" char)) |
9cad61d6 | 319 | ;; Fallback: UCS code point in \uNNNN form. |
02761f85 MO |
320 | (t |
321 | (format "\\u%04x" char))))) | |
322 | ||
323 | (defun json-encode-string (string) | |
324 | "Return a JSON representation of STRING." | |
325 | (format "\"%s\"" (mapconcat 'json-encode-char string ""))) | |
326 | ||
94e0e559 EC |
327 | (defun json-encode-key (object) |
328 | "Return a JSON representation of OBJECT. | |
329 | If the resulting JSON object isn't a valid JSON object key, | |
330 | this signals `json-key-format'." | |
331 | (let ((encoded (json-encode object))) | |
332 | (unless (stringp (json-read-from-string encoded)) | |
333 | (signal 'json-key-format (list object))) | |
334 | encoded)) | |
335 | ||
02761f85 MO |
336 | ;;; JSON Objects |
337 | ||
338 | (defun json-new-object () | |
339 | "Create a new Elisp object corresponding to a JSON object. | |
340 | Please see the documentation of `json-object-type'." | |
341 | (cond ((eq json-object-type 'hash-table) | |
342 | (make-hash-table :test 'equal)) | |
343 | (t | |
344 | (list)))) | |
345 | ||
346 | (defun json-add-to-object (object key value) | |
347 | "Add a new KEY -> VALUE association to OBJECT. | |
348 | Returns the updated object, which you should save, e.g.: | |
349 | (setq obj (json-add-to-object obj \"foo\" \"bar\")) | |
350 | Please see the documentation of `json-object-type' and `json-key-type'." | |
351 | (let ((json-key-type | |
352 | (if (eq json-key-type nil) | |
353 | (cdr (assq json-object-type '((hash-table . string) | |
354 | (alist . symbol) | |
355 | (plist . keyword)))) | |
356 | json-key-type))) | |
357 | (setq key | |
358 | (cond ((eq json-key-type 'string) | |
359 | key) | |
360 | ((eq json-key-type 'symbol) | |
361 | (intern key)) | |
362 | ((eq json-key-type 'keyword) | |
363 | (intern (concat ":" key))))) | |
364 | (cond ((eq json-object-type 'hash-table) | |
365 | (puthash key value object) | |
366 | object) | |
367 | ((eq json-object-type 'alist) | |
368 | (cons (cons key value) object)) | |
369 | ((eq json-object-type 'plist) | |
370 | (cons key (cons value object)))))) | |
371 | ||
372 | ;; JSON object parsing | |
373 | ||
374 | (defun json-read-object () | |
375 | "Read the JSON object at point." | |
376 | ;; Skip over the "{" | |
377 | (json-advance) | |
378 | (json-skip-whitespace) | |
379 | ;; read key/value pairs until "}" | |
380 | (let ((elements (json-new-object)) | |
381 | key value) | |
382 | (while (not (char-equal (json-peek) ?})) | |
383 | (json-skip-whitespace) | |
384 | (setq key (json-read-string)) | |
385 | (json-skip-whitespace) | |
386 | (if (char-equal (json-peek) ?:) | |
387 | (json-advance) | |
388 | (signal 'json-object-format (list ":" (json-peek)))) | |
389 | (setq value (json-read)) | |
390 | (setq elements (json-add-to-object elements key value)) | |
391 | (json-skip-whitespace) | |
392 | (unless (char-equal (json-peek) ?}) | |
393 | (if (char-equal (json-peek) ?,) | |
394 | (json-advance) | |
395 | (signal 'json-object-format (list "," (json-peek)))))) | |
396 | ;; Skip over the "}" | |
397 | (json-advance) | |
398 | elements)) | |
399 | ||
400 | ;; Hash table encoding | |
401 | ||
402 | (defun json-encode-hash-table (hash-table) | |
403 | "Return a JSON representation of HASH-TABLE." | |
404 | (format "{%s}" | |
405 | (json-join | |
406 | (let (r) | |
407 | (maphash | |
408 | (lambda (k v) | |
409 | (push (format "%s:%s" | |
94e0e559 | 410 | (json-encode-key k) |
02761f85 MO |
411 | (json-encode v)) |
412 | r)) | |
413 | hash-table) | |
414 | r) | |
415 | ", "))) | |
416 | ||
417 | ;; List encoding (including alists and plists) | |
418 | ||
419 | (defun json-encode-alist (alist) | |
420 | "Return a JSON representation of ALIST." | |
421 | (format "{%s}" | |
422 | (json-join (mapcar (lambda (cons) | |
423 | (format "%s:%s" | |
94e0e559 | 424 | (json-encode-key (car cons)) |
02761f85 MO |
425 | (json-encode (cdr cons)))) |
426 | alist) | |
427 | ", "))) | |
428 | ||
429 | (defun json-encode-plist (plist) | |
430 | "Return a JSON representation of PLIST." | |
431 | (let (result) | |
432 | (while plist | |
94e0e559 | 433 | (push (concat (json-encode-key (car plist)) |
02761f85 MO |
434 | ":" |
435 | (json-encode (cadr plist))) | |
436 | result) | |
437 | (setq plist (cddr plist))) | |
438 | (concat "{" (json-join (nreverse result) ", ") "}"))) | |
439 | ||
440 | (defun json-encode-list (list) | |
441 | "Return a JSON representation of LIST. | |
442 | Tries to DWIM: simple lists become JSON arrays, while alists and plists | |
443 | become JSON objects." | |
444 | (cond ((null list) "null") | |
445 | ((json-alist-p list) (json-encode-alist list)) | |
446 | ((json-plist-p list) (json-encode-plist list)) | |
447 | ((listp list) (json-encode-array list)) | |
448 | (t | |
449 | (signal 'json-error (list list))))) | |
450 | ||
451 | ;;; Arrays | |
452 | ||
453 | ;; Array parsing | |
454 | ||
455 | (defun json-read-array () | |
456 | "Read the JSON array at point." | |
457 | ;; Skip over the "[" | |
458 | (json-advance) | |
459 | (json-skip-whitespace) | |
460 | ;; read values until "]" | |
461 | (let (elements) | |
462 | (while (not (char-equal (json-peek) ?\])) | |
463 | (push (json-read) elements) | |
464 | (json-skip-whitespace) | |
465 | (unless (char-equal (json-peek) ?\]) | |
466 | (if (char-equal (json-peek) ?,) | |
467 | (json-advance) | |
468 | (signal 'json-error (list 'bleah))))) | |
469 | ;; Skip over the "]" | |
470 | (json-advance) | |
471 | (apply json-array-type (nreverse elements)))) | |
472 | ||
473 | ;; Array encoding | |
474 | ||
475 | (defun json-encode-array (array) | |
476 | "Return a JSON representation of ARRAY." | |
477 | (concat "[" (mapconcat 'json-encode array ", ") "]")) | |
478 | ||
479 | \f | |
480 | ||
481 | ;;; JSON reader. | |
482 | ||
483 | (defvar json-readtable | |
484 | (let ((table | |
485 | '((?t json-read-keyword "true") | |
486 | (?f json-read-keyword "false") | |
487 | (?n json-read-keyword "null") | |
488 | (?{ json-read-object) | |
489 | (?\[ json-read-array) | |
490 | (?\" json-read-string)))) | |
491 | (mapc (lambda (char) | |
492 | (push (list char 'json-read-number) table)) | |
7712319d | 493 | '(?- ?+ ?. ?0 ?1 ?2 ?3 ?4 ?5 ?6 ?7 ?8 ?9)) |
02761f85 MO |
494 | table) |
495 | "Readtable for JSON reader.") | |
496 | ||
497 | (defun json-read () | |
498 | "Parse and return the JSON object following point. | |
499 | Advances point just past JSON object." | |
500 | (json-skip-whitespace) | |
501 | (let ((char (json-peek))) | |
502 | (if (not (eq char :json-eof)) | |
503 | (let ((record (cdr (assq char json-readtable)))) | |
504 | (if (functionp (car record)) | |
505 | (apply (car record) (cdr record)) | |
506 | (signal 'json-readtable-error record))) | |
507 | (signal 'end-of-file nil)))) | |
508 | ||
509 | ;; Syntactic sugar for the reader | |
510 | ||
511 | (defun json-read-from-string (string) | |
512 | "Read the JSON object contained in STRING and return it." | |
513 | (with-temp-buffer | |
514 | (insert string) | |
515 | (goto-char (point-min)) | |
516 | (json-read))) | |
517 | ||
518 | (defun json-read-file (file) | |
519 | "Read the first JSON object contained in FILE and return it." | |
520 | (with-temp-buffer | |
521 | (insert-file-contents file) | |
522 | (goto-char (point-min)) | |
523 | (json-read))) | |
524 | ||
525 | \f | |
526 | ||
527 | ;;; JSON encoder | |
528 | ||
529 | (defun json-encode (object) | |
530 | "Return a JSON representation of OBJECT as a string." | |
531 | (cond ((memq object (list t json-null json-false)) | |
532 | (json-encode-keyword object)) | |
533 | ((stringp object) (json-encode-string object)) | |
534 | ((keywordp object) (json-encode-string | |
535 | (substring (symbol-name object) 1))) | |
536 | ((symbolp object) (json-encode-string | |
537 | (symbol-name object))) | |
538 | ((numberp object) (json-encode-number object)) | |
539 | ((arrayp object) (json-encode-array object)) | |
540 | ((hash-table-p object) (json-encode-hash-table object)) | |
541 | ((listp object) (json-encode-list object)) | |
542 | (t (signal 'json-error (list object))))) | |
543 | ||
544 | (provide 'json) | |
545 | ||
546 | ;;; json.el ends here |