-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathvalue.go
355 lines (283 loc) · 8.19 KB
/
value.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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
package gen
import (
"bytes"
"strconv"
)
// Value 表示将要序列化到`json`字符串中的值
type Value interface {
// Serialize 将值序列化为字符串,追加到`buf`后,返回新的`buf`
Serialize(buf []byte) []byte
// Size 返回值在最终的`json`串中占有多少字节
Size() int
}
// QuotedValue 表示需要用"包裹起来的值,例如字符串
type QuotedValue string
// Serialize 将`q`序列化为字符串,追加到`buf`后,返回新的`buf`
func (q QuotedValue) Serialize(buf []byte) []byte {
buf = append(buf, '"')
buf = append(buf, []byte(q)...)
return append(buf, '"')
}
// Size 返回`q`在最终的`json`串中占有多少字节
func (q QuotedValue) Size() int {
return len(q) + 2
}
// UnquotedValue 表示不需要用"包裹起来的值,例如整数,浮点数等
type UnquotedValue string
// Serialize 将`u`序列化为字符串,追加到`buf`后,返回新的`buf`
func (u UnquotedValue) Serialize(buf []byte) []byte {
return append(buf, []byte(u)...)
}
// Size 返回`u`在最终的`json`串中占有多少字节
func (u UnquotedValue) Size() int {
return len(u)
}
// Array 表示一个`json`数组
type Array []Value
// NewArray 创建一个`json`数组,返回其指针
func NewArray() *Array {
a := Array(make([]Value, 0, 1))
return &a
}
// Serialize 将`a`序列化为字符串,追加到`buf`后,返回新的`buf`
func (a Array) Serialize(buf []byte) []byte {
if len(buf) == 0 {
buf = make([]byte, 0, a.Size())
}
buf = append(buf, '[')
count := len(a)
for i, e := range a {
buf = e.Serialize(buf)
if i != count-1 {
buf = append(buf, ',')
}
}
return append(buf, ']')
}
// Size 返回`a`在最终的`json`串中占有多少字节
func (a Array) Size() int {
size := 0
for _, e := range a {
size += e.Size()
}
// for []
size += 2
if len(a) > 1 {
// for ,
size += len(a) - 1
}
return size
}
// AppendUint 将`uint64`类型的值`u`追加到数组`a`后
func (a *Array) AppendUint(u uint64) {
value := strconv.FormatUint(u, 10)
*a = append(*a, UnquotedValue(value))
}
// AppendInt 将`int64`类型的值`i`追加到数组`a`后
func (a *Array) AppendInt(i int64) {
value := strconv.FormatInt(i, 10)
*a = append(*a, UnquotedValue(value))
}
// AppendFloat 将`float64`类型的值`f`追加到数组`a`后
func (a *Array) AppendFloat(f float64) {
value := strconv.FormatFloat(f, 'g', 10, 64)
*a = append(*a, UnquotedValue(value))
}
// AppendBool 将`bool`类型的值`b`追加到数组`a`后
func (a *Array) AppendBool(b bool) {
value := strconv.FormatBool(b)
*a = append(*a, UnquotedValue(value))
}
// AppendString 将`string`类型的值`s`追加到数组`a`后
func (a *Array) AppendString(value string) {
*a = append(*a, QuotedValue(escapeString(value)))
}
// AppendMap 将`Map`类型的值`m`追加到数组`a`后
func (a *Array) AppendMap(m *Map) {
*a = append(*a, m)
}
// AppendUintArray 将`uint64`数组`u`追加到数组`a`后
func (a *Array) AppendUintArray(u []uint64) {
value := make([]Value, 0, len(u))
for _, v := range u {
value = append(value, UnquotedValue(strconv.FormatUint(v, 10)))
}
*a = append(*a, Array(value))
}
// AppendIntArray 将`int64`数组`i`追加到数组`a`后
func (a *Array) AppendIntArray(i []int64) {
value := make([]Value, 0, len(i))
for _, v := range i {
value = append(value, UnquotedValue(strconv.FormatInt(v, 10)))
}
*a = append(*a, Array(value))
}
// AppendFloatArray 将`float64`数组`f`追加到数组`a`后
func (a *Array) AppendFloatArray(f []float64) {
value := make([]Value, 0, len(f))
for _, v := range f {
value = append(value, UnquotedValue(strconv.FormatFloat(v, 'g', 10, 64)))
}
*a = append(*a, Array(value))
}
// AppendBoolArray 将`bool`数组`b`追加到数组`a`后
func (a *Array) AppendBoolArray(b []bool) {
value := make([]Value, 0, len(b))
for _, v := range b {
value = append(value, UnquotedValue(strconv.FormatBool(v)))
}
*a = append(*a, Array(value))
}
// AppendStringArray 将`string`数组`s`追加到数组`a`后
func (a *Array) AppendStringArray(s []string) {
value := make([]Value, 0, len(s))
for _, v := range s {
value = append(value, QuotedValue(escapeString(v)))
}
*a = append(*a, Array(value))
}
// AppendMapArray 将`Map`数组`m`追加到数组`a`后
func (a *Array) AppendMapArray(m []Map) {
value := make([]Value, 0, len(m))
for _, v := range m {
value = append(value, v)
}
*a = append(*a, Array(value))
}
// AppendArray 将`json`数组`oa`追加到数组`a`后
func (a *Array) AppendArray(oa Array) {
*a = append(*a, oa)
}
// Map 表示一个`json`映射
type Map struct {
keys []string
values []Value
}
// Serialize 将`m`序列化为字符串,追加到`buf`后,返回新的`buf`
func (m Map) Serialize(buf []byte) []byte {
if len(buf) == 0 {
buf = make([]byte, 0, m.Size())
}
buf = append(buf, '{')
count := len(m.keys)
for i, key := range m.keys {
buf = append(buf, '"')
buf = append(buf, []byte(key)...)
buf = append(buf, '"')
buf = append(buf, ':')
buf = m.values[i].Serialize(buf)
if i != count-1 {
buf = append(buf, ',')
}
}
return append(buf, '}')
}
// Size 返回`m`在最终的`json`串中占有多少字节
func (m Map) Size() int {
size := 0
for i, key := range m.keys {
// +2 for ", +1 for :
size += len(key) + 2 + 1
size += m.values[i].Size()
}
// +2 for {}
size += 2
if len(m.keys) > 1 {
// for ,
size += len(m.keys) - 1
}
return size
}
func (m *Map) put(key string, value Value) {
m.keys = append(m.keys, key)
m.values = append(m.values, value)
}
// PutUint 将`uint64`类型的值`u`与键`key`关联
func (m *Map) PutUint(key string, u uint64) {
value := strconv.FormatUint(u, 10)
m.put(key, UnquotedValue(value))
}
// PutInt 将`int64`类型的值`i`与键`key`关联
func (m *Map) PutInt(key string, i int64) {
value := strconv.FormatInt(i, 10)
m.put(key, UnquotedValue(value))
}
// PutFloat 将`float64`类型的值`f`与键`key`关联
func (m *Map) PutFloat(key string, f float64) {
value := strconv.FormatFloat(f, 'g', 10, 64)
m.put(key, UnquotedValue(value))
}
// PutBool 将`bool`类型的值`b`与键`key`关联
func (m *Map) PutBool(key string, b bool) {
value := strconv.FormatBool(b)
m.put(key, UnquotedValue(value))
}
// PutString 将`string`类型的值`value`与键`key`关联
func (m *Map) PutString(key, value string) {
m.put(key, QuotedValue(escapeString(value)))
}
// PutUintArray 将`uint64`数组类型的值`u`与键`key`关联
func (m *Map) PutUintArray(key string, u []uint64) {
value := make([]Value, 0, len(u))
for _, v := range u {
value = append(value, UnquotedValue(strconv.FormatUint(v, 10)))
}
m.put(key, Array(value))
}
// PutIntArray 将`int64`数组类型的值`i`与键`key`关联
func (m *Map) PutIntArray(key string, i []int64) {
value := make([]Value, 0, len(i))
for _, v := range i {
value = append(value, UnquotedValue(strconv.FormatInt(v, 10)))
}
m.put(key, Array(value))
}
// PutFloatArray 将`float64`数组类型的值`f`与键`key`关联
func (m *Map) PutFloatArray(key string, f []float64) {
value := make([]Value, 0, len(f))
for _, v := range f {
value = append(value, UnquotedValue(strconv.FormatFloat(v, 'g', 10, 64)))
}
m.put(key, Array(value))
}
// PutBoolArray 将`bool`数组类型的值`b`与键`key`关联
func (m *Map) PutBoolArray(key string, b []bool) {
value := make([]Value, 0, len(b))
for _, v := range b {
value = append(value, UnquotedValue(strconv.FormatBool(v)))
}
m.put(key, Array(value))
}
// PutStringArray 将`string`数组类型的值`s`与键`key`关联
func (m *Map) PutStringArray(key string, s []string) {
value := make([]Value, 0, len(s))
for _, v := range s {
value = append(value, QuotedValue(escapeString(v)))
}
m.put(key, Array(value))
}
// PutArray 将`json`数组`a`与键`key`关联
func (m *Map) PutArray(key string, a *Array) {
m.put(key, a)
}
// PutMap 将`json`映射`om`与键`key`关联
func (m *Map) PutMap(key string, om *Map) {
m.put(key, om)
}
// NewMap 创建一个`json`映射返回其指针
func NewMap() *Map {
return &Map{
keys: make([]string, 0, 8),
values: make([]Value, 0, 8),
}
}
func escapeString(s string) string {
var buf bytes.Buffer
for _, r := range s {
if r == '\\' || r == '"' {
buf.WriteByte('\\')
}
buf.WriteRune(r)
}
return buf.String()
}