annotate lisp/json.el @ 94921:c2449f1de3ac

(Vcharset_non_preferred_head): New variable. (Vcurrent_iso639_language): New variable. (syms_of_charset): Declare it as a Lisp variable. (char_charset): Don't check non preferred charsets. As a last resort, return charset_unicode. (Fset_charset_priority): Update Vcharset_non_preferred_head.
author Kenichi Handa <handa@m17n.org>
date Wed, 14 May 2008 01:22:48 +0000
parents 2b39e9f51e54
children 5d474cdb19f8
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
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
224 (defun json-read-number ()
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
225 "Read the JSON number following point.
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
226 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
227 representation will be parsed correctly."
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
228 (if (char-equal (json-peek) ?-)
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
229 (progn
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
230 (json-advance)
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
231 (- 0 (json-read-number)))
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
232 (if (looking-at "[0-9]+\\([.][0-9]+\\)?\\([eE][+-]?[0-9]+\\)?")
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
233 (progn
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
234 (goto-char (match-end 0))
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
235 (string-to-number (match-string 0)))
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
236 (signal 'json-number-format (list (point))))))
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
237
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
238 ;; Number encoding
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
239
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
240 (defun json-encode-number (number)
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
241 "Return a JSON representation of NUMBER."
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
242 (format "%s" number))
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
243
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
244 ;;; Strings
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 (defvar json-special-chars
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 (?\\ . ?\\)
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
249 (?/ . ?/)
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
250 (?b . ?\b)
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
251 (?f . ?\f)
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
252 (?n . ?\n)
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
253 (?r . ?\r)
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
254 (?t . ?\t))
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
255 "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
256
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
257 ;; String parsing
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
258
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
259 (defun json-read-escaped-char ()
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
260 "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
261 ;; Skip over the '\'
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
262 (json-advance)
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
263 (let* ((char (json-pop))
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
264 (special (assq char json-special-chars)))
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
265 (cond
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
266 (special (cdr special))
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
267 ((not (eq char ?u)) char)
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
268 ((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
269 (let ((hex (match-string 0)))
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
270 (json-advance 4)
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
271 (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
272 (t
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
273 (signal 'json-string-escape (list (point)))))))
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
274
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
275 (defun json-read-string ()
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
276 "Read the JSON string at point."
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
277 (unless (char-equal (json-peek) ?\")
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
278 (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
279 ;; Skip over the '"'
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
280 (json-advance)
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
281 (let ((characters '())
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
282 (char (json-peek)))
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
283 (while (not (char-equal char ?\"))
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
284 (push (if (char-equal char ?\\)
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
285 (json-read-escaped-char)
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
286 (json-pop))
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
287 characters)
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
288 (setq char (json-peek)))
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
289 ;; Skip over the '"'
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
290 (json-advance)
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
291 (if characters
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
292 (apply 'string (nreverse characters))
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
293 "")))
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
294
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
295 ;; String encoding
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 (defun json-encode-char (char)
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
298 "Encode CHAR as a JSON string."
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
299 (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
300 (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
301 (cond
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
302 ;; Special JSON character (\n, \r, etc.)
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
303 (control-char
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
304 (format "\\%c" control-char))
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
305 ;; ASCIIish printable character
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
306 ((and (> char 31) (< char 161))
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
307 (format "%c" char))
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
308 ;; 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
309 (t
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
310 (format "\\u%04x" char)))))
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
311
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
312 (defun json-encode-string (string)
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
313 "Return a JSON representation of STRING."
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
314 (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
315
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
316 ;;; JSON Objects
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
317
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
318 (defun json-new-object ()
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
319 "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
320 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
321 (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
322 (make-hash-table :test 'equal))
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
323 (t
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
324 (list))))
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-add-to-object (object key value)
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
327 "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
328 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
329 (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
330 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
331 (let ((json-key-type
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
332 (if (eq json-key-type nil)
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
333 (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
334 (alist . symbol)
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
335 (plist . keyword))))
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
336 json-key-type)))
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
337 (setq key
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
338 (cond ((eq json-key-type 'string)
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
339 key)
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
340 ((eq json-key-type 'symbol)
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
341 (intern key))
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
342 ((eq json-key-type 'keyword)
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
343 (intern (concat ":" key)))))
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
344 (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
345 (puthash key value object)
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
346 object)
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
347 ((eq json-object-type 'alist)
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
348 (cons (cons key value) object))
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
349 ((eq json-object-type 'plist)
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
350 (cons key (cons value object))))))
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
351
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
352 ;; JSON object parsing
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
353
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
354 (defun json-read-object ()
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
355 "Read the JSON object at point."
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
356 ;; Skip over the "{"
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
357 (json-advance)
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
358 (json-skip-whitespace)
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
359 ;; read key/value pairs until "}"
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
360 (let ((elements (json-new-object))
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
361 key value)
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
362 (while (not (char-equal (json-peek) ?}))
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
363 (json-skip-whitespace)
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
364 (setq key (json-read-string))
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
365 (json-skip-whitespace)
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
366 (if (char-equal (json-peek) ?:)
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
367 (json-advance)
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
368 (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
369 (setq value (json-read))
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
370 (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
371 (json-skip-whitespace)
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
372 (unless (char-equal (json-peek) ?})
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
373 (if (char-equal (json-peek) ?,)
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
374 (json-advance)
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
375 (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
376 ;; Skip over the "}"
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 elements))
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
379
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
380 ;; Hash table encoding
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
381
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
382 (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
383 "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
384 (format "{%s}"
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
385 (json-join
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
386 (let (r)
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
387 (maphash
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
388 (lambda (k v)
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
389 (push (format "%s:%s"
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
390 (json-encode k)
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
391 (json-encode v))
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
392 r))
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
393 hash-table)
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
394 r)
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
395 ", ")))
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
396
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
397 ;; List encoding (including alists and plists)
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 (defun json-encode-alist (alist)
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
400 "Return a JSON representation of ALIST."
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
401 (format "{%s}"
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
402 (json-join (mapcar (lambda (cons)
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
403 (format "%s:%s"
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
404 (json-encode (car cons))
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
405 (json-encode (cdr cons))))
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
406 alist)
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
407 ", ")))
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
408
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
409 (defun json-encode-plist (plist)
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
410 "Return a JSON representation of PLIST."
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
411 (let (result)
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
412 (while plist
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
413 (push (concat (json-encode (car plist))
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
414 ":"
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
415 (json-encode (cadr plist)))
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
416 result)
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
417 (setq plist (cddr plist)))
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
418 (concat "{" (json-join (nreverse result) ", ") "}")))
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
419
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
420 (defun json-encode-list (list)
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
421 "Return a JSON representation of LIST.
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
422 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
423 become JSON objects."
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
424 (cond ((null list) "null")
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
425 ((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
426 ((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
427 ((listp list) (json-encode-array list))
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
428 (t
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
429 (signal 'json-error (list list)))))
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
430
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
431 ;;; Arrays
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
432
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
433 ;; Array parsing
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
434
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
435 (defun json-read-array ()
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
436 "Read the JSON array at point."
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
437 ;; Skip over the "["
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
438 (json-advance)
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
439 (json-skip-whitespace)
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
440 ;; read values until "]"
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
441 (let (elements)
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
442 (while (not (char-equal (json-peek) ?\]))
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
443 (push (json-read) elements)
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
444 (json-skip-whitespace)
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
445 (unless (char-equal (json-peek) ?\])
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
446 (if (char-equal (json-peek) ?,)
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
447 (json-advance)
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
448 (signal 'json-error (list 'bleah)))))
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
449 ;; Skip over the "]"
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 (apply json-array-type (nreverse elements))))
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
452
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
453 ;; Array encoding
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
454
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
455 (defun json-encode-array (array)
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
456 "Return a JSON representation of ARRAY."
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
457 (concat "[" (mapconcat 'json-encode array ", ") "]"))
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
458
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
459
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 ;;; JSON reader.
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 (defvar json-readtable
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
464 (let ((table
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
465 '((?t json-read-keyword "true")
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
466 (?f json-read-keyword "false")
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
467 (?n json-read-keyword "null")
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
468 (?{ json-read-object)
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
469 (?\[ json-read-array)
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
470 (?\" json-read-string))))
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
471 (mapc (lambda (char)
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
472 (push (list char 'json-read-number) table))
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
473 '(?- ?0 ?1 ?2 ?3 ?4 ?5 ?6 ?7 ?8 ?9))
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
474 table)
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
475 "Readtable for JSON reader.")
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
476
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
477 (defun json-read ()
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
478 "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
479 Advances point just past JSON object."
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
480 (json-skip-whitespace)
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
481 (let ((char (json-peek)))
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
482 (if (not (eq char :json-eof))
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
483 (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
484 (if (functionp (car record))
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
485 (apply (car record) (cdr record))
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
486 (signal 'json-readtable-error record)))
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
487 (signal 'end-of-file nil))))
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
488
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
489 ;; Syntactic sugar for the reader
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
490
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
491 (defun json-read-from-string (string)
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
492 "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
493 (with-temp-buffer
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
494 (insert string)
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
495 (goto-char (point-min))
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
496 (json-read)))
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
497
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
498 (defun json-read-file (file)
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
499 "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
500 (with-temp-buffer
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
501 (insert-file-contents file)
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
502 (goto-char (point-min))
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
503 (json-read)))
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
504
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
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
507 ;;; JSON encoder
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 (defun json-encode (object)
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
510 "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
511 (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
512 (json-encode-keyword object))
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
513 ((stringp object) (json-encode-string object))
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
514 ((keywordp object) (json-encode-string
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
515 (substring (symbol-name object) 1)))
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
516 ((symbolp object) (json-encode-string
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
517 (symbol-name object)))
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
518 ((numberp object) (json-encode-number object))
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
519 ((arrayp object) (json-encode-array object))
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
520 ((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
521 ((listp object) (json-encode-list object))
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
522 (t (signal 'json-error (list object)))))
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
523
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
524 (provide 'json)
2b55e8843eff Move lisp/net/json.el to lisp/json.el.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
525
92123
e670b615aad9 Add arch tagline
Miles Bader <miles@gnu.org>
parents: 92086
diff changeset
526 ;; 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
527 ;;; json.el ends here