annotate lisp/json.el @ 97854:a361f652d863

(fill_gstring_body): Avoid compiler warnings.
author Eli Zaretskii <eliz@gnu.org>
date Fri, 29 Aug 2008 09:54:45 +0000
parents 5d474cdb19f8
children 4b085b844163
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
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
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."
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
126 (unless n (setq n 1))
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
127 (let ((goal (+ (point) n)))
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
128 (goto-char goal)
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
129 (when (< (point) goal)
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
130 (signal 'end-of-file nil))))
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
131
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
132 (defsubst json-peek ()
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
133 "Return the character at point."
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
134 (let ((char (char-after (point))))
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
135 (or char :json-eof)))
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
136
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
137 (defsubst json-pop ()
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
138 "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
139 (let ((char (json-peek)))
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
140 (if (eq char :json-eof)
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
141 (signal 'end-of-file nil)
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
142 (json-advance)
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
143 char)))
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 (defun json-skip-whitespace ()
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
146 "Skip past the whitespace at point."
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
147 (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
148 (goto-char (match-end 0))))
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
149
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
150
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 ;; Error conditions
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 (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
155 (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
156
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
157 (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
158 (put 'json-readtable-error 'error-conditions
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
159 '(json-readtable-error json-error error))
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
160
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
161 (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
162 (put 'json-unknown-keyword 'error-conditions
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
163 '(json-unknown-keyword json-error error))
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
164
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
165 (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
166 (put 'json-number-format 'error-conditions
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
167 '(json-number-format json-error error))
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
168
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
169 (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
170 (put 'json-string-escape 'error-conditions
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
171 '(json-string-escape json-error error))
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
172
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
173 (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
174 (put 'json-string-format 'error-conditions
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
175 '(json-string-format json-error error))
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 (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
178 (put 'json-object-format 'error-conditions
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
179 '(json-object-format json-error error))
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
180
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
181
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 ;;; Keywords
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 (defvar json-keywords '("true" "false" "null")
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
186 "List of JSON keywords.")
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
187
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
188 ;; Keyword parsing
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 (defun json-read-keyword (keyword)
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
191 "Read a JSON keyword at point.
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
192 KEYWORD is the keyword expected."
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
193 (unless (member keyword json-keywords)
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
194 (signal 'json-unknown-keyword (list keyword)))
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
195 (mapc (lambda (char)
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
196 (unless (char-equal char (json-peek))
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
197 (signal 'json-unknown-keyword
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
198 (list (save-excursion
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
199 (backward-word 1)
92086
23ee465a6963 (top-level): No need to require thingatpt.
Glenn Morris <rgm@gnu.org>
parents: 92061
diff changeset
200 (thing-at-point 'word)))))
92061
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
201 (json-advance))
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
202 keyword)
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
203 (unless (looking-at "\\(\\s-\\|[],}]\\|$\\)")
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
204 (signal 'json-unknown-keyword
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
205 (list (save-excursion
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
206 (backward-word 1)
92086
23ee465a6963 (top-level): No need to require thingatpt.
Glenn Morris <rgm@gnu.org>
parents: 92061
diff changeset
207 (thing-at-point 'word)))))
92061
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
208 (cond ((string-equal keyword "true") t)
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
209 ((string-equal keyword "false") json-false)
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
210 ((string-equal keyword "null") json-null)))
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
211
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
212 ;; Keyword encoding
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 (defun json-encode-keyword (keyword)
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
215 "Encode KEYWORD as a JSON value."
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
216 (cond ((eq keyword t) "true")
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
217 ((eq keyword json-false) "false")
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
218 ((eq keyword json-null) "null")))
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
219
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
220 ;;; Numbers
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 ;; Number parsing
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
223
97796
5d474cdb19f8 (json-read-number): New arg. Handle explicitly signed numbers.
Chong Yidong <cyd@stupidchicken.com>
parents: 94735
diff changeset
224 (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
225 "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
226 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
227
92061
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."
97796
5d474cdb19f8 (json-read-number): New arg. Handle explicitly signed numbers.
Chong Yidong <cyd@stupidchicken.com>
parents: 94735
diff changeset
230 ;; 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
231 (let ((number-regexp
5d474cdb19f8 (json-read-number): New arg. Handle explicitly signed numbers.
Chong Yidong <cyd@stupidchicken.com>
parents: 94735
diff changeset
232 "\\([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
233 (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
234 (json-advance)
5d474cdb19f8 (json-read-number): New arg. Handle explicitly signed numbers.
Chong Yidong <cyd@stupidchicken.com>
parents: 94735
diff changeset
235 (- (json-read-number t)))
5d474cdb19f8 (json-read-number): New arg. Handle explicitly signed numbers.
Chong Yidong <cyd@stupidchicken.com>
parents: 94735
diff changeset
236 ((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
237 (json-advance)
5d474cdb19f8 (json-read-number): New arg. Handle explicitly signed numbers.
Chong Yidong <cyd@stupidchicken.com>
parents: 94735
diff changeset
238 (json-read-number t))
5d474cdb19f8 (json-read-number): New arg. Handle explicitly signed numbers.
Chong Yidong <cyd@stupidchicken.com>
parents: 94735
diff changeset
239 ((and (looking-at number-regexp)
5d474cdb19f8 (json-read-number): New arg. Handle explicitly signed numbers.
Chong Yidong <cyd@stupidchicken.com>
parents: 94735
diff changeset
240 (or (match-beginning 1)
5d474cdb19f8 (json-read-number): New arg. Handle explicitly signed numbers.
Chong Yidong <cyd@stupidchicken.com>
parents: 94735
diff changeset
241 (match-beginning 2)))
92061
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
242 (goto-char (match-end 0))
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
243 (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
244 (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
245
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
246 ;; Number encoding
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 (defun json-encode-number (number)
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
249 "Return a JSON representation of NUMBER."
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
250 (format "%s" number))
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 ;;; Strings
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
253
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
254 (defvar json-special-chars
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
255 '((?\" . ?\")
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
256 (?\\ . ?\\)
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
257 (?/ . ?/)
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
258 (?b . ?\b)
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
259 (?f . ?\f)
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
260 (?n . ?\n)
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
261 (?r . ?\r)
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
262 (?t . ?\t))
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
263 "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
264
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
265 ;; String parsing
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
266
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
267 (defun json-read-escaped-char ()
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
268 "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
269 ;; Skip over the '\'
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
270 (json-advance)
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
271 (let* ((char (json-pop))
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
272 (special (assq char json-special-chars)))
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
273 (cond
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
274 (special (cdr special))
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
275 ((not (eq char ?u)) char)
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
276 ((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
277 (let ((hex (match-string 0)))
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
278 (json-advance 4)
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
279 (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
280 (t
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
281 (signal 'json-string-escape (list (point)))))))
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
282
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
283 (defun json-read-string ()
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
284 "Read the JSON string at point."
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
285 (unless (char-equal (json-peek) ?\")
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
286 (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
287 ;; Skip over the '"'
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
288 (json-advance)
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
289 (let ((characters '())
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
290 (char (json-peek)))
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
291 (while (not (char-equal char ?\"))
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
292 (push (if (char-equal char ?\\)
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
293 (json-read-escaped-char)
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
294 (json-pop))
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
295 characters)
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
296 (setq char (json-peek)))
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
297 ;; Skip over the '"'
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
298 (json-advance)
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
299 (if characters
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
300 (apply 'string (nreverse characters))
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
301 "")))
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
302
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
303 ;; String encoding
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
304
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
305 (defun json-encode-char (char)
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
306 "Encode CHAR as a JSON string."
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
307 (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
308 (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
309 (cond
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
310 ;; Special JSON character (\n, \r, etc.)
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
311 (control-char
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
312 (format "\\%c" control-char))
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
313 ;; ASCIIish printable character
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
314 ((and (> char 31) (< char 161))
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
315 (format "%c" char))
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
316 ;; 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
317 (t
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
318 (format "\\u%04x" char)))))
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-encode-string (string)
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
321 "Return a JSON representation of STRING."
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
322 (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
323
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
324 ;;; JSON Objects
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
325
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
326 (defun json-new-object ()
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
327 "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
328 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
329 (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
330 (make-hash-table :test 'equal))
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
331 (t
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
332 (list))))
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
333
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
334 (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
335 "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
336 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
337 (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
338 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
339 (let ((json-key-type
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
340 (if (eq json-key-type nil)
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
341 (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
342 (alist . symbol)
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
343 (plist . keyword))))
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
344 json-key-type)))
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
345 (setq key
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
346 (cond ((eq json-key-type 'string)
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
347 key)
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
348 ((eq json-key-type 'symbol)
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
349 (intern key))
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
350 ((eq json-key-type 'keyword)
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
351 (intern (concat ":" key)))))
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
352 (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
353 (puthash key value object)
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
354 object)
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
355 ((eq json-object-type 'alist)
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
356 (cons (cons key value) object))
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
357 ((eq json-object-type 'plist)
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
358 (cons key (cons value object))))))
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
359
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
360 ;; JSON object parsing
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
361
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
362 (defun json-read-object ()
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
363 "Read the JSON object at point."
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
364 ;; Skip over the "{"
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
365 (json-advance)
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 ;; read key/value pairs until "}"
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
368 (let ((elements (json-new-object))
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
369 key value)
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
370 (while (not (char-equal (json-peek) ?}))
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
371 (json-skip-whitespace)
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
372 (setq key (json-read-string))
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 (if (char-equal (json-peek) ?:)
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
375 (json-advance)
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
376 (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
377 (setq value (json-read))
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
378 (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
379 (json-skip-whitespace)
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
380 (unless (char-equal (json-peek) ?})
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
381 (if (char-equal (json-peek) ?,)
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
382 (json-advance)
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
383 (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
384 ;; Skip over the "}"
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
385 (json-advance)
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
386 elements))
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
387
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
388 ;; Hash table encoding
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
389
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
390 (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
391 "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
392 (format "{%s}"
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
393 (json-join
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
394 (let (r)
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
395 (maphash
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
396 (lambda (k v)
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
397 (push (format "%s:%s"
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
398 (json-encode k)
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
399 (json-encode v))
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
400 r))
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
401 hash-table)
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
402 r)
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
403 ", ")))
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
404
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
405 ;; List encoding (including alists and plists)
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
406
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
407 (defun json-encode-alist (alist)
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
408 "Return a JSON representation of ALIST."
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
409 (format "{%s}"
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
410 (json-join (mapcar (lambda (cons)
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
411 (format "%s:%s"
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
412 (json-encode (car cons))
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
413 (json-encode (cdr cons))))
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
414 alist)
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
415 ", ")))
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 (defun json-encode-plist (plist)
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
418 "Return a JSON representation of PLIST."
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
419 (let (result)
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
420 (while plist
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
421 (push (concat (json-encode (car plist))
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 (json-encode (cadr plist)))
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
424 result)
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
425 (setq plist (cddr plist)))
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
426 (concat "{" (json-join (nreverse result) ", ") "}")))
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
427
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
428 (defun json-encode-list (list)
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
429 "Return a JSON representation of LIST.
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
430 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
431 become JSON objects."
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
432 (cond ((null list) "null")
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
433 ((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
434 ((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
435 ((listp list) (json-encode-array list))
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
436 (t
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
437 (signal 'json-error (list list)))))
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
438
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
439 ;;; Arrays
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
440
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
441 ;; Array parsing
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
442
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
443 (defun json-read-array ()
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
444 "Read the JSON array at point."
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
445 ;; Skip over the "["
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
446 (json-advance)
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 ;; read values until "]"
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
449 (let (elements)
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
450 (while (not (char-equal (json-peek) ?\]))
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
451 (push (json-read) elements)
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
452 (json-skip-whitespace)
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
453 (unless (char-equal (json-peek) ?\])
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
454 (if (char-equal (json-peek) ?,)
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
455 (json-advance)
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
456 (signal 'json-error (list 'bleah)))))
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
457 ;; Skip over the "]"
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
458 (json-advance)
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
459 (apply json-array-type (nreverse elements))))
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 ;; Array encoding
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 (defun json-encode-array (array)
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
464 "Return a JSON representation of ARRAY."
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
465 (concat "[" (mapconcat 'json-encode array ", ") "]"))
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
466
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
467
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
468
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
469 ;;; JSON reader.
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
470
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
471 (defvar json-readtable
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
472 (let ((table
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
473 '((?t json-read-keyword "true")
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
474 (?f json-read-keyword "false")
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
475 (?n json-read-keyword "null")
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
476 (?{ json-read-object)
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
477 (?\[ json-read-array)
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
478 (?\" json-read-string))))
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
479 (mapc (lambda (char)
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
480 (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
481 '(?- ?+ ?. ?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
482 table)
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
483 "Readtable for JSON reader.")
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
484
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
485 (defun json-read ()
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
486 "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
487 Advances point just past JSON object."
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
488 (json-skip-whitespace)
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
489 (let ((char (json-peek)))
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
490 (if (not (eq char :json-eof))
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
491 (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
492 (if (functionp (car record))
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
493 (apply (car record) (cdr record))
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
494 (signal 'json-readtable-error record)))
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
495 (signal 'end-of-file nil))))
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
496
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
497 ;; Syntactic sugar for the reader
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
498
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
499 (defun json-read-from-string (string)
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
500 "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
501 (with-temp-buffer
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
502 (insert string)
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
503 (goto-char (point-min))
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
504 (json-read)))
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
505
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
506 (defun json-read-file (file)
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
507 "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
508 (with-temp-buffer
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
509 (insert-file-contents file)
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
510 (goto-char (point-min))
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
511 (json-read)))
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
512
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
513
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
514
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
515 ;;; JSON encoder
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
516
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
517 (defun json-encode (object)
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
518 "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
519 (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
520 (json-encode-keyword object))
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
521 ((stringp object) (json-encode-string object))
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
522 ((keywordp object) (json-encode-string
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
523 (substring (symbol-name object) 1)))
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
524 ((symbolp object) (json-encode-string
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
525 (symbol-name object)))
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
526 ((numberp object) (json-encode-number object))
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
527 ((arrayp object) (json-encode-array object))
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
528 ((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
529 ((listp object) (json-encode-list object))
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
530 (t (signal 'json-error (list object)))))
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
531
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
532 (provide 'json)
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
533
92123
e670b615aad9 Add arch tagline
Miles Bader <miles@gnu.org>
parents: 92086
diff changeset
534 ;; 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
535 ;;; json.el ends here