Skip to content

Commit 016ea00

Browse files
authored
Merge pull request #97 from jensenak/bugfix-recursive-object-index
Bugfix recursive object index
2 parents 7266f2b + 4b6934c commit 016ea00

File tree

2 files changed

+18
-2
lines changed

2 files changed

+18
-2
lines changed

parser.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,11 @@ func searchKeys(data []byte, keys ...string) int {
195195
if valueFound == nil {
196196
return -1
197197
} else {
198-
return i + valueOffset + searchKeys(valueFound, keys[level+1:]...)
198+
subIndex := searchKeys(valueFound, keys[level+1:]...)
199+
if subIndex < 0 {
200+
return -1
201+
}
202+
return i + valueOffset + subIndex
199203
}
200204
} else {
201205
// Do not search for keys inside arrays

parser_test.go

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -357,7 +357,19 @@ var getTests = []GetTest{
357357
path: []string{"b"},
358358
isFound: false,
359359
},
360-
360+
{ // Issue #81
361+
desc: `missing key in object in array`,
362+
json: `{"p":{"a":[{"u":"abc","t":"th"}]}}`,
363+
path: []string{"p", "a", "[0]", "x"},
364+
isFound: false,
365+
},
366+
{ // Issue #81 counter test
367+
desc: `existing key in object in array`,
368+
json: `{"p":{"a":[{"u":"abc","t":"th"}]}}`,
369+
path: []string{"p", "a", "[0]", "u"},
370+
isFound: true,
371+
data: "abc",
372+
},
361373
{ // This test returns not found instead of a parse error, as checking for the malformed JSON would reduce performance
362374
desc: "malformed key (followed by comma followed by colon)",
363375
json: `{"a",:1}`,

0 commit comments

Comments
 (0)