Welcome to mirror list, hosted at ThFree Co, Russian Federation.

jshn.sh « sh - git.openwrt.org/project/libubox.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 7f922c4f41a3a62f0ebcef421b4bcf6e62a8c59c (plain)
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
# functions for parsing and generating json

jshn_append() {
	local var="$1"
	local value="$2"
	local sep="${3:- }"

	eval "export -- \"$var=\${$var:+\${$var}\${value:+\$sep}}\$value\""
}

json_init() {
	[ -n "$JSON_UNSET" ] && eval "unset $JSON_UNSET"
	export -- JSON_SEQ=0 JSON_STACK= JSON_CUR="JSON_VAR" JSON_UNSET="" KEYS_JSON_VAR= TYPE_JSON_VAR=
}

json_add_generic() {
	local type="$1"
	local var="$2"
	local val="$3"
	local cur="${4:-$JSON_CUR}"

	if [ "${cur%%[0-9]*}" = "JSON_ARRAY" ]; then
		eval "local aseq=\"\${SEQ_$cur}\""
		var=$(( ${aseq:-0} + 1 ))
		export -- "SEQ_$cur=$var"
	else
		local name="$(echo -n "$var" | tr -C '[a-zA-Z0-9_]' _)"
		[[ "$name" == "$var" ]] || export -- "NAME_${cur}_${name}=$var"
		var="$name"
	fi

	export -- "${cur}_$var=$val"
	export -- "TYPE_${cur}_$var=$type"
	jshn_append JSON_UNSET "${cur}_$var TYPE_${cur}_$var"
	jshn_append "KEYS_${cur}" "$var"
}

json_add_table() {
	local TYPE="$1"
	JSON_SEQ=$(($JSON_SEQ + 1))
	jshn_append JSON_STACK "$JSON_CUR"
	local table="JSON_$TYPE$JSON_SEQ"
	export -- "UP_$table=$JSON_CUR"
	export -- "KEYS_$table="
	jshn_append JSON_UNSET "KEYS_$table UP_$table"
	[ "$TYPE" = "ARRAY" ] && jshn_append JSON_UNSET "SEQ_$table"
	JSON_CUR="$table"
}

json_add_object() {
	local cur="$JSON_CUR"
	json_add_table TABLE
	json_add_generic object "$1" "$JSON_CUR" "$cur"
}

json_close_object() {
	local oldstack="$JSON_STACK"
	JSON_CUR="${JSON_STACK##* }"
	JSON_STACK="${JSON_STACK% *}"
	[[ "$oldstack" == "$JSON_STACK" ]] && JSON_STACK=
}

json_add_array() {
	local cur="$JSON_CUR"
	json_add_table ARRAY
	json_add_generic array "$1" "$JSON_CUR" "$cur"
}

json_close_array() {
	json_close_object
}

json_add_string() {
	json_add_generic string "$1" "$2"
}

json_add_int() {
	json_add_generic int "$1" "$2"
}

json_add_boolean() {
	json_add_generic boolean "$1" "$2"
}

# functions read access to json variables

json_load() {
	eval `jshn -r "$1"`
}

json_dump() {
	jshn "$@" -w
}

json_get_type() {
	local dest="$1"
	local var="$2"
	eval "export -- \"$dest=\${TYPE_${JSON_CUR}_$var}\""
}

json_get_var() {
	local dest="$1"
	local var="$(echo -n "$2" | tr -C '[a-zA-Z0-9_]' _)"
	eval "export -- \"$dest=\${${JSON_CUR}_$var}\""
}

json_get_vars() {
	while [ "$#" -gt 0 ]; do
		local _var="$1"; shift
		json_get_var "$_var" "$_var"
	done
}

json_select() {
	local target="$1"
	local type

	[ -z "$1" ] && {
		JSON_CUR="JSON_VAR"
		return
	}
	[[ "$1" == ".." ]] && {
		eval "JSON_CUR=\"\${UP_$JSON_CUR}\""
		return;
	}
	json_get_type type "$target"
	case "$type" in
		object|array)
			json_get_var JSON_CUR "$target"
		;;
		*)
			echo "WARNING: Variable '$target' does not exist or is not an array/object"
		;;
	esac
}