2
0

jsontools.plugin.zsh 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. # JSON Tools
  2. # Adds command line aliases useful for dealing with JSON
  3. # Check that user-defined method is installed
  4. if [[ -n "$JSONTOOLS_METHOD" ]]; then
  5. (( $+commands[$JSONTOOLS_METHOD] )) || unset JSONTOOLS_METHOD
  6. fi
  7. # If method undefined, find the first one that is installed
  8. if [[ -z "$JSONTOOLS_METHOD" ]]; then
  9. for JSONTOOLS_METHOD in node python3 ruby; do
  10. # If method found, break out of loop
  11. (( $+commands[$JSONTOOLS_METHOD] )) && break
  12. # Otherwise unset the variable
  13. unset JSONTOOLS_METHOD
  14. done
  15. # If no methods were found, exit the plugin
  16. [[ -n "$JSONTOOLS_METHOD" ]] || return 1
  17. fi
  18. # Define json tools for each method
  19. case "$JSONTOOLS_METHOD" in
  20. node)
  21. # node doesn't make it easy to deal with stdin, so we pass it as an argument with xargs -0
  22. function pp_json() {
  23. xargs -0 node -e 'console.log(JSON.stringify(JSON.parse(process.argv[1]), null, 4));'
  24. }
  25. function is_json() {
  26. xargs -0 node -e '
  27. try {
  28. json = JSON.parse(process.argv[1]);
  29. console.log("true");
  30. process.exit(0);
  31. } catch (e) {
  32. console.log("false");
  33. process.exit(1);
  34. }
  35. '
  36. }
  37. function urlencode_json() {
  38. xargs -0 node -e "console.log(encodeURIComponent(process.argv[1]))"
  39. }
  40. function urldecode_json() {
  41. xargs -0 node -e "console.log(decodeURIComponent(process.argv[1]))"
  42. }
  43. ;;
  44. python3)
  45. function pp_json() {
  46. python3 -c 'import sys; del sys.path[0]; import runpy; runpy._run_module_as_main("json.tool")'
  47. }
  48. function is_json() {
  49. python3 -c '
  50. import sys; del sys.path[0];
  51. import json
  52. try:
  53. json.loads(sys.stdin.read())
  54. print("true"); sys.exit(0)
  55. except ValueError:
  56. print("false"); sys.exit(1)
  57. '
  58. }
  59. function urlencode_json() {
  60. python3 -c '
  61. import sys; del sys.path[0];
  62. from urllib.parse import quote_plus
  63. print(quote_plus(sys.stdin.read()))
  64. '
  65. }
  66. function urldecode_json() {
  67. python3 -c '
  68. import sys; del sys.path[0];
  69. from urllib.parse import unquote_plus
  70. print(unquote_plus(sys.stdin.read()))
  71. '
  72. }
  73. ;;
  74. ruby)
  75. function pp_json() {
  76. ruby -e '
  77. require "json"
  78. require "yaml"
  79. puts JSON.parse(STDIN.read).to_yaml
  80. '
  81. }
  82. function is_json() {
  83. ruby -e '
  84. require "json"
  85. begin
  86. puts !!JSON.parse(STDIN.read); exit(0)
  87. rescue JSON::ParserError
  88. puts false; exit(1)
  89. end
  90. '
  91. }
  92. function urlencode_json() {
  93. ruby -e 'require "cgi"; puts CGI.escape(STDIN.read)'
  94. }
  95. function urldecode_json() {
  96. ruby -e 'require "cgi"; puts CGI.unescape(STDIN.read)'
  97. }
  98. ;;
  99. esac
  100. unset JSONTOOLS_METHOD
  101. ## Add NDJSON support
  102. function {pp,is,urlencode,urldecode}_ndjson() {
  103. local json jsonfunc="${0//ndjson/json}"
  104. while read -r json; do
  105. $jsonfunc <<< "$json"
  106. done
  107. }