Skip to content

Commit 231c90d

Browse files
authored
Merge pull request #103 from JoshKCarroll/array-string-bug
Bugfix: Get() via index fails on string
2 parents 9fc8efe + e05546d commit 231c90d

File tree

2 files changed

+11
-1
lines changed

2 files changed

+11
-1
lines changed

parser.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -185,11 +185,14 @@ func searchKeys(data []byte, keys ...string) int {
185185
var curIdx int
186186
var valueFound []byte
187187
var valueOffset int
188-
189188
ArrayEach(data[i:], func(value []byte, dataType ValueType, offset int, err error) {
190189
if curIdx == aIdx {
191190
valueFound = value
192191
valueOffset = offset
192+
if dataType == String {
193+
valueOffset = valueOffset - 2
194+
valueFound = data[i + valueOffset:i + valueOffset + len(value) + 2]
195+
}
193196
}
194197
curIdx += 1
195198
})

parser_test.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -399,6 +399,13 @@ var getTests = []GetTest{
399399
isFound: true,
400400
data: `{"b":"2"}`,
401401
},
402+
{
403+
desc: "get string from array",
404+
json: `{"a":[{"b":1},"foo", 3],"c":{"c":[1,2]}}`,
405+
path: []string{"a", "[1]"},
406+
isFound: true,
407+
data: "foo",
408+
},
402409
{
403410
desc: "key in path is index",
404411
json: `{"a":[{"b":"1"},{"b":"2"},3],"c":{"c":[1,2]}}`,

0 commit comments

Comments
 (0)