/
json_iterator.go
155 lines (118 loc) · 2.97 KB
/
json_iterator.go
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
package json
import (
"fmt"
"github.com/tidwall/gjson"
)
// Iterator allows to traverse a tree data structure
type Iterator interface {
Next() bool
Value() *PathElem
}
// PathElem is an element inside a JSON tree, identified by a JSON value and a path inside the
// overall JSON document
type PathElem struct {
curMap []*kvPair
curArr []gjson.Result
index int
parent *PathElem
Path []string
Value *gjson.Result
}
type iterator struct {
Data *gjson.Result
OnlyLeaves bool
current *PathElem
done bool
}
type kvPair struct {
k string
v gjson.Result
}
// NewIterator initializes an iterator to a JSON document taken as input
func NewIterator(data *gjson.Result) Iterator {
return &iterator{Data: data}
}
func (j *iterator) Next() bool {
if j.done {
return false
}
if j.current == nil {
if j.Data.Type != gjson.JSON {
return false
}
j.current = &PathElem{
Value: j.Data,
Path: []string{},
index: 0,
}
if j.Data.IsObject() {
j.current.curMap = mapToKVPairs(j.current.Value.Map())
} else if j.Data.IsArray() {
j.current.curArr = j.current.Value.Array()
}
return true
}
var nextPathElem *PathElem
if j.current.Value.Type == gjson.JSON {
if j.current.Value.IsObject() {
j.current.curMap = mapToKVPairs(j.current.Value.Map())
} else if j.current.Value.IsArray() {
j.current.curArr = j.current.Value.Array()
}
child := &PathElem{
parent: j.current,
index: -1,
}
nextPathElem = child.nextPathElemFromParent()
} else {
nextPathElem = j.current.nextPathElemFromParent()
}
if nextPathElem == nil {
j.done = true
return false
}
j.current = nextPathElem
return true
}
// This might be a little bit hacky, but needs to be done in order for the map to be ordered
func mapToKVPairs(in map[string]gjson.Result) []*kvPair {
out := make([]*kvPair, 0, len(in))
for k, v := range in {
out = append(out, &kvPair{k, v})
}
return out
}
func (j *iterator) Value() *PathElem {
return j.current
}
func (p *PathElem) nextPathElemFromParent() *PathElem {
if p.parent == nil {
return nil
}
nextPathElem := &PathElem{
parent: p.parent,
index: p.index + 1,
}
if p.parent.curMap != nil {
if nextPathElem.index == len(nextPathElem.parent.curMap) {
// we are done on this level, go recursively upwards
return p.parent.nextPathElemFromParent()
}
// go to next item in parent map
nextItem := nextPathElem.parent.curMap[nextPathElem.index]
nextPathElem.Value = &nextItem.v
nextPathElem.Path = append(p.parent.Path, nextItem.k)
return nextPathElem
} else if p.parent.curArr != nil {
if nextPathElem.index == len(p.parent.curArr) {
// we are done on this level, go recursively upwards
return p.parent.nextPathElemFromParent()
}
// go to next item in parent array
nextItem := nextPathElem.parent.curArr[nextPathElem.index]
nextPathElem.Value = &nextItem
nextPathElem.Path = append(p.parent.Path, fmt.Sprintf("[%d]", nextPathElem.index))
return nextPathElem
}
return nil
}