annotate lisp/json.el @ 105350:05719e53cf8b

* net/ange-ftp.el (ange-ftp-delete-directory ): Add optional parameter RECURSIVE. Implementation is missing.
author Michael Albinus <michael.albinus@gmx.de>
date Thu, 01 Oct 2009 15:33:44 +0000
parents a9dc0e7c3f2b
children 1d1d5d9bd884
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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
100908
a9dc0e7c3f2b Add 2009 to copyright years.
Glenn Morris <rgm@gnu.org>
parents: 98572
diff changeset
3 ;; Copyright (C) 2006, 2007, 2008, 2009 Free Software Foundation, Inc.
92061
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
94678
ee5932bf781d Switch to recommended form of GPLv3 permissions notice.
Glenn Morris <rgm@gnu.org>
parents: 92123
diff changeset
11 ;; GNU Emacs is free software: you can redistribute it and/or modify
92061
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
94678
ee5932bf781d Switch to recommended form of GPLv3 permissions notice.
Glenn Morris <rgm@gnu.org>
parents: 92123
diff changeset
13 ;; the Free Software Foundation, either version 3 of the License, or
ee5932bf781d Switch to recommended form of GPLv3 permissions notice.
Glenn Morris <rgm@gnu.org>
parents: 92123
diff changeset
14 ;; (at your option) any later version.
92061
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
94678
ee5932bf781d Switch to recommended form of GPLv3 permissions notice.
Glenn Morris <rgm@gnu.org>
parents: 92123
diff changeset
22 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
92061
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
23
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
24 ;;; Commentary:
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 ;; 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
27 ;; Notation).
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
28
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
29 ;; 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
30
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
31 ;; 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
32 ;; `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
33 ;; entry point, `json-encode'.
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
34
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
35 ;; 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
36 ;; 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
37 ;; 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
38 ;; `json-array-type').
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
39
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
40 ;; 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
41 ;; 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
42
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
43 ;;; History:
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 ;; 2006-03-11 - Initial version.
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
46 ;; 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
47 ;; other cleanups, bugfixes, and improvements.
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
48 ;; 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
49 ;; 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
50
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
51 ;;; Code:
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 (eval-when-compile (require 'cl))
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 ;; Compatibility code
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 (defalias 'json-encode-char0 'encode-char)
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
58 (defalias 'json-decode-char0 'decode-char)
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
59
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
60
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
61 ;; Parameters
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 (defvar json-object-type 'alist
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
64 "Type to convert JSON objects to.
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
65 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
66 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
67
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
68 (defvar json-array-type 'vector
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
69 "Type to convert JSON arrays to.
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
70 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
71 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
72
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
73 (defvar json-key-type nil
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
74 "Type to convert JSON keys to.
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
75 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
76
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
77 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
78 `json-object-type':
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
79
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
80 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
81 `hash-table' `string'
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
82 `alist' `symbol'
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
83 `plist' `keyword'
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
84
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
85 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
86 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
87 `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
88
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
89 (defvar json-false :json-false
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
90 "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
91 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
92 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
93 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
94
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
95 (defvar json-null nil
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
96 "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
97 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
98 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
99 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
100
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
101
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 ;;; Utilities
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 (defun json-join (strings separator)
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
106 "Join STRINGS with SEPARATOR."
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
107 (mapconcat 'identity strings separator))
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
108
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
109 (defun json-alist-p (list)
94735
2b39e9f51e54 (json-alist-p, json-plist-p): Don't use `iff' in docstrings.
Juanma Barranquero <lekktu@gmail.com>
parents: 94678
diff changeset
110 "Non-null if and only if LIST is an alist."
92061
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
111 (or (null list)
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
112 (and (consp (car list))
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
113 (json-alist-p (cdr list)))))
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
114
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
115 (defun json-plist-p (list)
94735
2b39e9f51e54 (json-alist-p, json-plist-p): Don't use `iff' in docstrings.
Juanma Barranquero <lekktu@gmail.com>
parents: 94678
diff changeset
116 "Non-null if and only if LIST is a plist."
92061
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
117 (or (null list)
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
118 (and (keywordp (car list))
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
119 (consp (cdr list))
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
120 (json-plist-p (cddr list)))))
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
121
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
122 ;; Reader utilities
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 (defsubst json-advance (&optional n)
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
125 "Skip past the following N characters."
98553
4b085b844163 (json-advance): Use forward-char.
Chong Yidong <cyd@stupidchicken.com>
parents: 97796
diff changeset
126 (forward-char n))
92061
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
127
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
128 (defsubst json-peek ()
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
129 "Return the character at point."
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
130 (let ((char (char-after (point))))
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
131 (or char :json-eof)))
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
132
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
133 (defsubst json-pop ()
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
134 "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
135 (let ((char (json-peek)))
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
136 (if (eq char :json-eof)
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
137 (signal 'end-of-file nil)
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
138 (json-advance)
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
139 char)))
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
140
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
141 (defun json-skip-whitespace ()
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
142 "Skip past the whitespace at point."
98572
e26dedf05927 (json-skip-whitespace): Fix last change.
Chong Yidong <cyd@stupidchicken.com>
parents: 98553
diff changeset
143 (skip-chars-forward "\t\r\n\f\b "))
92061
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
144
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
145
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 ;; Error conditions
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
148
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
149 (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
150 (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
151
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
152 (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
153 (put 'json-readtable-error 'error-conditions
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
154 '(json-readtable-error json-error error))
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-unknown-keyword 'error-message "Unrecognized keyword")
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
157 (put 'json-unknown-keyword 'error-conditions
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
158 '(json-unknown-keyword json-error error))
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
159
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
160 (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
161 (put 'json-number-format 'error-conditions
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
162 '(json-number-format json-error error))
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
163
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
164 (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
165 (put 'json-string-escape 'error-conditions
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
166 '(json-string-escape json-error error))
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
167
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
168 (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
169 (put 'json-string-format 'error-conditions
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
170 '(json-string-format json-error error))
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
171
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
172 (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
173 (put 'json-object-format 'error-conditions
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
174 '(json-object-format json-error error))
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
175
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
176
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
177
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
178 ;;; Keywords
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
179
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
180 (defvar json-keywords '("true" "false" "null")
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
181 "List of JSON keywords.")
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 ;; Keyword parsing
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 (defun json-read-keyword (keyword)
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
186 "Read a JSON keyword at point.
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
187 KEYWORD is the keyword expected."
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
188 (unless (member keyword json-keywords)
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
189 (signal 'json-unknown-keyword (list keyword)))
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
190 (mapc (lambda (char)
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
191 (unless (char-equal char (json-peek))
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
192 (signal 'json-unknown-keyword
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
193 (list (save-excursion
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
194 (backward-word 1)
92086
23ee465a6963 (top-level): No need to require thingatpt.
Glenn Morris <rgm@gnu.org>
parents: 92061
diff changeset
195 (thing-at-point 'word)))))
92061
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
196 (json-advance))
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
197 keyword)
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
198 (unless (looking-at "\\(\\s-\\|[],}]\\|$\\)")
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 (cond ((string-equal keyword "true") t)
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
204 ((string-equal keyword "false") json-false)
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
205 ((string-equal keyword "null") json-null)))
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
206
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
207 ;; Keyword encoding
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
208
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
209 (defun json-encode-keyword (keyword)
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
210 "Encode KEYWORD as a JSON value."
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
211 (cond ((eq keyword t) "true")
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
212 ((eq keyword json-false) "false")
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
213 ((eq keyword json-null) "null")))
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
214
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
215 ;;; Numbers
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
216
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
217 ;; Number parsing
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
218
97796
5d474cdb19f8 (json-read-number): New arg. Handle explicitly signed numbers.
Chong Yidong <cyd@stupidchicken.com>
parents: 94735
diff changeset
219 (defun json-read-number (&optional sign)
5d474cdb19f8 (json-read-number): New arg. Handle explicitly signed numbers.
Chong Yidong <cyd@stupidchicken.com>
parents: 94735
diff changeset
220 "Read the JSON number following point.
5d474cdb19f8 (json-read-number): New arg. Handle explicitly signed numbers.
Chong Yidong <cyd@stupidchicken.com>
parents: 94735
diff changeset
221 The optional SIGN argument is for internal use.
5d474cdb19f8 (json-read-number): New arg. Handle explicitly signed numbers.
Chong Yidong <cyd@stupidchicken.com>
parents: 94735
diff changeset
222
92061
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
223 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
224 representation will be parsed correctly."
97796
5d474cdb19f8 (json-read-number): New arg. Handle explicitly signed numbers.
Chong Yidong <cyd@stupidchicken.com>
parents: 94735
diff changeset
225 ;; If SIGN is non-nil, the number is explicitly signed.
5d474cdb19f8 (json-read-number): New arg. Handle explicitly signed numbers.
Chong Yidong <cyd@stupidchicken.com>
parents: 94735
diff changeset
226 (let ((number-regexp
5d474cdb19f8 (json-read-number): New arg. Handle explicitly signed numbers.
Chong Yidong <cyd@stupidchicken.com>
parents: 94735
diff changeset
227 "\\([0-9]+\\)?\\(\\.[0-9]+\\)?\\([Ee][+-]?[0-9]+\\)?"))
5d474cdb19f8 (json-read-number): New arg. Handle explicitly signed numbers.
Chong Yidong <cyd@stupidchicken.com>
parents: 94735
diff changeset
228 (cond ((and (null sign) (char-equal (json-peek) ?-))
5d474cdb19f8 (json-read-number): New arg. Handle explicitly signed numbers.
Chong Yidong <cyd@stupidchicken.com>
parents: 94735
diff changeset
229 (json-advance)
5d474cdb19f8 (json-read-number): New arg. Handle explicitly signed numbers.
Chong Yidong <cyd@stupidchicken.com>
parents: 94735
diff changeset
230 (- (json-read-number t)))
5d474cdb19f8 (json-read-number): New arg. Handle explicitly signed numbers.
Chong Yidong <cyd@stupidchicken.com>
parents: 94735
diff changeset
231 ((and (null sign) (char-equal (json-peek) ?+))
5d474cdb19f8 (json-read-number): New arg. Handle explicitly signed numbers.
Chong Yidong <cyd@stupidchicken.com>
parents: 94735
diff changeset
232 (json-advance)
5d474cdb19f8 (json-read-number): New arg. Handle explicitly signed numbers.
Chong Yidong <cyd@stupidchicken.com>
parents: 94735
diff changeset
233 (json-read-number t))
5d474cdb19f8 (json-read-number): New arg. Handle explicitly signed numbers.
Chong Yidong <cyd@stupidchicken.com>
parents: 94735
diff changeset
234 ((and (looking-at number-regexp)
5d474cdb19f8 (json-read-number): New arg. Handle explicitly signed numbers.
Chong Yidong <cyd@stupidchicken.com>
parents: 94735
diff changeset
235 (or (match-beginning 1)
5d474cdb19f8 (json-read-number): New arg. Handle explicitly signed numbers.
Chong Yidong <cyd@stupidchicken.com>
parents: 94735
diff changeset
236 (match-beginning 2)))
92061
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
237 (goto-char (match-end 0))
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
238 (string-to-number (match-string 0)))
97796
5d474cdb19f8 (json-read-number): New arg. Handle explicitly signed numbers.
Chong Yidong <cyd@stupidchicken.com>
parents: 94735
diff changeset
239 (t (signal 'json-number-format (list (point)))))))
92061
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
240
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
241 ;; Number encoding
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
242
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
243 (defun json-encode-number (number)
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
244 "Return a JSON representation of NUMBER."
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
245 (format "%s" number))
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
246
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
247 ;;; Strings
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
248
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
249 (defvar json-special-chars
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 (?/ . ?/)
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
253 (?b . ?\b)
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
254 (?f . ?\f)
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
255 (?n . ?\n)
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
256 (?r . ?\r)
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
257 (?t . ?\t))
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
258 "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
259
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
260 ;; String parsing
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
261
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
262 (defun json-read-escaped-char ()
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
263 "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
264 ;; Skip over the '\'
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
265 (json-advance)
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
266 (let* ((char (json-pop))
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
267 (special (assq char json-special-chars)))
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
268 (cond
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
269 (special (cdr special))
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
270 ((not (eq char ?u)) char)
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
271 ((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
272 (let ((hex (match-string 0)))
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
273 (json-advance 4)
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
274 (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
275 (t
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
276 (signal 'json-string-escape (list (point)))))))
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
277
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
278 (defun json-read-string ()
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
279 "Read the JSON string at point."
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
280 (unless (char-equal (json-peek) ?\")
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
281 (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
282 ;; Skip over the '"'
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
283 (json-advance)
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
284 (let ((characters '())
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
285 (char (json-peek)))
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
286 (while (not (char-equal char ?\"))
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
287 (push (if (char-equal char ?\\)
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
288 (json-read-escaped-char)
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
289 (json-pop))
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
290 characters)
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
291 (setq char (json-peek)))
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
292 ;; Skip over the '"'
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
293 (json-advance)
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
294 (if characters
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
295 (apply 'string (nreverse characters))
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
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
298 ;; String encoding
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
299
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
300 (defun json-encode-char (char)
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
301 "Encode CHAR as a JSON string."
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
302 (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
303 (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
304 (cond
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
305 ;; Special JSON character (\n, \r, etc.)
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
306 (control-char
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
307 (format "\\%c" control-char))
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
308 ;; ASCIIish printable character
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
309 ((and (> char 31) (< char 161))
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
310 (format "%c" char))
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
311 ;; 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
312 (t
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
313 (format "\\u%04x" char)))))
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
314
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
315 (defun json-encode-string (string)
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
316 "Return a JSON representation of STRING."
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
317 (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
318
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
319 ;;; JSON Objects
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
320
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
321 (defun json-new-object ()
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
322 "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
323 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
324 (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
325 (make-hash-table :test 'equal))
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
326 (t
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
327 (list))))
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
328
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
329 (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
330 "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
331 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
332 (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
333 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
334 (let ((json-key-type
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
335 (if (eq json-key-type nil)
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
336 (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
337 (alist . symbol)
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
338 (plist . keyword))))
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
339 json-key-type)))
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
340 (setq key
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
341 (cond ((eq json-key-type 'string)
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
342 key)
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
343 ((eq json-key-type 'symbol)
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
344 (intern key))
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
345 ((eq json-key-type 'keyword)
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
346 (intern (concat ":" key)))))
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
347 (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
348 (puthash key value object)
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
349 object)
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
350 ((eq json-object-type 'alist)
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
351 (cons (cons key value) object))
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
352 ((eq json-object-type 'plist)
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
353 (cons key (cons value object))))))
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
354
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
355 ;; JSON object parsing
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
356
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
357 (defun json-read-object ()
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
358 "Read the JSON object at point."
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
359 ;; Skip over the "{"
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
360 (json-advance)
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
361 (json-skip-whitespace)
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
362 ;; read key/value pairs until "}"
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
363 (let ((elements (json-new-object))
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
364 key value)
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
365 (while (not (char-equal (json-peek) ?}))
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
366 (json-skip-whitespace)
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
367 (setq key (json-read-string))
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
368 (json-skip-whitespace)
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
369 (if (char-equal (json-peek) ?:)
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
370 (json-advance)
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
371 (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
372 (setq value (json-read))
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
373 (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
374 (json-skip-whitespace)
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
375 (unless (char-equal (json-peek) ?})
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
376 (if (char-equal (json-peek) ?,)
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
377 (json-advance)
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
378 (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
379 ;; Skip over the "}"
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
380 (json-advance)
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
381 elements))
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
382
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
383 ;; Hash table encoding
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
384
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
385 (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
386 "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
387 (format "{%s}"
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
388 (json-join
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
389 (let (r)
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
390 (maphash
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
391 (lambda (k v)
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
392 (push (format "%s:%s"
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
393 (json-encode k)
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
394 (json-encode v))
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
395 r))
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
396 hash-table)
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
397 r)
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
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
400 ;; List encoding (including alists and plists)
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
401
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
402 (defun json-encode-alist (alist)
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
403 "Return a JSON representation of ALIST."
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
404 (format "{%s}"
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
405 (json-join (mapcar (lambda (cons)
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
406 (format "%s:%s"
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
407 (json-encode (car cons))
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
408 (json-encode (cdr cons))))
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
409 alist)
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
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
412 (defun json-encode-plist (plist)
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
413 "Return a JSON representation of PLIST."
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
414 (let (result)
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
415 (while plist
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
416 (push (concat (json-encode (car plist))
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
417 ":"
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
418 (json-encode (cadr plist)))
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
419 result)
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
420 (setq plist (cddr plist)))
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
421 (concat "{" (json-join (nreverse result) ", ") "}")))
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
422
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
423 (defun json-encode-list (list)
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
424 "Return a JSON representation of LIST.
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
425 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
426 become JSON objects."
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
427 (cond ((null list) "null")
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
428 ((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
429 ((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
430 ((listp list) (json-encode-array list))
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
431 (t
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
432 (signal 'json-error (list list)))))
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
433
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
434 ;;; Arrays
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
435
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
436 ;; Array parsing
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
437
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
438 (defun json-read-array ()
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
439 "Read the JSON array at point."
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
440 ;; Skip over the "["
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
441 (json-advance)
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
442 (json-skip-whitespace)
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
443 ;; read values until "]"
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
444 (let (elements)
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
445 (while (not (char-equal (json-peek) ?\]))
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
446 (push (json-read) elements)
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
447 (json-skip-whitespace)
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
448 (unless (char-equal (json-peek) ?\])
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
449 (if (char-equal (json-peek) ?,)
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
450 (json-advance)
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
451 (signal 'json-error (list 'bleah)))))
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
452 ;; Skip over the "]"
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
453 (json-advance)
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
454 (apply json-array-type (nreverse elements))))
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
455
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
456 ;; Array encoding
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
457
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
458 (defun json-encode-array (array)
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
459 "Return a JSON representation of ARRAY."
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
460 (concat "[" (mapconcat 'json-encode array ", ") "]"))
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
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
464 ;;; JSON reader.
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
465
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
466 (defvar json-readtable
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
467 (let ((table
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
468 '((?t json-read-keyword "true")
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
469 (?f json-read-keyword "false")
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
470 (?n json-read-keyword "null")
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
471 (?{ json-read-object)
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
472 (?\[ json-read-array)
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
473 (?\" json-read-string))))
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
474 (mapc (lambda (char)
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
475 (push (list char 'json-read-number) table))
97796
5d474cdb19f8 (json-read-number): New arg. Handle explicitly signed numbers.
Chong Yidong <cyd@stupidchicken.com>
parents: 94735
diff changeset
476 '(?- ?+ ?. ?0 ?1 ?2 ?3 ?4 ?5 ?6 ?7 ?8 ?9))
92061
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
477 table)
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
478 "Readtable for JSON reader.")
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
479
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
480 (defun json-read ()
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
481 "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
482 Advances point just past JSON object."
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
483 (json-skip-whitespace)
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
484 (let ((char (json-peek)))
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
485 (if (not (eq char :json-eof))
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
486 (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
487 (if (functionp (car record))
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
488 (apply (car record) (cdr record))
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
489 (signal 'json-readtable-error record)))
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
490 (signal 'end-of-file nil))))
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
491
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
492 ;; Syntactic sugar for the reader
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
493
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
494 (defun json-read-from-string (string)
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
495 "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
496 (with-temp-buffer
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
497 (insert string)
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
498 (goto-char (point-min))
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
499 (json-read)))
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
500
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
501 (defun json-read-file (file)
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
502 "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
503 (with-temp-buffer
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
504 (insert-file-contents file)
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
505 (goto-char (point-min))
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
506 (json-read)))
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
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
510 ;;; JSON encoder
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
511
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
512 (defun json-encode (object)
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
513 "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
514 (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
515 (json-encode-keyword object))
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
516 ((stringp object) (json-encode-string object))
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
517 ((keywordp object) (json-encode-string
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
518 (substring (symbol-name object) 1)))
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
519 ((symbolp object) (json-encode-string
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
520 (symbol-name object)))
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
521 ((numberp object) (json-encode-number object))
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
522 ((arrayp object) (json-encode-array object))
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
523 ((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
524 ((listp object) (json-encode-list object))
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
525 (t (signal 'json-error (list object)))))
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
526
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
527 (provide 'json)
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
528
92123
e670b615aad9 Add arch tagline
Miles Bader <miles@gnu.org>
parents: 92086
diff changeset
529 ;; 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
530 ;;; json.el ends here