mirror of
https://github.com/hassio-addons/bashio.git
synced 2025-05-03 18:41:31 +00:00
Allow multiple lines in var.json (#104)
This commit is contained in:
parent
854f8d646c
commit
d3e2077093
1 changed files with 27 additions and 4 deletions
31
lib/var.sh
31
lib/var.sh
|
@ -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}"
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue