;;; json.el --- JavaScript Object Notation parser / generator
-;; Copyright (C) 2006, 2007, 2008 Free Software Foundation, Inc.
+;; Copyright (C) 2006-2013 Free Software Foundation, Inc.
;; Author: Edward O'Connor <ted@oconnor.cx>
-;; Version: 1.2
+;; Version: 1.3
;; Keywords: convenience
;; This file is part of GNU Emacs.
-;; GNU Emacs is free software; you can redistribute it and/or modify
+;; GNU Emacs is free software: you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
-;; the Free Software Foundation; either version 3, or (at your option)
-;; any later version.
+;; the Free Software Foundation, either version 3 of the License, or
+;; (at your option) any later version.
;; GNU Emacs is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; GNU General Public License for more details.
;; You should have received a copy of the GNU General Public License
-;; along with GNU Emacs; see the file COPYING. If not, write to the
-;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
-;; Boston, MA 02110-1301, USA.
+;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
;;; Commentary:
;; other cleanups, bugfixes, and improvements.
;; 2006-12-29 - XEmacs support, from Aidan Kehoe <kehoea@parhasard.net>.
;; 2008-02-21 - Installed in GNU Emacs.
+;; 2011-10-17 - Patch `json-alist-p' and `json-plist-p' to avoid recursion -tzz
;;; Code:
(defvar json-object-type 'alist
"Type to convert JSON objects to.
-Must be one of `alist', `plist', or `hash-table'. Consider let-binding
+Must be one of `alist', `plist', or `hash-table'. Consider let-binding
this around your call to `json-read' instead of `setq'ing it.")
(defvar json-array-type 'vector
"Type to convert JSON arrays to.
-Must be one of `vector' or `list'. Consider let-binding this around
+Must be one of `vector' or `list'. Consider let-binding this around
your call to `json-read' instead of `setq'ing it.")
(defvar json-key-type nil
`plist' `keyword'
Note that values other than `string' might behave strangely for
-Sufficiently Weird keys. Consider let-binding this around your call to
+Sufficiently Weird keys. Consider let-binding this around your call to
`json-read' instead of `setq'ing it.")
(defvar json-false :json-false
"Value to use when reading JSON `false'.
If this has the same value as `json-null', you might not be able to tell
-the difference between `false' and `null'. Consider let-binding this
+the difference between `false' and `null'. Consider let-binding this
around your call to `json-read' instead of `setq'ing it.")
(defvar json-null nil
"Value to use when reading JSON `null'.
If this has the same value as `json-false', you might not be able to
-tell the difference between `false' and `null'. Consider let-binding
+tell the difference between `false' and `null'. Consider let-binding
this around your call to `json-read' instead of `setq'ing it.")
\f
(mapconcat 'identity strings separator))
(defun json-alist-p (list)
- "Non-null iff LIST is an alist."
- (or (null list)
- (and (consp (car list))
- (json-alist-p (cdr list)))))
+ "Non-null if and only if LIST is an alist."
+ (while (consp list)
+ (setq list (if (consp (car list))
+ (cdr list)
+ 'not-alist)))
+ (null list))
(defun json-plist-p (list)
- "Non-null iff LIST is a plist."
- (or (null list)
- (and (keywordp (car list))
- (consp (cdr list))
- (json-plist-p (cddr list)))))
+ "Non-null if and only if LIST is a plist."
+ (while (consp list)
+ (setq list (if (and (keywordp (car list))
+ (consp (cdr list)))
+ (cddr list)
+ 'not-plist)))
+ (null list))
;; Reader utilities
(defsubst json-advance (&optional n)
"Skip past the following N characters."
- (unless n (setq n 1))
- (let ((goal (+ (point) n)))
- (goto-char goal)
- (when (< (point) goal)
- (signal 'end-of-file nil))))
+ (forward-char n))
(defsubst json-peek ()
"Return the character at point."
(defun json-skip-whitespace ()
"Skip past the whitespace at point."
- (while (looking-at "[\t\r\n\f\b ]")
- (goto-char (match-end 0))))
+ (skip-chars-forward "\t\r\n\f\b "))
\f
(put 'json-number-format 'error-conditions
'(json-number-format json-error error))
-(put 'json-string-escape 'error-message "Bad unicode escape")
+(put 'json-string-escape 'error-message "Bad Unicode escape")
(put 'json-string-escape 'error-conditions
'(json-string-escape json-error error))
(put 'json-string-format 'error-conditions
'(json-string-format json-error error))
+(put 'json-key-format 'error-message "Bad JSON object key")
+(put 'json-key-format 'error-conditions
+ '(json-key-format json-error error))
+
(put 'json-object-format 'error-message "Bad JSON object")
(put 'json-object-format 'error-conditions
'(json-object-format json-error error))
;; Number parsing
-(defun json-read-number ()
- "Read the JSON number following point.
+(defun json-read-number (&optional sign)
+ "Read the JSON number following point.
+The optional SIGN argument is for internal use.
+
N.B.: Only numbers which can fit in Emacs Lisp's native number
representation will be parsed correctly."
- (if (char-equal (json-peek) ?-)
- (progn
- (json-advance)
- (- 0 (json-read-number)))
- (if (looking-at "[0-9]+\\([.][0-9]+\\)?\\([eE][+-]?[0-9]+\\)?")
- (progn
+ ;; If SIGN is non-nil, the number is explicitly signed.
+ (let ((number-regexp
+ "\\([0-9]+\\)?\\(\\.[0-9]+\\)?\\([Ee][+-]?[0-9]+\\)?"))
+ (cond ((and (null sign) (char-equal (json-peek) ?-))
+ (json-advance)
+ (- (json-read-number t)))
+ ((and (null sign) (char-equal (json-peek) ?+))
+ (json-advance)
+ (json-read-number t))
+ ((and (looking-at number-regexp)
+ (or (match-beginning 1)
+ (match-beginning 2)))
(goto-char (match-end 0))
(string-to-number (match-string 0)))
- (signal 'json-number-format (list (point))))))
+ (t (signal 'json-number-format (list (point)))))))
;; Number encoding
(setq char (json-encode-char0 char 'ucs))
(let ((control-char (car (rassoc char json-special-chars))))
(cond
- ;; Special JSON character (\n, \r, etc.)
+ ;; Special JSON character (\n, \r, etc.).
(control-char
(format "\\%c" control-char))
- ;; ASCIIish printable character
- ((and (> char 31) (< char 161))
+ ;; ASCIIish printable character.
+ ((and (> char 31) (< char 127))
(format "%c" char))
- ;; Fallback: UCS code point in \uNNNN form
+ ;; Fallback: UCS code point in \uNNNN form.
(t
(format "\\u%04x" char)))))
"Return a JSON representation of STRING."
(format "\"%s\"" (mapconcat 'json-encode-char string "")))
+(defun json-encode-key (object)
+ "Return a JSON representation of OBJECT.
+If the resulting JSON object isn't a valid JSON object key,
+this signals `json-key-format'."
+ (let ((encoded (json-encode object)))
+ (unless (stringp (json-read-from-string encoded))
+ (signal 'json-key-format (list object)))
+ encoded))
+
;;; JSON Objects
(defun json-new-object ()
(maphash
(lambda (k v)
(push (format "%s:%s"
- (json-encode k)
+ (json-encode-key k)
(json-encode v))
r))
hash-table)
(format "{%s}"
(json-join (mapcar (lambda (cons)
(format "%s:%s"
- (json-encode (car cons))
+ (json-encode-key (car cons))
(json-encode (cdr cons))))
alist)
", ")))
"Return a JSON representation of PLIST."
(let (result)
(while plist
- (push (concat (json-encode (car plist))
+ (push (concat (json-encode-key (car plist))
":"
(json-encode (cadr plist)))
result)
(?\" json-read-string))))
(mapc (lambda (char)
(push (list char 'json-read-number) table))
- '(?- ?0 ?1 ?2 ?3 ?4 ?5 ?6 ?7 ?8 ?9))
+ '(?- ?+ ?. ?0 ?1 ?2 ?3 ?4 ?5 ?6 ?7 ?8 ?9))
table)
"Readtable for JSON reader.")
(provide 'json)
-;; arch-tag: 15f6e4c8-b831-4172-8749-bbc680c50ea1
;;; json.el ends here