Skip to content

Instantly share code, notes, and snippets.

@gongo
Last active June 11, 2020 00:48
Show Gist options
  • Star 8 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save gongo/1789605 to your computer and use it in GitHub Desktop.
Save gongo/1789605 to your computer and use it in GitHub Desktop.
見づらい JSON 文字列を雰囲気見やすくする elisp です http://gongo.hatenablog.com/entry/2012/02/10/222051
;;; json-reformat --- Reformat tool for JSON
;; Author: Wataru MIYAGUNI <gonngo@gmail.com>
;; Keywords: json
;; Copyright (c) 2012 Wataru MIYAGUNI
;;
;; MIT License
;;
;; Permission is hereby granted, free of charge, to any person obtaining
;; a copy of this software and associated documentation files (the
;; "Software"), to deal in the Software without restriction, including
;; without limitation the rights to use, copy, modify, merge, publish,
;; distribute, sublicense, and/or sell copies of the Software, and to
;; permit persons to whom the Software is furnished to do so, subject to
;; the following conditions:
;;
;; The above copyright notice and this permission notice shall be
;; included in all copies or substantial portions of the Software.
;;
;; THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
;; EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
;; MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
;; NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
;; LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
;; OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
;; WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
;;; Commentary:
;;; Code:
(require 'json)
(defun json-reformat:indent (level)
(make-string (* level 4) ? ))
(defun json-reformat:p-of-number (val)
(number-to-string val))
(defun json-reformat:p-of-list (val level)
(concat "{\n" (json:list-to-string val (1+ level)) (json-reformat:indent level) "}"))
(defun json-reformat:p-of-vector (val level)
(if (= (length val) 0) "[]"
(concat "[\n"
(mapconcat
'identity
(loop for v across val
collect (concat
(json-reformat:indent (1+ level))
(json-reformat:print-value v (1+ level))
))
(concat ",\n"))
"\n" (json-reformat:indent level) "]"
)))
(defun json-reformat:p-of-symbol (val)
(cond ((equal 't val) "true")
((equal json-false val) "false")
(t (symbol-name val))))
(defun json-reformat:print-value (val level)
(cond ((consp val) (json-reformat:p-of-list val level))
((numberp val) (json-reformat:p-of-number val))
((vectorp val) (json-reformat:p-of-vector val level))
((null val) "null")
((symbolp val) (json-reformat:p-of-symbol val))
(t (json-encode-string val))))
(defun json:list-to-string (root level)
(let (key val str)
(while root
(setq key (car root)
val (cadr root)
root (cddr root))
(setq str
(concat str (json-reformat:indent level)
"\"" key "\""
": "
(json-reformat:print-value val level)
(when root ",")
"\n"
)))
str))
(defun json-reformat-region (begin end)
(message "gist.github.com/gongo/1789605 is moved to github.com/gongo/json-reformat.
This repository is not maintained.")
(interactive "r")
(save-excursion
(save-restriction
(narrow-to-region begin end)
(goto-char (point-min))
(let* ((json-key-type 'string)
(json-object-type 'plist)
(before (buffer-substring (point-min) (point-max)))
(json-tree (json-read-from-string before))
after)
(setq after (json-reformat:p-of-list json-tree 0))
(delete-region (point-min) (point-max))
(insert after)))))
(provide 'json-reformat)
@clarete
Copy link

clarete commented Sep 18, 2012

Hey, very nice work. Thank you for that.

I'd like to use/(maybe) modify/redistribute this script, but I don't think it's a good idea doing this without choosing a license before. I'd love to see this script licensed under GPL or, if you prefer, MIT. Is that possible? Thanks again!

@gongo
Copy link
Author

gongo commented Sep 27, 2012

@clarete thanks comment! I have appended "MIT License".

@gongo
Copy link
Author

gongo commented Apr 24, 2013

@stianalmaas
Copy link

Thanks for this, gongo. It works very well.
Still, I have a problem with utf-8 text. It is encoded as unicode numbers, like /uxxxx. Do you know why? I'm new to Emacs lisp.

@gongo
Copy link
Author

gongo commented Dec 20, 2013

Hi @stianalmaas . Sorry for my late reply.

Still, I have a problem with utf-8 text. It is encoded as unicode numbers, like /uxxxx

Is my understanding of code below correct?

# before

{"id":3,"name": "\u2661"}

# after

{
    "name": "\u2661", # <= Not decoded!
    "id": 3
}

It's in the specifications.

@okumuralab
Copy link

To prevent Unicode strings from being formatted as \uxxxx, change
(t (json-encode-string val))))
to
(t (format ""%s"" val))))

@gongo
Copy link
Author

gongo commented Feb 25, 2014

I see 😄

@gongo
Copy link
Author

gongo commented Mar 1, 2014

I moved to https://github.com/gongo/json-reformat .
I will continue to develop in this repository.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment