Function: json--print-string

json--print-string is a byte-compiled function defined in json.el.gz.

Signature

(json--print-string STRING &optional FROM)

Documentation

Insert a JSON representation of STRING at point.

FROM is the index of STRING to start from and defaults to 0.

Source Code

;; Defined in /usr/src/emacs/lisp/json.el.gz
;; String encoding

(defun json--print-string (string &optional from)
  "Insert a JSON representation of STRING at point.
FROM is the index of STRING to start from and defaults to 0."
  (insert ?\")
  (goto-char (prog1 (point) (princ string)))
  (and from (delete-char from))
  ;; Escape only quotation mark, backslash, and the control
  ;; characters U+0000 to U+001F (RFC 4627, ECMA-404).
  (while (re-search-forward (rx (in ?\" ?\\ cntrl)) nil 'move)
    (let ((char (preceding-char)))
      (delete-char -1)
      (insert ?\\ (or
                   ;; Special JSON character (\n, \r, etc.).
                   (car (rassq char json-special-chars))
                   ;; Fallback: UCS code point in \uNNNN form.
                   (format "u%04x" char)))))
  (insert ?\")
  string)