forked from spyzhov/ajson
-
Notifications
You must be signed in to change notification settings - Fork 0
/
decode.go
212 lines (201 loc) · 4.54 KB
/
decode.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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
package ajson
import (
. "github.com/spyzhov/ajson/internal"
)
/*
The action codes.
Copy from `internal/state.go:144`
*/
const (
cl States = -2 /* colon */
cm States = -3 /* comma */
//qt States = -4 /* quote */
bo States = -5 /* bracket open */
co States = -6 /* curly br. open */
bc States = -7 /* bracket close */
cc States = -8 /* curly br. close */
ec States = -9 /* curly br. empty */
)
// Unmarshal parses the JSON-encoded data and return the root node of struct.
//
// Doesn't calculate values, just type of stored value. It will store link to the data, on all life long.
func Unmarshal(data []byte) (root *Node, err error) {
buf := newBuffer(data)
var (
state States
key *string
current *Node
)
_, err = buf.first()
if err != nil {
return nil, buf.errorEOF()
}
for {
state = buf.getState()
if state == __ {
return nil, buf.errorSymbol()
}
if state >= GO {
// region Change State
switch buf.state {
case ST:
if current != nil && current.IsObject() && key == nil {
// Detected: Key
key, err = getString(buf)
buf.state = CO
} else {
// Detected: String
current, err = newNode(current, buf, String, &key)
if err != nil {
break
}
err = buf.string(quotes, false)
current.borders[1] = buf.index + 1
buf.state = OK
if current.parent != nil {
current = current.parent
}
}
case MI, ZE, IN:
current, err = newNode(current, buf, Numeric, &key)
if err != nil {
break
}
err = buf.numeric(false)
current.borders[1] = buf.index
buf.index -= 1
buf.state = OK
if current.parent != nil {
current = current.parent
}
case T1, F1:
current, err = newNode(current, buf, Bool, &key)
if err != nil {
break
}
if buf.state == T1 {
err = buf.true()
} else {
err = buf.false()
}
current.borders[1] = buf.index + 1
buf.state = OK
if current.parent != nil {
current = current.parent
}
case N1:
current, err = newNode(current, buf, Null, &key)
if err != nil {
break
}
err = buf.null()
current.borders[1] = buf.index + 1
buf.state = OK
if current.parent != nil {
current = current.parent
}
}
// endregion Change State
} else {
// region Action
switch state {
case ec: /* empty } */
if key != nil {
err = buf.errorSymbol()
}
fallthrough
case cc: /* } */
if current != nil && current.IsObject() && !current.ready() {
current.borders[1] = buf.index + 1
if current.parent != nil {
current = current.parent
}
} else {
err = buf.errorSymbol()
}
buf.state = OK
case bc: /* ] */
if current != nil && current.IsArray() && !current.ready() {
current.borders[1] = buf.index + 1
if current.parent != nil {
current = current.parent
}
} else {
err = buf.errorSymbol()
}
buf.state = OK
case co: /* { */
current, err = newNode(current, buf, Object, &key)
buf.state = OB
case bo: /* [ */
current, err = newNode(current, buf, Array, &key)
buf.state = AR
case cm: /* , */
if current == nil {
return nil, buf.errorSymbol()
}
if current.IsObject() {
buf.state = KE
} else if current.IsArray() {
buf.state = VA
} else {
err = buf.errorSymbol()
}
case cl: /* : */
if current == nil || !current.IsObject() || key == nil {
err = buf.errorSymbol()
} else {
buf.state = VA
}
default: /* syntax error */
err = buf.errorSymbol()
}
// endregion Action
}
if err != nil {
return
}
if buf.step() != nil {
break
}
if _, err = buf.first(); err != nil {
err = nil
break
}
}
if current == nil || buf.state != OK {
err = buf.errorEOF()
} else {
root = current.root()
if !root.ready() {
err = buf.errorEOF()
root = nil
}
}
return
}
// UnmarshalSafe do the same thing as Unmarshal, but copy data to the local variable, to make it editable.
func UnmarshalSafe(data []byte) (root *Node, err error) {
var safe []byte
safe = append(safe, data...)
return Unmarshal(safe)
}
// Must returns a Node if there was no error. Else - panic with error as the value.
func Must(root *Node, err error) *Node {
if err != nil {
panic(err)
}
return root
}
func getString(b *buffer) (*string, error) {
start := b.index
err := b.string(quotes, false)
if err != nil {
return nil, err
}
value, ok := unquote(b.data[start:b.index+1], quotes)
if !ok {
return nil, errorSymbol(b)
}
return &value, nil
}