Skip to content

Commit 3fdec1a

Browse files
authored
Merge pull request #195 from AllenX2018/fix-issue-165
fix issue #165
2 parents 47da132 + 74af025 commit 3fdec1a

File tree

2 files changed

+36
-5
lines changed

2 files changed

+36
-5
lines changed

parser.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -388,7 +388,6 @@ func EachKey(data []byte, cb func(int, []byte, ValueType, error), paths ...[]str
388388
}
389389
}
390390

391-
var stackbuf [unescapeStackBufSize]byte // stack-allocated array for allocation-free unescaping of small strings
392391
pathsBuf := make([]string, maxPath)
393392

394393
for i < ln {
@@ -420,6 +419,7 @@ func EachKey(data []byte, cb func(int, []byte, ValueType, error), paths ...[]str
420419
// for unescape: if there are no escape sequences, this is cheap; if there are, it is a
421420
// bit more expensive, but causes no allocations unless len(key) > unescapeStackBufSize
422421
var keyUnesc []byte
422+
var stackbuf [unescapeStackBufSize]byte
423423
if !keyEscaped {
424424
keyUnesc = key
425425
} else if ku, err := Unescape(key, stackbuf[:]); err != nil {

parser_test.go

+35-4
Original file line numberDiff line numberDiff line change
@@ -1579,7 +1579,33 @@ func TestObjectEach(t *testing.T) {
15791579
}
15801580
}
15811581

1582-
var testJson = []byte(`{"name": "Name", "order": "Order", "sum": 100, "len": 12, "isPaid": true, "nested": {"a":"test", "b":2, "nested3":{"a":"test3","b":4}, "c": "unknown"}, "nested2": {"a":"test2", "b":3}, "arr": [{"a":"zxc", "b": 1}, {"a":"123", "b":2}], "arrInt": [1,2,3,4], "intPtr": 10}`)
1582+
var testJson = []byte(`{
1583+
"name": "Name",
1584+
"order": "Order",
1585+
"sum": 100,
1586+
"len": 12,
1587+
"isPaid": true,
1588+
"nested": {"a":"test", "b":2, "nested3":{"a":"test3","b":4}, "c": "unknown"},
1589+
"nested2": {
1590+
"a":"test2",
1591+
"b":3
1592+
},
1593+
"arr": [
1594+
{
1595+
"a":"zxc",
1596+
"b": 1
1597+
},
1598+
{
1599+
"a":"123",
1600+
"b":2
1601+
}
1602+
],
1603+
"arrInt": [1,2,3,4],
1604+
"intPtr": 10,
1605+
"a\n":{
1606+
"b\n":99
1607+
}
1608+
}`)
15831609

15841610
func TestEachKey(t *testing.T) {
15851611
paths := [][]string{
@@ -1594,6 +1620,7 @@ func TestEachKey(t *testing.T) {
15941620
{"arrInt", "[5]"}, // Should not find last key
15951621
{"nested"},
15961622
{"arr", "["}, // issue#177 Invalid arguments
1623+
{"a\n", "b\n"}, // issue#165
15971624
}
15981625

15991626
keysFound := 0
@@ -1642,13 +1669,17 @@ func TestEachKey(t *testing.T) {
16421669
}
16431670
case 10:
16441671
t.Errorf("Found key #10 that should not be found")
1672+
case 11:
1673+
if string(value) != "99" {
1674+
t.Error("Should find 10 key", string(value))
1675+
}
16451676
default:
1646-
t.Errorf("Should find only 9 keys, got %v key", idx)
1677+
t.Errorf("Should find only 10 keys, got %v key", idx)
16471678
}
16481679
}, paths...)
16491680

1650-
if keysFound != 9 {
1651-
t.Errorf("Should find 9 keys: %d", keysFound)
1681+
if keysFound != 10 {
1682+
t.Errorf("Should find 10 keys: %d", keysFound)
16521683
}
16531684
}
16541685

0 commit comments

Comments
 (0)