Skip to content

Commit 1a29609

Browse files
florenbuger
authored andcommitted
Attempt to fix #174. If you are trying to extract "foo" and "foo.bar" (#181)
from '{"foo": {"bar": 1}}' with EachKey, the parser would find the "foo" key, extract the contents ('{"bar": 1}'), and then *add the offset* of that extraction to the current index. This means it would never find the "bar" key. This change simply eliminates the adding of the offset.
1 parent ee4c978 commit 1a29609

File tree

3 files changed

+22
-17
lines changed

3 files changed

+22
-17
lines changed

go.mod

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
1-
module github.com/buger/jsonparser
1+
module github.com/buger/jsonparser
2+
3+
go 1.13

parser.go

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -436,13 +436,9 @@ func EachKey(data []byte, cb func(int, []byte, ValueType, error), paths ...[]str
436436
pathsMatched++
437437
pathFlags |= bitwiseFlags[pi+1]
438438

439-
v, dt, of, e := Get(data[i:])
439+
v, dt, _, e := Get(data[i:])
440440
cb(pi, v, dt, e)
441441

442-
if of != -1 {
443-
i += of
444-
}
445-
446442
if pathsMatched == len(paths) {
447443
break
448444
}

parser_test.go

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -887,18 +887,18 @@ var getStringTests = []GetTest{
887887
data: "value\b\f\n\r\tvalue", // value is unescaped since this is GetString()
888888
},
889889
{ // This test checks we avoid an infinite loop for certain malformed JSON. We don't check for all malformed JSON as it would reduce performance.
890-
desc: `malformed with double quotes`,
891-
json: `{"a"":1}`,
892-
path: []string{"a"},
890+
desc: `malformed with double quotes`,
891+
json: `{"a"":1}`,
892+
path: []string{"a"},
893893
isFound: false,
894-
data: ``,
894+
data: ``,
895895
},
896896
{ // More malformed JSON testing, to be sure we avoid an infinite loop.
897-
desc: `malformed with double quotes, and path does not exist`,
898-
json: `{"z":123,"y":{"x":7,"w":0},"v":{"u":"t","s":"r","q":0,"p":1558051800},"a":"b","c":"2016-11-02T20:10:11Z","d":"e","f":"g","h":{"i":"j""},"k":{"l":"m"}}`,
899-
path: []string{"o"},
897+
desc: `malformed with double quotes, and path does not exist`,
898+
json: `{"z":123,"y":{"x":7,"w":0},"v":{"u":"t","s":"r","q":0,"p":1558051800},"a":"b","c":"2016-11-02T20:10:11Z","d":"e","f":"g","h":{"i":"j""},"k":{"l":"m"}}`,
899+
path: []string{"o"},
900900
isFound: false,
901-
data: ``,
901+
data: ``,
902902
},
903903
}
904904

@@ -1466,6 +1466,7 @@ func TestEachKey(t *testing.T) {
14661466
{"arr", "[1]", "b"},
14671467
{"arrInt", "[3]"},
14681468
{"arrInt", "[5]"}, // Should not find last key
1469+
{"nested"},
14691470
}
14701471

14711472
keysFound := 0
@@ -1506,13 +1507,19 @@ func TestEachKey(t *testing.T) {
15061507
if string(value) != "4" {
15071508
t.Error("Should find 8 key", string(value))
15081509
}
1510+
case 8:
1511+
t.Errorf("Found key #8 that should not be found")
1512+
case 9:
1513+
if string(value) != `{"a":"test", "b":2, "nested3":{"a":"test3","b":4}, "c": "unknown"}` {
1514+
t.Error("Should find 9 key", string(value))
1515+
}
15091516
default:
1510-
t.Errorf("Should found only 8 keys")
1517+
t.Errorf("Should find only 9 keys, got %v key", idx)
15111518
}
15121519
}, paths...)
15131520

1514-
if keysFound != 8 {
1515-
t.Errorf("Should find 8 keys: %d", keysFound)
1521+
if keysFound != 9 {
1522+
t.Errorf("Should find 9 keys: %d", keysFound)
15161523
}
15171524
}
15181525

0 commit comments

Comments
 (0)