rwxrob-dot/scripts/jsonesc
Rob Muhlestein 2ea0289e9f Rebase
2022-02-09 02:29:05 -05:00

26 lines
606 B
Bash
Executable File

#!/usr/bin/env bash
# note, unicode is passed as is (as it should be, imho)
# and forward slashes are not unnecessarily escaped
jsonesc () {
local q="$1"
q=${q//$'\\'/\\\\} # back slash (must be first)
q=${q//$'\"'/'\"'} # double quote
#q=${q//$'/'/'//'} # forward slash (but just no)
q=${q/$'\b'/'\b'} # backspace
q=${q//$'\f'/'\f'} # form feed
q=${q//$'\n'/'\n'} # line feed
q=${q//$'\r'/'\r'} # carriage return
q=${q/$'\t'/'\t'} # tab
printf '%s' $q
}
IFS=
read line
buf=$line
while IFS= read line; do
buf="${buf}"$'\n'"${line}"
done
jsonesc "${buf}"