-
Notifications
You must be signed in to change notification settings - Fork 4
/
w.go
149 lines (126 loc) · 2.82 KB
/
w.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
package jx
import (
"bytes"
"io"
)
// Writer writes json tokens to underlying buffer.
//
// Zero value is valid.
type Writer struct {
Buf []byte // underlying buffer
stream *streamState
}
// Write implements io.Writer.
func (w *Writer) Write(p []byte) (n int, err error) {
if w.stream != nil {
return 0, errStreaming
}
w.Buf = append(w.Buf, p...)
return len(p), nil
}
// WriteTo implements io.WriterTo.
func (w *Writer) WriteTo(t io.Writer) (n int64, err error) {
if w.stream != nil {
return 0, errStreaming
}
wrote, err := t.Write(w.Buf)
return int64(wrote), err
}
// String returns string of underlying buffer.
func (w Writer) String() string {
w.stream.mustNotBeStreaming()
return string(w.Buf)
}
// Reset resets underlying buffer.
//
// If w is in streaming mode, it is reset to non-streaming mode.
func (w *Writer) Reset() {
w.Buf = w.Buf[:0]
w.stream = nil
}
// ResetWriter resets underlying buffer and sets output writer.
func (w *Writer) ResetWriter(out io.Writer) {
w.Buf = w.Buf[:0]
if w.stream == nil {
w.stream = newStreamState(out)
}
w.stream.Reset(out)
}
// Grow grows the underlying buffer.
//
// Calls (*bytes.Buffer).Grow(n int) on w.Buf.
func (w *Writer) Grow(n int) {
buf := bytes.NewBuffer(w.Buf)
buf.Grow(n)
w.Buf = buf.Bytes()
}
// byte writes a single byte.
func (w *Writer) byte(c byte) (fail bool) {
if w.stream == nil {
w.Buf = append(w.Buf, c)
return false
}
return writeStreamBytes(w, c)
}
func (w *Writer) twoBytes(c1, c2 byte) bool {
if w.stream == nil {
w.Buf = append(w.Buf, c1, c2)
return false
}
return writeStreamBytes(w, c1, c2)
}
// RawStr writes string as raw json.
func (w *Writer) RawStr(v string) bool {
return w.rawStr(v)
}
func (w *Writer) rawStr(v string) bool {
return writeStreamByteseq(w, v)
}
// Raw writes byte slice as raw json.
func (w *Writer) Raw(b []byte) bool {
return writeStreamByteseq(w, b)
}
// Null writes null.
func (w *Writer) Null() bool {
return writeStreamByteseq(w, "null")
}
// True writes true.
func (w *Writer) True() bool {
return writeStreamByteseq(w, "true")
}
// False writes false.
func (w *Writer) False() bool {
return writeStreamByteseq(w, "false")
}
// Bool encodes boolean.
func (w *Writer) Bool(v bool) bool {
if v {
return w.True()
}
return w.False()
}
// ObjStart writes object start.
func (w *Writer) ObjStart() bool {
return w.byte('{')
}
// FieldStart encodes field name and writes colon.
func (w *Writer) FieldStart(field string) bool {
return w.Str(field) ||
w.byte(':')
}
// ObjEnd writes end of object token.
func (w *Writer) ObjEnd() bool {
return w.byte('}')
}
// ArrStart writes start of array.
func (w *Writer) ArrStart() bool {
return w.byte('[')
}
// ArrEnd writes end of array.
func (w *Writer) ArrEnd() bool {
return w.byte(']')
}
// Comma writes comma.
func (w *Writer) Comma() bool {
return w.byte(',')
}