-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathjson.sh
More file actions
158 lines (139 loc) · 3.55 KB
/
json.sh
File metadata and controls
158 lines (139 loc) · 3.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
#!/bin/bash
# Copyright 2018 Juliano Santos [SHAMAN]
#
# This file is part of bashsrc.
#
# bashsrc is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# bashsrc is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with bashsrc. If not, see <http://www.gnu.org/licenses/>.
[ -v __JSON_SH__ ] && return 0
readonly __JSON_SH__=1
source builtin.sh
source setup.sh
# Dependência.
setup.package 'jq (>= 1.5)'
# .FUNCTION json.load <file[str]> <obj[map]> -> [bool]
#
# Converte o arquivo contendo um documento JSON em uma estrutura
# de dados mapeada e salva no objeto apontado por 'obj'.
#
function json.load()
{
getopt.parse 2 "file:str:$1" "obj:map:$2" "${@:3}"
if [ ! -f "$1" ]; then
error.error "'$1' não é um arquivo regular"
return $?
elif [ ! -r "$1" ]; then
error.error "'$1' não foi possível ler o arquivo"
return $?
fi
json.__setmap__ file "$1" $2
return $?
}
# .FUNCTION json.loads <expr[str]> <obj[map]> -> [bool]
#
# Converte a expressão JSON em uma estrutura de dados mapeada
# e salva no objeto apontado por 'obj'.
#
# == EXEMPLO ==
#
# #!/bin/bash
#
# source json.sh
# source map.sh
#
# # Inicializa o map.
# declare -A dados=()
#
# # Implementa o tipo map.
# var dados map_t
#
# # Processando/convertendo JSON
# json.loads '{"autor":{"nome":"Juliano","sobrenome":"santos","idade":35,"pseudonimo":"SHAMAN"}}' dados
#
# Listando as chaves do mapa.
# dados.keys
#
# echo ---
#
# # Acessando valores.
# dados.get autor.nome
# dados.get autor.pseudonimo
# dados.get autor.idade
#
# == SAÍDA ==
#
# autor.nome
# autor.pseudonimo
# autor.sobrenome
# autor.idade
# ---
# Juliano
# SHAMAN
# 35
#
function json.loads()
{
getopt.parse 2 "expr:str:$1" "obj:map:$2" "${@:3}"
json.__setmap__ expr "$1" $2
return $?
}
# json.__setmap____ <tipo> <json> <map>
#
# A função interna processa e converte os dados de um arquivo ou
# expressão JSON para um array associativo especificado (map).
#
function json.__setmap__()
{
local __cjson__ __key__ __val__
local -n __ref__=$3
# Converte os objetos json em uma lista mapeada de dados.
__cjson__='path(..|
select(type == "string" or type == "number" or type == "boolean"))|
map(if type == "number" then .|tostring|"["+.+"]" else . end)|
join(".")'
# Lê a chave atual.
while IFS=$'\n' read __key__; do
__key__=${__key__//.\[/\[}
# Lê os dados.
case $1 in
expr) __val__=$(jq ".$__key__" <<< "$2");;
file) __val__=$(jq ".$__key__" "$2");;
esac 2>/dev/null
__val__=${__val__#\"}
__val__=${__val__%\"}
# Salva no objeto os dados da respectiva chave.
__ref__[$__key__]=$__val__
done < <(
# Processa a linha de comando para o tipo especificado.
case $1 in
expr) jq -r "$__cjson__" <<< "$2";;
file) jq -r "$__cjson__" "$2";;
esac 2>/dev/null
)
# Se o objeto não contém elementos processados.
[ ${#__ref__[@]} -eq 0 ] && error.error 'erro ao processar os dados json'
return $?
}
# Tipos/Implementações
typedef json_t json.loads
# .TYPE json_t
#
# Implementa o objeto 'S' com os métodos:
#
# S.loads
#
# Funções (somente-leitura)
readonly -f json.load \
json.loads \
json.__setmap__
# /* __JSON_SH__ */