Repository navigation
Expand file tree
/
Copy pathencoding_json.go
More file actions
110 lines (95 loc) · 2.17 KB
/
Copy pathencoding_json.go
File metadata and controls
110 lines (95 loc) · 2.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
package benchmarks
import (
"bytes"
"encoding/json"
"fmt"
)
type jsonBencher struct {
twitterDoc twitterDoc
}
func (x *jsonBencher) name() string {
return "encoding_json"
}
func (x *jsonBencher) readFloat64(data []byte) (val float64, err error) {
tkn, err := json.NewDecoder(bytes.NewReader(data)).Token()
if err != nil {
return 0, err
}
var ok bool
val, ok = tkn.(float64)
if ok {
return val, nil
}
return 0, fmt.Errorf("not a number")
}
func (x *jsonBencher) readInt64(data []byte) (val int64, err error) {
decoder := json.NewDecoder(bytes.NewReader(data))
decoder.UseNumber()
tkn, err := decoder.Token()
if err != nil {
return 0, err
}
num, ok := tkn.(json.Number)
if ok {
return num.Int64()
}
return 0, fmt.Errorf("not a number")
}
func (x *jsonBencher) decodeInt64(data []byte, v *int64) error {
return json.Unmarshal(data, &v)
}
func (x *jsonBencher) readObject(data []byte) (val map[string]any, err error) {
err = json.Unmarshal(data, &val)
if err != nil {
return nil, err
}
return val, nil
}
func (x *jsonBencher) valid(data []byte) bool {
return json.Valid(data)
}
func (x *jsonBencher) readRepoData(data []byte, val *repoData) error {
return json.Unmarshal(data, &val)
}
func (x *jsonBencher) readString(data []byte) (string, error) {
tkn, err := json.NewDecoder(bytes.NewReader(data)).Token()
if err != nil {
return "", err
}
s, ok := tkn.(string)
if ok {
return s, nil
}
return "", fmt.Errorf("not a string")
}
func (x *jsonBencher) readBool(data []byte) (bool, error) {
tkn, err := json.NewDecoder(bytes.NewReader(data)).Token()
if err != nil {
return false, err
}
b, ok := tkn.(bool)
if ok {
return b, nil
}
return false, fmt.Errorf("not a bool")
}
type twitterUser struct {
ID int64 `json:"id"`
}
type twitterStatus struct {
User twitterUser `json:"user"`
}
type twitterDoc struct {
Statuses []twitterStatus `json:"statuses"`
}
func (x *jsonBencher) distinctUserIDs(data []byte, dest []int64) ([]int64, error) {
err := json.Unmarshal(data, &x.twitterDoc)
if err != nil {
return nil, err
}
result := dest[:0]
for _, status := range x.twitterDoc.Statuses {
result = append(result, status.User.ID)
}
return result, nil
}