rwxrob-dot/scripts/jsonesc

26 lines
606 B
Plaintext
Raw Normal View History

2022-02-09 07:29:05 +00:00
#!/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}"