Skip to content

Commit c706c1f

Browse files
committed
Merge #22
1 parent 7a7ff39 commit c706c1f

File tree

2 files changed

+23
-7
lines changed

2 files changed

+23
-7
lines changed

parser.go

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,19 @@ import (
99
"unsafe"
1010
)
1111

12+
func tokenEnd(data []byte) int {
13+
for i, c := range data {
14+
switch c {
15+
case ' ', '\n', '\r', '\t', ',', '}', ']':
16+
return i
17+
}
18+
}
19+
20+
return -1
21+
}
22+
1223
// Find position of next character which is not ' ', ',', '}' or ']'
13-
func nextValue(data []byte, skipComma bool) int {
24+
func nextToken(data []byte, skipComma bool) int {
1425
for i, c := range data {
1526
switch c {
1627
case ' ', '\n', '\r', '\t':
@@ -102,7 +113,7 @@ func searchKeys(data []byte, keys ...string) int {
102113
i += strEnd
103114
keyEnd := i - 1
104115

105-
valueOffset := nextValue(data[i:], true)
116+
valueOffset := nextToken(data[i:], true)
106117
if valueOffset == -1 {
107118
return -1
108119
}
@@ -166,7 +177,7 @@ func Get(data []byte, keys ...string) (value []byte, dataType int, offset int, e
166177
}
167178

168179
// Go to closest value
169-
nO := nextValue(data[offset:], false)
180+
nO := nextToken(data[offset:], false)
170181

171182
if nO == -1 {
172183
return []byte{}, NotExist, -1, errors.New("Malformed JSON error")
@@ -205,9 +216,7 @@ func Get(data []byte, keys ...string) (value []byte, dataType int, offset int, e
205216
endOffset += offset
206217
} else {
207218
// Number, Boolean or None
208-
end := bytes.IndexFunc(data[endOffset:], func(c rune) bool {
209-
return c == ' ' || c == '\n' || c == ',' || c == '}' || c == ']'
210-
})
219+
end := tokenEnd(data[endOffset:])
211220

212221
if end == -1 {
213222
return nil, dataType, offset, errors.New("Value looks like Number/Boolean/None, but can't find its end: ',' or '}' symbol")
@@ -265,7 +274,7 @@ func ArrayEach(data []byte, cb func(value []byte, dataType int, offset int, err
265274
}
266275

267276
// Go to closest value
268-
nO := nextValue(data[offset:], false)
277+
nO := nextToken(data[offset:], false)
269278

270279
if nO == -1 {
271280
return errors.New("Malformed JSON")

parser_test.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -347,6 +347,13 @@ var getBoolTests = []Test{
347347
path: []string{"a"},
348348
isErr: true,
349349
},
350+
Test{
351+
desc: `read boolean true with whitespace and another key`,
352+
json: "{\r\t\n \"a\"\r\t\n :\r\t\n true\r\t\n ,\r\t\n \"b\": 1}",
353+
path: []string{"a"},
354+
isFound: true,
355+
data: true,
356+
},
350357
}
351358

352359
var getArrayTests = []Test{

0 commit comments

Comments
 (0)