400643ff5b20521aebe16ca36fde34bde212db89

Author
Bob Williams <bobwilliams.ii@gmail.com>
Committer
Bob Williams <bobwilliams.ii@gmail.com>
Date

Message

adding urldecode_json to compliment urlencode_json and updating readme.md; slight tweak to urlencode_json from previous commit

Diff

 1diff --git a/plugins/jsontools/README.md b/plugins/jsontools/README.md
 2index 975d2bb79f69d6d94e2b80108c1579cca7aca566..4faf58b98ead4edaabf24c77d5ebefe61c0a88ed 100644
 3--- a/plugins/jsontools/README.md
 4+++ b/plugins/jsontools/README.md
 5@@ -7,6 +7,7 @@ Handy command line tools for dealing with json data.
 6 - **pp_json** - pretty prints json
 7 - **is_json** - returns true if valid json; false otherwise
 8 - **urlencode_json** - returns a url encoded string for the given json 
 9+- **urldecode_json** - returns decoded json for the given url encoded string
10 
11 ## Usage
12 Usage is simple...just take your json data and pipe it into the appropriate jsontool.
13@@ -32,4 +33,10 @@ less data.json | is_json
14 ```sh
15 # json data directly from the command line
16 echo '{"b":2, "a":1}' | urlencode_json
17+```
18+
19+##### urldecode_json
20+```sh
21+# url encoded string to decode
22+echo '%7B%22b%22:2,%20%22a%22:1%7D%0A' | urldecode_json
23 ```
24diff --git a/plugins/jsontools/jsontools.plugin.zsh b/plugins/jsontools/jsontools.plugin.zsh
25index 8877ee62fc2bd50c5dfc5ac2342c39aba3789ed2..20d5eb1c9928e05b61631072910a7fd106235906 100644
26--- a/plugins/jsontools/jsontools.plugin.zsh
27+++ b/plugins/jsontools/jsontools.plugin.zsh
28@@ -8,7 +8,8 @@ fi
29 if [[ $(whence node) != "" && ( "x$JSONTOOLS_METHOD" = "x"  || "x$JSONTOOLS_METHOD" = "xnode" ) ]]; then
30 	alias pp_json='xargs -0 node -e "console.log(JSON.stringify(JSON.parse(process.argv[1]), null, 4));"'
31 	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); }"'
32-	alias urlencode_json='xargs -0 node -e "console.log(encodeURIComponent(JSON.stringify(process.argv[1])))"'
33+	alias urlencode_json='xargs -0 node -e "console.log(encodeURIComponent(process.argv[1]))"'
34+	alias urldecode_json='xargs -0 node -e "console.log(decodeURIComponent(process.argv[1]))"'
35 elif [[ $(whence python) != "" && ( "x$JSONTOOLS_METHOD" = "x" || "x$JSONTOOLS_METHOD" = "xpython" ) ]]; then
36 	alias pp_json='python -mjson.tool'
37 	alias is_json='python -c "
38@@ -22,12 +23,17 @@ else:
39 sys.exit(0)"'
40 	alias urlencode_json='python -c "
41 import urllib, json, sys;
42-print urllib.quote_plus(json.dumps(sys.stdin.read()))
43+print urllib.quote_plus(sys.stdin.read())
44+sys.exit(0)"'
45+	alias urldecode_json='python -c "
46+import urllib, json, sys;
47+print urllib.unquote_plus(sys.stdin.read())
48 sys.exit(0)"'
49 elif [[ $(whence ruby) != "" && ( "x$JSONTOOLS_METHOD" = "x" || "x$JSONTOOLS_METHOD" = "xruby" ) ]]; then
50 	alias pp_json='ruby -e "require \"json\"; require \"yaml\"; puts JSON.parse(STDIN.read).to_yaml"'
51 	alias is_json='ruby -e "require \"json\"; begin; JSON.parse(STDIN.read); puts true; rescue Exception => e; puts false; end"'
52 	alias urlencode_json='ruby -e "require \"uri\"; puts URI.escape(STDIN.read)"'
53+	alias urldecode_json='ruby -e "require \"uri\"; puts URI.unescape(STDIN.read)"'
54 fi
55 
56-unset JSONTOOLS_METHOD
57+unset JSONTOOLS_METHOD