-
Notifications
You must be signed in to change notification settings - Fork 2
/
iterator_read_object.go
72 lines (71 loc) · 1.57 KB
/
iterator_read_object.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
package jzon
func readObjectWithStack(it *Iterator, _ byte) (interface{}, error) {
c, err := it.nextToken()
if err != nil {
return nil, err
}
topObj := map[string]interface{}{}
if c == '}' {
it.head++
return topObj, nil
}
for {
if c != '"' {
return nil, UnexpectedByteError{got: c, exp: '"'}
}
it.head++
field, err := it.readObjectField()
if err != nil {
return nil, err
}
c, err = it.nextToken()
if err != nil {
return nil, err
}
it.head++
// We disabled the following switch to test benchmark
// comparing to using builtin stack
// the result is using ours own stack will improve the
// performance by about 25%
switch c {
case '{':
s := stackPool.Get().(*stack).initObject()
ns := nodeStackPool.Get().(*nodeStack).
initObject(topObj).
pushObject(field)
ret, err := readWithStack(it, stackElementObjectBegin, s, ns)
releaseNodeStack(ns)
stackPool.Put(s)
return ret, err
case '[':
s := stackPool.Get().(*stack).initObject()
ns := nodeStackPool.Get().(*nodeStack).
initObject(topObj).
pushArray(field)
ret, err := readWithStack(it, stackElementArrayBegin, s, ns)
releaseNodeStack(ns)
stackPool.Put(s)
return ret, err
}
o, err := readFunctions[c](it, c)
if err != nil {
return nil, err
}
topObj[field] = o
c, err = it.nextToken()
if err != nil {
return nil, err
}
it.head++
if c == '}' {
return topObj, nil
}
if c != ',' {
return nil, UnexpectedByteError{got: c, exp: '}', exp2: ','}
}
c, err = it.nextToken()
if err != nil {
return nil, err
}
}
}