0605d53fa7f04c864e7bc5f9f68bd38f24fbbfd5

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

Message

adding urlencode_json and associated README.md details

Diff

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