Skip to content

Commit 2cde0f9

Browse files
committed
Fix array last item bug
1 parent e2dfe78 commit 2cde0f9

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

parser.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ func tokenEnd(data []byte) int {
3131
return -1
3232
}
3333

34+
3435
// Find position of next character which is not ' ', ',', '}' or ']'
3536
func nextToken(data []byte, skipComma bool) int {
3637
for i, c := range data {
@@ -285,6 +286,19 @@ func Get(data []byte, keys ...string) (value []byte, dataType ValueType, offset
285286
return value, dataType, endOffset, nil
286287
}
287288

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+
288302
// ArrayEach is used when iterating arrays, accepts a callback function with the same return arguments as `Get`.
289303
func ArrayEach(data []byte, cb func(value []byte, dataType ValueType, offset int, err error), keys ...string) (err error) {
290304
if len(data) == 0 {
@@ -330,6 +344,20 @@ func ArrayEach(data []byte, cb func(value []byte, dataType ValueType, offset int
330344
}
331345

332346
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+
}
333361
}
334362

335363
return nil

0 commit comments

Comments
 (0)