-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathjson-pretty-print.el
56 lines (45 loc) · 1.78 KB
/
json-pretty-print.el
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
;;; json-pretty-print.el --- JSON pretty-printing functions
;; Copyright (C) 2012 Ryan Crum
;; Author: Ryan Crum
;; Git: git://github.com/thorstadt/json-pretty-print.git
;; Version: 1.0
;; Created: 2011-05-17
;; Keywords: convenience
;; This file is NOT part of GNU Emacs.
;;; License:
;; This program 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.
;;
;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; 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.
;;; Commentary:
;; This library depends on the version of json.el with pretty-printing.
;; It can be found at http://github.com/thorstadt/json.el
(require 'json)
(defun json-pretty-print-buffer ()
(interactive)
(let ((json-encoding-pretty-print t))
(let ((json-string (json-encode (json-read-from-string (buffer-string))))
(buf (current-buffer)))
(with-current-buffer buf
(erase-buffer)
(insert json-string)))))
(defun json-pretty-print ()
(interactive)
(unless mark-active
(error "No region selected."))
(let ((begin (region-beginning))
(end (region-end)))
(kill-region begin end)
(let ((json-encoding-pretty-print t))
(insert (json-encode (json-read-from-string (current-kill 0)))))))
(provide 'json-pretty-print)