json.sh 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. # -*- mode: sh; tab-width: 4; coding: utf-8 -*-
  2. # vim: ts=4 noet ai ft=sh
  3. # JSON functions for JPM.sh
  4. # This file is part of JPM.sh.
  5. # Copyright (C) 2016 the Desktopd developers
  6. #
  7. # This program is free software: you can redistribute it and/or modify
  8. # it under the terms of the GNU Affero General Public License as
  9. # published by the Free Software Foundation, either version 3 of the
  10. # License, or (at your option) any later version.
  11. #
  12. # This program is distributed in the hope that it will be useful,
  13. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. # GNU Affero General Public License for more details.
  16. #
  17. # You should have received a copy of the GNU Affero General Public License
  18. # along with this program. If not, see <https://www.gnu.org/licenses/>.
  19. sedPath=$gLibDir/json-escape.sed
  20. jpmJsonGenSed () {
  21. # Create a sed script
  22. {
  23. printf 's/\\\\/\\\\\\\\/g\n' > "$sedPath"
  24. printf 's/"/\\\\"/g\n' >> "$sedPath"
  25. printf 's/\0/\\\\u0000/g\n' >> "$sedPath"
  26. printf 's/\1/\\\\u0001/g\n' >> "$sedPath"
  27. printf 's/\2/\\\\u0002/g\n' >> "$sedPath"
  28. printf 's/\3/\\\\u0003/g\n' >> "$sedPath"
  29. printf 's/\4/\\\\u0004/g\n' >> "$sedPath"
  30. printf 's/\5/\\\\u0005/g\n' >> "$sedPath"
  31. printf 's/\6/\\\\u0006/g\n' >> "$sedPath"
  32. printf 's/\7/\\\\u0007/g\n' >> "$sedPath"
  33. printf 's/\10/\\\\b/g\n' >> "$sedPath"
  34. printf 's/\11/\\\\t/g\n' >> "$sedPath"
  35. #printf ':a\nN\n$!ba\ns/\\n/\\\\n/g\n' >> "$sedPath"
  36. printf 's/\13/\\\\u000b/g\n' >> "$sedPath"
  37. printf 's/\14/\\\\f/g\n' >> "$sedPath"
  38. printf 's/\15/\\\\r/g\n' >> "$sedPath"
  39. printf 's/\16/\\\\u000e/g\n' >> "$sedPath"
  40. printf 's/\17/\\\\u000f/g\n' >> "$sedPath"
  41. printf 's/\20/\\\\u0010/g\n' >> "$sedPath"
  42. printf 's/\21/\\\\u0011/g\n' >> "$sedPath"
  43. printf 's/\22/\\\\u0012/g\n' >> "$sedPath"
  44. printf 's/\23/\\\\u0013/g\n' >> "$sedPath"
  45. printf 's/\24/\\\\u0014/g\n' >> "$sedPath"
  46. printf 's/\25/\\\\u0015/g\n' >> "$sedPath"
  47. printf 's/\26/\\\\u0016/g\n' >> "$sedPath"
  48. printf 's/\27/\\\\u0017/g\n' >> "$sedPath"
  49. printf 's/\30/\\\\u0018/g\n' >> "$sedPath"
  50. printf 's/\31/\\\\u0019/g\n' >> "$sedPath"
  51. printf 's/\32/\\\\u001a/g\n' >> "$sedPath"
  52. printf 's/\33/\\\\u001b/g\n' >> "$sedPath"
  53. printf 's/\34/\\\\u001c/g\n' >> "$sedPath"
  54. printf 's/\35/\\\\u001d/g\n' >> "$sedPath"
  55. printf 's/\36/\\\\u001e/g\n' >> "$sedPath"
  56. printf 's/\37/\\\\u001f/g\n' >> "$sedPath"
  57. } 2>/dev/null
  58. }
  59. # This does not sort keys.
  60. jpmJsonNormalize () {
  61. egrep -v '^\s*$' | sed 's/^\s*// ; s/":\s*/":/g ; s/,\s*"/,"/g' | tr -d '\n'
  62. }
  63. jpmJsonNormalizeArg () {
  64. printf %s "$1" | jpmJsonNormalize
  65. }
  66. jpmJsonToStringValue () {
  67. printf '"'
  68. sed -f "$sedPath" | awk 'FNR == 1 { printf "%s", $0 ; while ( getline == 1 ) { printf "\\n%s", $0; } ; }' # | awk 1 ORS='\\n'
  69. printf '"'
  70. }
  71. jpmJsonArgToStringValue () {
  72. printf %s "$1" | jpmJsonToStringValue
  73. }