@@ -31,6 +31,7 @@ func tokenEnd(data []byte) int {
31
31
return - 1
32
32
}
33
33
34
+
34
35
// Find position of next character which is not ' ', ',', '}' or ']'
35
36
func nextToken (data []byte , skipComma bool ) int {
36
37
for i , c := range data {
@@ -285,6 +286,19 @@ func Get(data []byte, keys ...string) (value []byte, dataType ValueType, offset
285
286
return value , dataType , endOffset , nil
286
287
}
287
288
289
+ func nextArrayItem (data []byte ) int {
290
+ for i , c := range data {
291
+ switch c {
292
+ case ' ' , '\n' , '\r' , '\t' :
293
+ continue
294
+ default :
295
+ return i
296
+ }
297
+ }
298
+
299
+ return - 1
300
+ }
301
+
288
302
// ArrayEach is used when iterating arrays, accepts a callback function with the same return arguments as `Get`.
289
303
func ArrayEach (data []byte , cb func (value []byte , dataType ValueType , offset int , err error ), keys ... string ) (err error ) {
290
304
if len (data ) == 0 {
@@ -330,6 +344,20 @@ func ArrayEach(data []byte, cb func(value []byte, dataType ValueType, offset int
330
344
}
331
345
332
346
offset += o
347
+
348
+ nextItem := nextArrayItem (data [offset :])
349
+ if nextItem == - 1 {
350
+ return MalformedArrayError
351
+ }
352
+ offset += nextItem
353
+
354
+ if data [offset ] == ']' {
355
+ break
356
+ }
357
+
358
+ if data [offset ] != ',' {
359
+ return MalformedArrayError
360
+ }
333
361
}
334
362
335
363
return nil
0 commit comments