Thanks Driven Life

日々是感謝

Emacs 24.4 から標準搭載された "json-pretty-print" について

経緯

Why would you use? · Issue #11 · gongo/json-reformat

わ、わい?

I see that emacs comes with json-pretty-print and json-pretty-print-buffer.

まじかよって思って調べたら確かにあった…

json-pretty-print is an interactive compiled Lisp function in
`json.el'.

(json-pretty-print BEGIN END)

Pretty-print selected region.

調べてみた

自分が json-reformat.el を作った時には確かに無かったので、Emacs の 24.3 と 24.4 を diff ってみたところ

$ diff -u emacs-24.3/lisp/json.el emacs-24.4/lisp/json.el

たしかに。24.4 から搭載されている模様。

--- emacs-24.3/lisp/json.el 2013-01-02 05:37:17.000000000 +0900
+++ emacs-24.4/lisp/json.el 2014-03-21 14:34:40.000000000 +0900
@@ -1,9 +1,9 @@
 ;;; json.el --- JavaScript Object Notation parser / generator

-;; Copyright (C) 2006-2013 Free Software Foundation, Inc.
+;; Copyright (C) 2006-2014 Free Software Foundation, Inc.

 ;; Author: Edward O'Connor <ted@oconnor.cx>
-;; Version: 1.3
+;; Version: 1.4
 ;; Keywords: convenience

 ;; This file is part of GNU Emacs.
@@ -48,10 +48,10 @@
 ;; 2006-12-29 - XEmacs support, from Aidan Kehoe <kehoea@parhasard.net>.
 ;; 2008-02-21 - Installed in GNU Emacs.
 ;; 2011-10-17 - Patch `json-alist-p' and `json-plist-p' to avoid recursion -tzz
+;; 2012-10-25 - Added pretty-printed reformatting -Ryan Crum (ryan@ryancrum.org)

 ;;; Code:

-(eval-when-compile (require 'cl))

(snip)

json-reformat.el と json-pretty-print の違い

たとえば

{"name":"foobar","nick":"foo \u00e4 bar","description":"<pre>\nbaz\n</pre>"}

こいつを Reformat することにします。まずは json-reformat.el の json-reformat-region

{
  "description": "<pre>
baz
</pre>",
  "nick": "foo ä bar",
  "name": "foobar"
}

続いて 24.4 から登場の json-pretty-print

{
  "description": "<pre>\nbaz\n<\/pre>",
  "nick": "foo \u00e4 bar",
  "name": "foobar"
}

こんな感じです。 json-reformat.el の挙動を json-pretty-print に合わせる予定は無いので Emacsjson reformatting をしたい方はどちらか好きな方をお選び下さい。

[追記] 現状、どっちも対応する感じになってます。

https://github.com/gongo/json-reformat#configuration

json-reformat:pretty-string?t or nil