Allow multiple lines in var.json (#104)

This commit is contained in:
Felipe Santos 2023-05-12 03:09:14 -03:00 committed by GitHub
parent 854f8d646c
commit d3e2077093
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -139,14 +139,17 @@ function bashio::var.json() {
counter=0;
for i in "${data[@]}"; do
item="\"$i\""
separator=","
if [ $((++counter%2)) -eq 0 ]; then
separator=":";
fi
item="\"$i\""
if [[ "${i:0:1}" == "^" ]]; then
item="${i:1}"
if [[ "${i:0:1}" == "^" ]]; then
item="${i:1}"
else
item=$(bashio::var.json_string "${i}")
fi
fi
json="$json$separator$item";
@ -156,3 +159,23 @@ function bashio::var.json() {
return "${__BASHIO_EXIT_OK}"
}
# ------------------------------------------------------------------------------
# Escapes a string for use in a JSON object.
#
# Arguments:
# $1 String to escape
# ------------------------------------------------------------------------------
function bashio::var.json_string() {
local string="${1}"
local json_string
# https://stackoverflow.com/a/50380697/12156188
if json_string=$(echo -n "${string}" | jq -Rs .); then
echo "${json_string}"
return "${__BASHIO_EXIT_OK}"
fi
bashio::log.error "Failed to escape string"
return "${__BASHIO_EXIT_NOK}"
}