X-Git-Url: https://code.delx.au/gnu-emacs/blobdiff_plain/e427b55370b28f55e285ce0ee4328246eb7522ea..2958e5237c6ee57378f1b47217aafd0b21ec86ab:/lisp/json.el diff --git a/lisp/json.el b/lisp/json.el index 98974e67b7..eaf8596a6d 100644 --- a/lisp/json.el +++ b/lisp/json.el @@ -52,13 +52,6 @@ ;;; Code: - -;; Compatibility code - -(defalias 'json-encode-char0 'encode-char) -(defalias 'json-decode-char0 'decode-char) - - ;; Parameters (defvar json-object-type 'alist @@ -265,7 +258,6 @@ representation will be parsed correctly." (defvar json-special-chars '((?\" . ?\") (?\\ . ?\\) - (?/ . ?/) (?b . ?\b) (?f . ?\f) (?n . ?\n) @@ -287,7 +279,7 @@ representation will be parsed correctly." ((looking-at "[0-9A-Fa-f][0-9A-Fa-f][0-9A-Fa-f][0-9A-Fa-f]") (let ((hex (match-string 0))) (json-advance 4) - (json-decode-char0 'ucs (string-to-number hex 16)))) + (string-to-number hex 16))) (t (signal 'json-string-escape (list (point))))))) @@ -313,24 +305,29 @@ representation will be parsed correctly." ;; String encoding -(defun json-encode-char (char) - "Encode CHAR as a JSON string." - (setq char (json-encode-char0 char 'ucs)) - (let ((control-char (car (rassoc char json-special-chars)))) - (cond - ;; Special JSON character (\n, \r, etc.). - (control-char - (format "\\%c" control-char)) - ;; ASCIIish printable character. - ((and (> char 31) (< char 127)) - (format "%c" char)) - ;; Fallback: UCS code point in \uNNNN form. - (t - (format "\\u%04x" char))))) - (defun json-encode-string (string) "Return a JSON representation of STRING." - (format "\"%s\"" (mapconcat 'json-encode-char string ""))) + ;; Reimplement the meat of `replace-regexp-in-string', for + ;; performance (bug#20154). + (let ((l (length string)) + (start 0) + res mb) + ;; Only escape quotation mark, backslash and the control + ;; characters U+0000 to U+001F (RFC 4627, ECMA-404). + (while (setq mb (string-match "[\"\\[:cntrl:]]" string start)) + (let* ((c (aref string mb)) + (special (rassq c json-special-chars))) + (push (substring string start mb) res) + (push (if special + ;; Special JSON character (\n, \r, etc.). + (string ?\\ (car special)) + ;; Fallback: UCS code point in \uNNNN form. + (format "\\u%04x" c)) + res) + (setq start (1+ mb)))) + (push (substring string start l) res) + (push "\"" res) + (apply #'concat "\"" (nreverse res)))) (defun json-encode-key (object) "Return a JSON representation of OBJECT.