forked from Thinkofname/steven-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
json.go
343 lines (319 loc) · 6.12 KB
/
json.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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
// Copyright 2015 Matthew Collins
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// Package json proxies requests to encoding/json but allows for 'broken'
// json
package json
import (
"bytes"
"encoding/json"
"fmt"
"unicode"
)
func Unmarshal(data []byte, v interface{}) error {
err := json.Unmarshal(data, v)
if err != nil {
fmt.Printf("Invalid json: '%s' trying to work around it\n", err)
data, err = clean(data)
if err == nil {
err = json.Unmarshal(data, v)
}
}
return err
}
type mode int
const (
mAny mode = iota
mEOF
mObject
mObjectKey
mObjectNext
mArray
mArrayNext
)
type state struct {
data []rune
out *bytes.Buffer
offset int
mode mode
stack []mode
err error
}
func clean(data []byte) ([]byte, error) {
var buf bytes.Buffer
s := &state{
data: []rune(string(data)),
out: &buf,
}
for s.Run() {
}
return buf.Bytes(), s.Err()
}
func (s *state) Run() bool {
if s.offset >= len(s.data) {
s.mode = mEOF
// TODO Error if unexpected
}
if s.err != nil || s.mode == mEOF {
return false
}
switch s.mode {
case mAny:
s.mode = s.any()
case mObject:
s.mode = s.object()
case mObjectKey:
s.mode = s.objectKey()
case mObjectNext:
s.mode = s.objectNext()
case mArray:
s.mode = s.array()
case mArrayNext:
s.mode = s.arrayNext()
default:
panic("unhandled case")
}
return true
}
func (s *state) any() mode {
r := s.data[s.offset]
s.offset++
switch {
case r == '{': // Object
s.out.WriteRune('{')
s.stack = append(s.stack, mObject)
return mObject
case r == '"':
s.out.WriteRune('"')
s.parseString()
return s.endValue()
case r == '[': // Array
s.out.WriteRune('[')
s.stack = append(s.stack, mArray)
return mArray
case r == 'f': // False
s.offset--
for _, c := range "false" {
if s.data[s.offset] != c {
s.err = fmt.Errorf("any: unexpected rune %c", s.data[s.offset])
return 0
}
s.out.WriteRune(s.data[s.offset])
s.offset++
}
return s.endValue()
case r == 't':
s.offset--
for _, c := range "true" {
if s.data[s.offset] != c {
s.err = fmt.Errorf("any: unexpected rune %c", s.data[s.offset])
return 0
}
s.out.WriteRune(s.data[s.offset])
s.offset++
}
return s.endValue()
case unicode.IsDigit(r), r == '-':
s.out.WriteRune(r)
s.parseNumber()
return s.endValue()
case unicode.IsSpace(r):
return mAny
case r == '/':
s.parseComment()
return mAny
}
s.out.WriteRune('"')
s.out.WriteRune(r)
for s.offset < len(s.data) {
r := s.data[s.offset]
s.offset++
if !unicode.IsLetter(r) {
s.offset--
break
}
s.out.WriteRune(r)
}
s.out.WriteRune('"')
return s.endValue()
}
func (s *state) endValue() mode {
if len(s.stack) == 0 {
return mEOF
}
m := s.stack[len(s.stack)-1]
switch m {
case mObject:
return mObjectNext
case mArray:
return mArrayNext
default:
panic(m)
}
}
func (s *state) array() mode {
r := s.data[s.offset]
s.offset++
switch {
case r == ']':
s.out.WriteRune(']')
s.stack = s.stack[:len(s.stack)-1]
return s.endValue()
case unicode.IsSpace(r):
return mArray
}
s.offset--
return s.any()
}
func (s *state) arrayNext() mode {
r := s.data[s.offset]
s.offset++
switch {
case r == '/':
s.parseComment()
return mArrayNext
case r == ',':
s.out.WriteRune(',')
return mAny
case r == ']':
s.out.WriteRune(']')
s.stack = s.stack[:len(s.stack)-1]
return s.endValue()
case unicode.IsSpace(r):
return mArrayNext
}
s.err = fmt.Errorf("arrayNext: unexpected rune %c", r)
return 0
}
func (s *state) object() mode {
// Expecting keys
r := s.data[s.offset]
s.offset++
switch {
case r == '/':
s.parseComment()
return mObject
case r == '"': // Key start
s.out.WriteRune('"')
s.parseString()
return mObjectKey
case unicode.IsLetter(r): // Broken key
s.out.WriteRune('"')
s.out.WriteRune(r)
for s.offset < len(s.data) {
r := s.data[s.offset]
s.offset++
if !unicode.IsLetter(r) {
s.offset--
break
}
s.out.WriteRune(r)
}
s.out.WriteRune('"')
return mObjectKey
case r == '}':
s.out.WriteRune('}')
s.stack = s.stack[:len(s.stack)-1]
return s.endValue()
case unicode.IsSpace(r):
return mObject
}
s.err = fmt.Errorf("object: unexpected rune %c", r)
return 0
}
func (s *state) objectKey() mode {
r := s.data[s.offset]
s.offset++
switch {
case r == ':':
s.out.WriteRune(':')
return mAny
case unicode.IsSpace(r):
return mObjectKey
}
s.err = fmt.Errorf("objectKey: unexpected rune %c", r)
return 0
}
func (s *state) objectNext() mode {
r := s.data[s.offset]
s.offset++
switch {
case r == '/':
s.parseComment()
return mObjectNext
case r == ',':
s.out.WriteRune(',')
return mObject
case r == '}':
s.out.WriteRune('}')
s.stack = s.stack[:len(s.stack)-1]
return s.endValue()
case unicode.IsSpace(r):
return mObjectNext
}
s.err = fmt.Errorf("objectNext: unexpected rune %c", r)
return 0
}
func (s *state) parseNumber() {
for s.offset < len(s.data) {
r := s.data[s.offset]
s.offset++
switch {
case unicode.IsDigit(r), r == '.':
s.out.WriteRune(r)
default:
s.offset--
return
}
}
}
func (s *state) parseString() {
for s.offset < len(s.data) {
r := s.data[s.offset]
s.offset++
switch r {
case '"':
s.out.WriteRune(r)
return
case '\\':
s.out.WriteRune(r)
r = s.data[s.offset]
s.offset++
fallthrough
default:
s.out.WriteRune(r)
}
}
}
func (s *state) parseComment() {
t := s.data[s.offset]
s.offset++
for s.offset < len(s.data) {
r := s.data[s.offset]
s.offset++
switch r {
case '\n':
if t == '/' {
return
}
case '*':
if s.data[s.offset+1] == '/' || t == '*' {
s.offset++
return
}
}
}
}
func (s *state) Err() error {
return s.err
}