Skip to content

Commit

Permalink
Merge pull request #13 from gongo/add_option_pretty_string
Browse files Browse the repository at this point in the history
Add flag `json-reformat:pretty-string?`
  • Loading branch information
gongo committed Dec 11, 2014
2 parents a99387e + 7e61723 commit b8dac16
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 32 deletions.
46 changes: 22 additions & 24 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,40 +40,38 @@ M-x json-reformat-region
json-reformat:indent-width (integer)
Change indentation level (default 4)
```
## IMPORTANT
json-reformat:pretty-string? (boolean)
From emacs 24.4, `json-pretty-print` and `json-pretty-print-buffer` was bundled.
Specify whether to decode the string (default nil)
Example:
Example:
```json
{"name":"foobar","nick":"foo \u00e4 bar","description":"<pre>\nbaz\n</pre>"}
```
;; {"name":"foo\"bar","nick":"foo \u00e4 bar","description":"<pre>\nbaz\n</pre>"}
Result of `json-pretty-print`:
If nil:
```json
{
"description": "<pre>\nbaz\n<\/pre>",
"nick": "foo \u00e4 bar",
"name": "foobar"
}
```
{
"name": "foo\"bar",
"nick": "foo \u00e4 bar",
"description": "<pre>\nbaz\n<\/pre>"
}
Result of `json-reformat-region`:
Else t:
```json
{
"description": "<pre>
baz
</pre>",
"nick": "foo ä bar",
"name": "foobar"
}
{
"name": "foo\"bar",
"nick": "foo ä bar",
"description": "<pre>
baz
</pre>"
}
```

## SEE ALSO

From emacs 24.4, `json-pretty-print` and `json-pretty-print-buffer` (similar specifications as `json-reformat-region`) was bundled.

## LICENSE

MIT License. see `json-reformat.el`
Expand Down
39 changes: 34 additions & 5 deletions json-reformat.el
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,33 @@
:type 'integer
:group 'json-reformat)

(defcustom json-reformat:pretty-string? nil
"Whether to decode the string.
Example:
{\"name\":\"foobar\",\"nick\":\"foo \\u00e4 bar\",\"description\":\"<pre>\\nbaz\\n</pre>\"}
If nil:
{
\"name\": \"foobar\",
\"nick\": \"foo \\u00e4 bar\",
\"description\": \"<pre>\\nbaz\\n<\\/pre>\"
}
Else t:
{
\"name\": \"foobar\",
\"nick\": \"foo ä bar\",
\"description\": \"<pre>
baz
</pre>\"
}"
:type 'boolean
:group 'json-reformat)

(defun json-reformat:indent (level)
(make-string (* level json-reformat:indent-width) ? ))

Expand All @@ -56,8 +83,10 @@
((equal json-false val) "false")
(t (symbol-name val))))

(defun json-reformat:decode-string (val)
(format "\"%s\"" val))
(defun json-reformat:string-to-string (val)
(if json-reformat:pretty-string?
(format "\"%s\"" (replace-regexp-in-string "\"" "\\\\\"" val))
(json-encode-string val)))

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

(defun json-reformat:print-node (val level)
(cond ((consp val) (json-reformat:tree-to-string (json-reformat:reverse-plist val) level))
(cond ((consp val) (json-reformat:tree-to-string (json-reformat:reverse-plist val) level))
((numberp val) (json-reformat:number-to-string val))
((vectorp val) (json-reformat:vector-to-string val level))
((null val) "null")
((null val) "null")
((symbolp val) (json-reformat:symbol-to-string val))
(t (json-reformat:decode-string val))))
(t (json-reformat:string-to-string val))))

(defun json-reformat:tree-to-string (root level)
(concat "{\n"
Expand Down
15 changes: 12 additions & 3 deletions test/json-reformat-test.el
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,20 @@
\]" (json-reformat:vector-to-string [1 [2 [3 4] 5] 6 []] 0)))
)

(ert-deftest json-reformat-test:decode-string ()
(should (string= "\"foobar\"" (json-reformat:decode-string "foobar")))
(should (string= "\"\"" (json-reformat:decode-string "\u2661")))
(ert-deftest json-reformat-test:string-to-string ()
(should (string= "\"foobar\"" (json-reformat:string-to-string "foobar")))
(should (string= "\"fo\\\"o\\nbar\"" (json-reformat:string-to-string "fo\"o\nbar")))
(should (string= "\"\\u2661\"" (json-reformat:string-to-string "\u2661")))
)

(ert-deftest json-reformat-test:string-to-string-when-pretty ()
(let ((json-reformat:pretty-string? t))
(should (string= "\"foobar\"" (json-reformat:string-to-string "foobar")))
(should (string= "\"fo\\\"o
bar\"" (json-reformat:string-to-string "fo\"o\nbar")))
(should (string= "\"\"" (json-reformat:string-to-string "\u2661")))
))

(ert-deftest json-reformat-test:print-node ()
(should (string= "null" (json-reformat:print-node nil 0)))
)
Expand Down

0 comments on commit b8dac16

Please sign in to comment.