Function: json-pretty-print
json-pretty-print is an interactive and byte-compiled function defined
in json.el.gz.
Signature
(json-pretty-print BEGIN END &optional MINIMIZE)
Documentation
Pretty-print selected region.
With prefix argument MINIMIZE, minimize it instead.
Probably introduced at or before Emacs version 25.1.
Key Bindings
Source Code
;; Defined in /usr/src/emacs/lisp/json.el.gz
(defun json-pretty-print (begin end &optional minimize)
"Pretty-print selected region.
With prefix argument MINIMIZE, minimize it instead."
(interactive "r\nP")
(let ((json-encoding-pretty-print (null minimize))
;; Distinguish an empty object from 'null'.
(json-null :json-null)
;; Ensure that ordering is maintained.
(json-object-type 'alist)
;; Ensure that keys survive roundtrip (bug#24252, bug#42545).
(json-key-type 'string)
(orig-buf (current-buffer)))
;; Strategy: Repeatedly `json-read' from the original buffer and
;; write the pretty-printed snippet to a temporary buffer.
;; Use `replace-region-contents' to swap the original
;; region with the contents of the temporary buffer so that point,
;; marks, etc. are kept.
;; Stop as soon as we get an error from `json-read'.
(with-temp-buffer
(let ((tmp-buf (current-buffer)))
;; This apparently affords decent performance gains in `json--print'.
(setq-local inhibit-modification-hooks t)
(set-buffer orig-buf)
(save-excursion
(save-restriction
(narrow-to-region begin end)
(goto-char begin)
(while
(progn
(skip-chars-forward " \t\n")
(condition-case nil
(let ((beg (point))
(json (json-read))
(standard-output tmp-buf))
(with-current-buffer tmp-buf
(erase-buffer) (json--print json))
(replace-region-contents
beg (point) tmp-buf
json-pretty-print-max-secs
;; FIXME: What's a good value here? Can we use
;; something better, e.g., by deriving a value
;; from the size of the region?
64)
'keep-going)
;; EOF is expected because we json-read until we hit
;; the end of the narrow region.
(json-end-of-file nil))))))))))