27c28a94af2eab2e307b86f73450e766bbb2bfff

Author
Robby Russell <robby@planetargon.com>
Committer
Robby Russell <robby@planetargon.com>
Date

Message

Merge pull request #2647 from bobwilliams/master

Adding a README.md for plugins/jsontools

Diff

 1diff --git a/plugins/jsontools/README.md b/plugins/jsontools/README.md
 2new file mode 100644
 3index 0000000000000000000000000000000000000000..4faf58b98ead4edaabf24c77d5ebefe61c0a88ed
 4--- /dev/null
 5+++ b/plugins/jsontools/README.md
 6@@ -0,0 +1,42 @@
 7+# jsontools
 8+
 9+Handy command line tools for dealing with json data.
10+
11+## Tools
12+
13+- **pp_json** - pretty prints json
14+- **is_json** - returns true if valid json; false otherwise
15+- **urlencode_json** - returns a url encoded string for the given json 
16+- **urldecode_json** - returns decoded json for the given url encoded string
17+
18+## Usage
19+Usage is simple...just take your json data and pipe it into the appropriate jsontool.
20+```sh
21+<json data> | <jsontools tool>
22+```
23+## Examples
24+
25+##### pp_json
26+
27+```sh
28+# curl json data and pretty print the results
29+curl https://coderwall.com/bobwilliams.json | pp_json
30+```
31+
32+##### is_json
33+```sh
34+# pretty print the contents of an existing json file
35+less data.json | is_json
36+```
37+
38+##### urlencode_json
39+```sh
40+# json data directly from the command line
41+echo '{"b":2, "a":1}' | urlencode_json
42+```
43+
44+##### urldecode_json
45+```sh
46+# url encoded string to decode
47+echo '%7B%22b%22:2,%20%22a%22:1%7D%0A' | urldecode_json
48+```
49diff --git a/plugins/jsontools/jsontools.plugin.zsh b/plugins/jsontools/jsontools.plugin.zsh
50index 7b6f8c585d8737a8314f8129e2aa5945fd2ba3d2..20d5eb1c9928e05b61631072910a7fd106235906 100644
51--- a/plugins/jsontools/jsontools.plugin.zsh
52+++ b/plugins/jsontools/jsontools.plugin.zsh
53@@ -2,15 +2,38 @@
54 # Adds command line aliases useful for dealing with JSON
55 
56 if [[ $(whence $JSONTOOLS_METHOD) = "" ]]; then
57-    JSONTOOLS_METHOD=""
58+	JSONTOOLS_METHOD=""
59 fi
60 
61 if [[ $(whence node) != "" && ( "x$JSONTOOLS_METHOD" = "x"  || "x$JSONTOOLS_METHOD" = "xnode" ) ]]; then
62-    alias pp_json='node -e "console.log(JSON.stringify(process.argv[1]), null, 4)"'
63+	alias pp_json='xargs -0 node -e "console.log(JSON.stringify(JSON.parse(process.argv[1]), null, 4));"'
64+	alias is_json='xargs -0 node -e "try {json = JSON.parse(process.argv[1]);} catch (e) { console.log(false); json = null; } if(json) { console.log(true); }"'
65+	alias urlencode_json='xargs -0 node -e "console.log(encodeURIComponent(process.argv[1]))"'
66+	alias urldecode_json='xargs -0 node -e "console.log(decodeURIComponent(process.argv[1]))"'
67 elif [[ $(whence python) != "" && ( "x$JSONTOOLS_METHOD" = "x" || "x$JSONTOOLS_METHOD" = "xpython" ) ]]; then
68 	alias pp_json='python -mjson.tool'
69+	alias is_json='python -c "
70+import json, sys;
71+try: 
72+	json.loads(sys.stdin.read())
73+except ValueError, e: 
74+	print False
75+else:
76+	print True
77+sys.exit(0)"'
78+	alias urlencode_json='python -c "
79+import urllib, json, sys;
80+print urllib.quote_plus(sys.stdin.read())
81+sys.exit(0)"'
82+	alias urldecode_json='python -c "
83+import urllib, json, sys;
84+print urllib.unquote_plus(sys.stdin.read())
85+sys.exit(0)"'
86 elif [[ $(whence ruby) != "" && ( "x$JSONTOOLS_METHOD" = "x" || "x$JSONTOOLS_METHOD" = "xruby" ) ]]; then
87 	alias pp_json='ruby -e "require \"json\"; require \"yaml\"; puts JSON.parse(STDIN.read).to_yaml"'
88+	alias is_json='ruby -e "require \"json\"; begin; JSON.parse(STDIN.read); puts true; rescue Exception => e; puts false; end"'
89+	alias urlencode_json='ruby -e "require \"uri\"; puts URI.escape(STDIN.read)"'
90+	alias urldecode_json='ruby -e "require \"uri\"; puts URI.unescape(STDIN.read)"'
91 fi
92 
93 unset JSONTOOLS_METHOD