jsontools.plugin.zsh 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. # JSON Tools
  2. # Adds command line aliases useful for dealing with JSON
  3. if [[ $(whence $JSONTOOLS_METHOD) = "" ]]; then
  4. JSONTOOLS_METHOD=""
  5. fi
  6. if [[ $(whence node) != "" && ( "x$JSONTOOLS_METHOD" = "x" || "x$JSONTOOLS_METHOD" = "xnode" ) ]]; then
  7. alias pp_json='xargs -0 node -e "console.log(JSON.stringify(JSON.parse(process.argv[1]), null, 4));"'
  8. 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); }"'
  9. alias urlencode_json='xargs -0 node -e "console.log(encodeURIComponent(process.argv[1]))"'
  10. alias urldecode_json='xargs -0 node -e "console.log(decodeURIComponent(process.argv[1]))"'
  11. elif [[ $(whence python) != "" && ( "x$JSONTOOLS_METHOD" = "x" || "x$JSONTOOLS_METHOD" = "xpython" ) ]]; then
  12. alias pp_json='python -mjson.tool'
  13. alias is_json='python -c "
  14. import json, sys;
  15. try:
  16. json.loads(sys.stdin.read())
  17. except ValueError, e:
  18. print False
  19. else:
  20. print True
  21. sys.exit(0)"'
  22. alias urlencode_json='python -c "
  23. import urllib, json, sys;
  24. print urllib.quote_plus(sys.stdin.read())
  25. sys.exit(0)"'
  26. alias urldecode_json='python -c "
  27. import urllib, json, sys;
  28. print urllib.unquote_plus(sys.stdin.read())
  29. sys.exit(0)"'
  30. elif [[ $(whence ruby) != "" && ( "x$JSONTOOLS_METHOD" = "x" || "x$JSONTOOLS_METHOD" = "xruby" ) ]]; then
  31. alias pp_json='ruby -e "require \"json\"; require \"yaml\"; puts JSON.parse(STDIN.read).to_yaml"'
  32. alias is_json='ruby -e "require \"json\"; begin; JSON.parse(STDIN.read); puts true; rescue Exception => e; puts false; end"'
  33. alias urlencode_json='ruby -e "require \"uri\"; puts URI.escape(STDIN.read)"'
  34. alias urldecode_json='ruby -e "require \"uri\"; puts URI.unescape(STDIN.read)"'
  35. fi
  36. unset JSONTOOLS_METHOD