Skip to content

Commit 5048f35

Browse files
authored
Merge pull request #45 from buger/key-each
Add EachKey
2 parents 2248061 + 16a5ab2 commit 5048f35

File tree

5 files changed

+226
-1
lines changed

5 files changed

+226
-1
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
SOURCE = parser.go
22
CONTAINER = jsonparser
33
SOURCE_PATH = /go/src/github.com/buger/jsonparser
4-
BENCHMARK = JsonParserSmall
4+
BENCHMARK = JsonParser
55
BENCHTIME = 5s
66
TEST = .
77
DRUN = docker run -v `pwd`:$(SOURCE_PATH) -i -t $(CONTAINER)

benchmark/benchmark_medium_payload_test.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,32 @@ func BenchmarkJsonParserMedium(b *testing.B) {
3434
}
3535
}
3636

37+
func BenchmarkJsonParserEachKeyManualMedium(b *testing.B) {
38+
for i := 0; i < b.N; i++ {
39+
paths := [][]string{
40+
[]string{"person", "name", "fullName"},
41+
[]string{"person", "github", "followers"},
42+
[]string{"company"},
43+
[]string{"person", "gravatar", "avatars"},
44+
}
45+
46+
jsonparser.EachKey(mediumFixture, func(idx int, value []byte, vt jsonparser.ValueType, err error){
47+
switch idx {
48+
case 0:
49+
// jsonparser.ParseString(value)
50+
case 1:
51+
jsonparser.ParseInt(value)
52+
case 2:
53+
// jsonparser.ParseString(value)
54+
case 3:
55+
jsonparser.ArrayEach(value, func(avalue []byte, dataType jsonparser.ValueType, offset int, err error) {
56+
jsonparser.Get(avalue, "url")
57+
})
58+
}
59+
}, paths...)
60+
}
61+
}
62+
3763
/*
3864
encoding/json
3965
*/

benchmark/benchmark_small_payload_test.go

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,60 @@ func BenchmarkJsonParserSmall(b *testing.B) {
3535
}
3636
}
3737

38+
func BenchmarkJsonParserEachKeyManualSmall(b *testing.B) {
39+
for i := 0; i < b.N; i++ {
40+
paths := [][]string{
41+
[]string{"uuid"},
42+
[]string{"tz"},
43+
[]string{"ua"},
44+
[]string{"st"},
45+
}
46+
47+
jsonparser.EachKey(smallFixture, func(idx int, value []byte, vt jsonparser.ValueType, err error){
48+
switch idx {
49+
case 0:
50+
// jsonparser.ParseString(value)
51+
case 1:
52+
jsonparser.ParseInt(value)
53+
case 2:
54+
// jsonparser.ParseString(value)
55+
case 3:
56+
jsonparser.ParseInt(value)
57+
}
58+
}, paths...)
59+
}
60+
}
61+
62+
63+
func BenchmarkJsonParserEachKeyStructSmall(b *testing.B) {
64+
for i := 0; i < b.N; i++ {
65+
paths := [][]string{
66+
[]string{"uuid"},
67+
[]string{"tz"},
68+
[]string{"ua"},
69+
[]string{"st"},
70+
}
71+
var data SmallPayload
72+
73+
jsonparser.EachKey(smallFixture, func(idx int, value []byte, vt jsonparser.ValueType, err error){
74+
switch idx {
75+
case 0:
76+
data.Uuid, _ = jsonparser.ParseString(value)
77+
case 1:
78+
v, _ := jsonparser.ParseInt(value)
79+
data.Tz = int(v)
80+
case 2:
81+
data.Ua, _ = jsonparser.ParseString(value)
82+
case 3:
83+
v, _ := jsonparser.ParseInt(value)
84+
data.St = int(v)
85+
}
86+
}, paths...)
87+
88+
nothing(data.Uuid, data.Tz, data.Ua, data.St)
89+
}
90+
}
91+
3892
/*
3993
encoding/json
4094
*/

parser.go

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"bytes"
55
"errors"
66
"fmt"
7+
"math"
78
)
89

910
// Errors
@@ -178,6 +179,113 @@ func searchKeys(data []byte, keys ...string) int {
178179
return -1
179180
}
180181

182+
var bitwiseFlags []int64
183+
func init() {
184+
for i:=0; i<63; i++ {
185+
bitwiseFlags = append(bitwiseFlags, int64(math.Pow(2, float64(i))))
186+
}
187+
}
188+
189+
func EachKey(data []byte, cb func(int, []byte, ValueType, error), paths ...[]string) int {
190+
var pathFlags int64
191+
var level, pathsMatched, i int
192+
ln := len(data)
193+
194+
var stackbuf [unescapeStackBufSize]byte // stack-allocated array for allocation-free unescaping of small strings
195+
196+
for i < ln {
197+
switch data[i] {
198+
case '"':
199+
i++
200+
keyBegin := i
201+
202+
strEnd, keyEscaped := stringEnd(data[i:])
203+
if strEnd == -1 {
204+
return -1
205+
}
206+
i += strEnd
207+
208+
keyEnd := i - 1
209+
210+
valueOffset := nextToken(data[i:])
211+
if valueOffset == -1 {
212+
return -1
213+
}
214+
215+
i += valueOffset
216+
217+
218+
// if string is a key, and key level match
219+
if data[i] == ':' {
220+
match := false
221+
key := data[keyBegin:keyEnd]
222+
223+
// for unescape: if there are no escape sequences, this is cheap; if there are, it is a
224+
// bit more expensive, but causes no allocations unless len(key) > unescapeStackBufSize
225+
var keyUnesc []byte
226+
if !keyEscaped {
227+
keyUnesc = key
228+
} else if ku, err := Unescape(key, stackbuf[:]); err != nil {
229+
return -1
230+
} else {
231+
keyUnesc = ku
232+
}
233+
234+
for pi, p := range paths {
235+
if len(p) < level || (pathFlags & bitwiseFlags[pi]) != 0 {
236+
continue
237+
}
238+
239+
if equalStr(&keyUnesc, p[level-1]) {
240+
match = true
241+
242+
if len(p) == level {
243+
i++
244+
pathsMatched++
245+
pathFlags |= bitwiseFlags[pi]
246+
247+
v, dt, of, e := Get(data[i:])
248+
cb(pi, v, dt, e)
249+
250+
if of != -1 {
251+
i += of
252+
}
253+
254+
if pathsMatched == len(paths) {
255+
return i
256+
}
257+
}
258+
}
259+
}
260+
261+
if !match {
262+
tokenOffset := nextToken(data[i+1:])
263+
i += tokenOffset
264+
265+
if data[i] == '{' {
266+
blockSkip := blockEnd(data[i:], '{', '}')
267+
i += blockSkip + 1
268+
}
269+
}
270+
} else {
271+
i--
272+
}
273+
case '{':
274+
level++
275+
case '}':
276+
level--
277+
case '[':
278+
// Do not search for keys inside arrays
279+
arraySkip := blockEnd(data[i:], '[', ']')
280+
i += arraySkip - 1
281+
}
282+
283+
i++
284+
}
285+
286+
return -1
287+
}
288+
181289
// Data types available in valid JSON data.
182290
type ValueType int
183291

parser_test.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -617,6 +617,43 @@ func TestArrayEach(t *testing.T) {
617617
}, "a", "b")
618618
}
619619

620+
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}`)
621+
622+
func TestEachKey(t *testing.T) {
623+
paths := [][]string{
624+
[]string{"name"},
625+
[]string{"nested", "a"},
626+
[]string{"nested", "nested3", "b"},
627+
}
628+
629+
keysFound := 0
630+
631+
EachKey(testJson, func(idx int, value []byte, vt ValueType, err error){
632+
keysFound++
633+
634+
switch idx {
635+
case 0:
636+
if string(value) != "Name" {
637+
t.Errorf("Should find 1 key")
638+
}
639+
case 1:
640+
if string(value) != "test" {
641+
t.Errorf("Should find 2 key")
642+
}
643+
case 2:
644+
if string(value) != "4" {
645+
t.Errorf("Should find 3 key")
646+
}
647+
default:
648+
t.Errorf("Should found only 3 keys")
649+
}
650+
}, paths...)
651+
652+
if keysFound != 3 {
653+
t.Errorf("Should find 3 keys: %d", keysFound)
654+
}
655+
}
656+
620657
type ParseTest struct {
621658
in string
622659
intype ValueType

0 commit comments

Comments
 (0)