-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjson_builder.go
49 lines (38 loc) · 884 Bytes
/
json_builder.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
package jsonpb
import "github.com/vizee/jsonpb/jsonlit"
type JsonBuilder struct {
buf []byte
}
func UnsafeJsonBuilder(buf []byte) *JsonBuilder {
return &JsonBuilder{buf: buf}
}
func (b *JsonBuilder) Len() int {
return len(b.buf)
}
func (b *JsonBuilder) Reserve(n int) {
if cap(b.buf)-len(b.buf) < n {
newbuf := make([]byte, len(b.buf), cap(b.buf)+n)
copy(newbuf, b.buf)
b.buf = newbuf
}
}
func (b *JsonBuilder) String() string {
return asString(b.buf)
}
func (b *JsonBuilder) IntoBytes() []byte {
buf := b.buf
b.buf = nil
return buf
}
func (b *JsonBuilder) AppendBytes(s ...byte) {
b.buf = append(b.buf, s...)
}
func (b *JsonBuilder) AppendString(s string) {
b.buf = append(b.buf, s...)
}
func (b *JsonBuilder) AppendByte(c byte) {
b.buf = append(b.buf, c)
}
func (b *JsonBuilder) AppendEscapedString(s string) {
b.buf = jsonlit.EscapeString(b.buf, s)
}