Skip to content

Commit b8dac16

Browse files
committed
Merge pull request #13 from gongo/add_option_pretty_string
Add flag `json-reformat:pretty-string?`
2 parents a99387e + 7e61723 commit b8dac16

File tree

3 files changed

+68
-32
lines changed

3 files changed

+68
-32
lines changed

README.md

Lines changed: 22 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -40,40 +40,38 @@ M-x json-reformat-region
4040
json-reformat:indent-width (integer)
4141
4242
Change indentation level (default 4)
43-
```
4443
45-
## IMPORTANT
44+
json-reformat:pretty-string? (boolean)
4645
47-
From emacs 24.4, `json-pretty-print` and `json-pretty-print-buffer` was bundled.
46+
Specify whether to decode the string (default nil)
4847
49-
Example:
48+
Example:
5049
51-
```json
52-
{"name":"foobar","nick":"foo \u00e4 bar","description":"<pre>\nbaz\n</pre>"}
53-
```
50+
;; {"name":"foo\"bar","nick":"foo \u00e4 bar","description":"<pre>\nbaz\n</pre>"}
5451
55-
Result of `json-pretty-print`:
52+
If nil:
5653
57-
```json
58-
{
59-
"description": "<pre>\nbaz\n<\/pre>",
60-
"nick": "foo \u00e4 bar",
61-
"name": "foobar"
62-
}
63-
```
54+
{
55+
"name": "foo\"bar",
56+
"nick": "foo \u00e4 bar",
57+
"description": "<pre>\nbaz\n<\/pre>"
58+
}
6459
65-
Result of `json-reformat-region`:
60+
Else t:
6661
67-
```json
68-
{
69-
"description": "<pre>
70-
baz
71-
</pre>",
72-
"nick": "foo ä bar",
73-
"name": "foobar"
74-
}
62+
{
63+
"name": "foo\"bar",
64+
"nick": "foo ä bar",
65+
"description": "<pre>
66+
baz
67+
</pre>"
68+
}
7569
```
7670

71+
## SEE ALSO
72+
73+
From emacs 24.4, `json-pretty-print` and `json-pretty-print-buffer` (similar specifications as `json-reformat-region`) was bundled.
74+
7775
## LICENSE
7876

7977
MIT License. see `json-reformat.el`

json-reformat.el

Lines changed: 34 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,33 @@
4545
:type 'integer
4646
:group 'json-reformat)
4747

48+
(defcustom json-reformat:pretty-string? nil
49+
"Whether to decode the string.
50+
51+
Example:
52+
53+
{\"name\":\"foobar\",\"nick\":\"foo \\u00e4 bar\",\"description\":\"<pre>\\nbaz\\n</pre>\"}
54+
55+
If nil:
56+
57+
{
58+
\"name\": \"foobar\",
59+
\"nick\": \"foo \\u00e4 bar\",
60+
\"description\": \"<pre>\\nbaz\\n<\\/pre>\"
61+
}
62+
63+
Else t:
64+
65+
{
66+
\"name\": \"foobar\",
67+
\"nick\": \"foo ä bar\",
68+
\"description\": \"<pre>
69+
baz
70+
</pre>\"
71+
}"
72+
:type 'boolean
73+
:group 'json-reformat)
74+
4875
(defun json-reformat:indent (level)
4976
(make-string (* level json-reformat:indent-width) ? ))
5077

@@ -56,8 +83,10 @@
5683
((equal json-false val) "false")
5784
(t (symbol-name val))))
5885

59-
(defun json-reformat:decode-string (val)
60-
(format "\"%s\"" val))
86+
(defun json-reformat:string-to-string (val)
87+
(if json-reformat:pretty-string?
88+
(format "\"%s\"" (replace-regexp-in-string "\"" "\\\\\"" val))
89+
(json-encode-string val)))
6190

6291
(defun json-reformat:vector-to-string (val level)
6392
(if (= (length val) 0) "[]"
@@ -84,12 +113,12 @@
84113
rval))
85114

86115
(defun json-reformat:print-node (val level)
87-
(cond ((consp val) (json-reformat:tree-to-string (json-reformat:reverse-plist val) level))
116+
(cond ((consp val) (json-reformat:tree-to-string (json-reformat:reverse-plist val) level))
88117
((numberp val) (json-reformat:number-to-string val))
89118
((vectorp val) (json-reformat:vector-to-string val level))
90-
((null val) "null")
119+
((null val) "null")
91120
((symbolp val) (json-reformat:symbol-to-string val))
92-
(t (json-reformat:decode-string val))))
121+
(t (json-reformat:string-to-string val))))
93122

94123
(defun json-reformat:tree-to-string (root level)
95124
(concat "{\n"

test/json-reformat-test.el

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,11 +55,20 @@
5555
\]" (json-reformat:vector-to-string [1 [2 [3 4] 5] 6 []] 0)))
5656
)
5757

58-
(ert-deftest json-reformat-test:decode-string ()
59-
(should (string= "\"foobar\"" (json-reformat:decode-string "foobar")))
60-
(should (string= "\"\"" (json-reformat:decode-string "\u2661")))
58+
(ert-deftest json-reformat-test:string-to-string ()
59+
(should (string= "\"foobar\"" (json-reformat:string-to-string "foobar")))
60+
(should (string= "\"fo\\\"o\\nbar\"" (json-reformat:string-to-string "fo\"o\nbar")))
61+
(should (string= "\"\\u2661\"" (json-reformat:string-to-string "\u2661")))
6162
)
6263

64+
(ert-deftest json-reformat-test:string-to-string-when-pretty ()
65+
(let ((json-reformat:pretty-string? t))
66+
(should (string= "\"foobar\"" (json-reformat:string-to-string "foobar")))
67+
(should (string= "\"fo\\\"o
68+
bar\"" (json-reformat:string-to-string "fo\"o\nbar")))
69+
(should (string= "\"\"" (json-reformat:string-to-string "\u2661")))
70+
))
71+
6372
(ert-deftest json-reformat-test:print-node ()
6473
(should (string= "null" (json-reformat:print-node nil 0)))
6574
)

0 commit comments

Comments
 (0)