-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathjson_test.go
142 lines (125 loc) · 2.88 KB
/
json_test.go
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
package marshalhash
import (
"bytes"
"encoding/json"
"reflect"
"testing"
)
func TestCopyJSON(t *testing.T) {
var buf bytes.Buffer
enc := NewWriter(&buf)
enc.WriteMapHeader(5)
enc.WriteString("thing_1")
enc.WriteString("a string object")
enc.WriteString("a_map")
enc.WriteMapHeader(2)
enc.WriteString("float_a")
enc.WriteFloat32(1.0)
enc.WriteString("int_b")
enc.WriteInt64(-100)
enc.WriteString("some bytes")
enc.WriteBytes([]byte("here are some bytes"))
enc.WriteString("a bool")
enc.WriteBool(true)
enc.WriteString("a map")
enc.WriteMapStrStr(map[string]string{
"internal_one": "blah",
"internal_two": "blahhh...",
})
enc.Flush()
var js bytes.Buffer
_, err := CopyToJSON(&js, &buf)
if err != nil {
t.Fatal(err)
}
mp := make(map[string]interface{})
err = json.Unmarshal(js.Bytes(), &mp)
if err != nil {
t.Log(js.String())
t.Fatalf("Error unmarshaling: %s", err)
}
if len(mp) != 5 {
t.Errorf("map length should be %d, not %d", 4, len(mp))
}
so, ok := mp["thing_1"]
if !ok || so != "a string object" {
t.Errorf("expected %q; got %q", "a string object", so)
}
in, ok := mp["a map"]
if !ok {
t.Error("no key 'a map'")
}
if inm, ok := in.(map[string]interface{}); !ok {
t.Error("inner map not type-assertable to map[string]interface{}")
} else {
inm1, ok := inm["internal_one"]
if !ok || !reflect.DeepEqual(inm1, "blah") {
t.Errorf("inner map field %q should be %q, not %q", "internal_one", "blah", inm1)
}
}
}
func BenchmarkCopyToJSON(b *testing.B) {
var buf bytes.Buffer
enc := NewWriter(&buf)
enc.WriteMapHeader(4)
enc.WriteString("thing_1")
enc.WriteString("a string object")
enc.WriteString("a_first_map")
enc.WriteMapHeader(2)
enc.WriteString("float_a")
enc.WriteFloat32(1.0)
enc.WriteString("int_b")
enc.WriteInt64(-100)
enc.WriteString("an array")
enc.WriteArrayHeader(2)
enc.WriteBool(true)
enc.WriteUint(2089)
enc.WriteString("a_second_map")
enc.WriteMapStrStr(map[string]string{
"internal_one": "blah",
"internal_two": "blahhh...",
})
enc.Flush()
var js bytes.Buffer
bts := buf.Bytes()
_, err := CopyToJSON(&js, &buf)
if err != nil {
b.Fatal(err)
}
b.SetBytes(int64(len(js.Bytes())))
b.ResetTimer()
b.ReportAllocs()
for i := 0; i < b.N; i++ {
js.Reset()
CopyToJSON(&js, bytes.NewReader(bts))
}
}
func BenchmarkStdlibJSON(b *testing.B) {
obj := map[string]interface{}{
"thing_1": "a string object",
"a_first_map": map[string]interface{}{
"float_a": float32(1.0),
"float_b": -100,
},
"an array": []interface{}{
"part_A",
"part_B",
},
"a_second_map": map[string]interface{}{
"internal_one": "blah",
"internal_two": "blahhh...",
},
}
var js bytes.Buffer
err := json.NewEncoder(&js).Encode(&obj)
if err != nil {
b.Fatal(err)
}
b.SetBytes(int64(len(js.Bytes())))
b.ResetTimer()
b.ReportAllocs()
for i := 0; i < b.N; i++ {
js.Reset()
json.NewEncoder(&js).Encode(&obj)
}
}