-
Notifications
You must be signed in to change notification settings - Fork 2
/
node.go
99 lines (84 loc) · 1.81 KB
/
node.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
package jzon
import (
"sync"
)
var (
nodeStackPool = sync.Pool{
New: func() interface{} {
return &nodeStack{}
},
}
)
type readNode struct {
m map[string]interface{}
s []interface{}
field string
}
type nodeStack struct {
stack []readNode
}
func (ns *nodeStack) initArray(s []interface{}) *nodeStack {
ns.stack = append(ns.stack[:0], readNode{
s: s,
})
return ns
}
func (ns *nodeStack) initObject(m map[string]interface{}) *nodeStack {
ns.stack = append(ns.stack[:0], readNode{
m: m,
})
return ns
}
func (ns *nodeStack) pushArray(field string) *nodeStack {
ns.stack = append(ns.stack, readNode{
s: make([]interface{}, 0),
field: field,
})
return ns
}
func (ns *nodeStack) pushObject(field string) *nodeStack {
ns.stack = append(ns.stack, readNode{
m: map[string]interface{}{},
field: field,
})
return ns
}
func (ns *nodeStack) topObject() map[string]interface{} {
return ns.stack[len(ns.stack)-1].m
}
func (ns *nodeStack) topArray() []interface{} {
return ns.stack[len(ns.stack)-1].s
}
func (ns *nodeStack) setTopObject(key string, value interface{}) {
ns.stack[len(ns.stack)-1].m[key] = value
}
func (ns *nodeStack) appendTopArray(value interface{}) {
l := len(ns.stack) - 1
ns.stack[l].s = append(ns.stack[l].s, value)
}
func (ns *nodeStack) popObject() {
l := len(ns.stack) - 1
first := &ns.stack[l]
next := &ns.stack[l-1]
if next.m != nil {
next.m[first.field] = first.m
} else {
next.s = append(next.s, first.m)
}
ns.stack = ns.stack[:l]
}
func (ns *nodeStack) popArray() {
l := len(ns.stack) - 1
first := &ns.stack[l]
next := &ns.stack[l-1]
if next.m != nil {
next.m[first.field] = first.s
} else {
next.s = append(next.s, first.s)
}
ns.stack = ns.stack[:l]
}
func releaseNodeStack(ns *nodeStack) {
ns.stack = ns.stack[:0]
nodeStackPool.Put(ns)
}