forked from tokuhirom/json_path_scanner
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjson_path_scanner.go
More file actions
45 lines (40 loc) · 894 Bytes
/
json_path_scanner.go
File metadata and controls
45 lines (40 loc) · 894 Bytes
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
package json_path_scanner
import (
"strconv"
"strings"
)
type PathValue struct {
Path string
Value interface{}
}
func newPathValue(key string, value interface{}) *PathValue {
return &PathValue{
Path: key,
Value: value,
}
}
func Scan(value interface{}, ch chan<- *PathValue) {
defer close(ch)
scanJson("$", value, ch)
}
func scanJson(label string, value interface{}, ch chan<- *PathValue) {
switch value.(type) {
case int, float64, string, bool, nil:
ch <- newPathValue(label, value)
case map[string]interface{}:
m := value.(map[string]interface{})
for k, v := range m {
if strings.Contains(k, ".") {
scanJson(label+"['"+k+"']", v, ch)
} else {
scanJson(label+"."+k, v, ch)
}
}
case []interface{}:
for i, v := range value.([]interface{}) {
scanJson(label+"["+strconv.Itoa(i)+"]", v, ch)
}
default:
panic("Unsupported type in json")
}
}