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