-
Notifications
You must be signed in to change notification settings - Fork 114
/
encode_array.go
212 lines (196 loc) · 5.65 KB
/
encode_array.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
package gojay
// EncodeArray encodes an implementation of MarshalerJSONArray to JSON
func (enc *Encoder) EncodeArray(v MarshalerJSONArray) error {
if enc.isPooled == 1 {
panic(InvalidUsagePooledEncoderError("Invalid usage of pooled encoder"))
}
_, _ = enc.encodeArray(v)
_, err := enc.Write()
if err != nil {
enc.err = err
return err
}
return nil
}
func (enc *Encoder) encodeArray(v MarshalerJSONArray) ([]byte, error) {
enc.grow(200)
enc.writeByte('[')
v.MarshalJSONArray(enc)
enc.writeByte(']')
return enc.buf, enc.err
}
// AddArray adds an implementation of MarshalerJSONArray to be encoded, must be used inside a slice or array encoding (does not encode a key)
// value must implement Marshaler
func (enc *Encoder) AddArray(v MarshalerJSONArray) {
enc.Array(v)
}
// AddArrayOmitEmpty adds an array or slice to be encoded, must be used inside a slice or array encoding (does not encode a key)
// value must implement MarshalerAddArrayOmitEmpty
func (enc *Encoder) AddArrayOmitEmpty(v MarshalerJSONArray) {
enc.ArrayOmitEmpty(v)
}
// AddArrayNullEmpty adds an array or slice to be encoded, must be used inside a slice or array encoding (does not encode a key)
// value must implement Marshaler, if v is empty, `null` will be encoded`
func (enc *Encoder) AddArrayNullEmpty(v MarshalerJSONArray) {
enc.ArrayNullEmpty(v)
}
// AddArrayKey adds an array or slice to be encoded, must be used inside an object as it will encode a key
// value must implement Marshaler
func (enc *Encoder) AddArrayKey(key string, v MarshalerJSONArray) {
enc.ArrayKey(key, v)
}
// AddArrayKeyOmitEmpty adds an array or slice to be encoded and skips it if it is nil.
// Must be called inside an object as it will encode a key.
func (enc *Encoder) AddArrayKeyOmitEmpty(key string, v MarshalerJSONArray) {
enc.ArrayKeyOmitEmpty(key, v)
}
// AddArrayKeyNullEmpty adds an array or slice to be encoded and skips it if it is nil.
// Must be called inside an object as it will encode a key. `null` will be encoded`
func (enc *Encoder) AddArrayKeyNullEmpty(key string, v MarshalerJSONArray) {
enc.ArrayKeyNullEmpty(key, v)
}
// Array adds an implementation of MarshalerJSONArray to be encoded, must be used inside a slice or array encoding (does not encode a key)
// value must implement Marshaler
func (enc *Encoder) Array(v MarshalerJSONArray) {
if v.IsNil() {
enc.grow(3)
r := enc.getPreviousRune()
if r != '[' {
enc.writeByte(',')
}
enc.writeByte('[')
enc.writeByte(']')
return
}
enc.grow(100)
r := enc.getPreviousRune()
if r != '[' {
enc.writeByte(',')
}
enc.writeByte('[')
v.MarshalJSONArray(enc)
enc.writeByte(']')
}
// ArrayOmitEmpty adds an array or slice to be encoded, must be used inside a slice or array encoding (does not encode a key)
// value must implement Marshaler
func (enc *Encoder) ArrayOmitEmpty(v MarshalerJSONArray) {
if v.IsNil() {
return
}
enc.grow(4)
r := enc.getPreviousRune()
if r != '[' {
enc.writeByte(',')
}
enc.writeByte('[')
v.MarshalJSONArray(enc)
enc.writeByte(']')
}
// ArrayNullEmpty adds an array or slice to be encoded, must be used inside a slice or array encoding (does not encode a key)
// value must implement Marshaler
func (enc *Encoder) ArrayNullEmpty(v MarshalerJSONArray) {
enc.grow(4)
r := enc.getPreviousRune()
if r != '[' {
enc.writeByte(',')
}
if v.IsNil() {
enc.writeBytes(nullBytes)
return
}
enc.writeByte('[')
v.MarshalJSONArray(enc)
enc.writeByte(']')
}
// ArrayKey adds an array or slice to be encoded, must be used inside an object as it will encode a key
// value must implement Marshaler
func (enc *Encoder) ArrayKey(key string, v MarshalerJSONArray) {
if enc.hasKeys {
if !enc.keyExists(key) {
return
}
}
if v.IsNil() {
enc.grow(2 + len(key))
r := enc.getPreviousRune()
if r != '{' {
enc.writeByte(',')
}
enc.writeByte('"')
enc.writeStringEscape(key)
enc.writeBytes(objKeyArr)
enc.writeByte(']')
return
}
enc.grow(5 + len(key))
r := enc.getPreviousRune()
if r != '{' {
enc.writeByte(',')
}
enc.writeByte('"')
enc.writeStringEscape(key)
enc.writeBytes(objKeyArr)
v.MarshalJSONArray(enc)
enc.writeByte(']')
}
// ArrayKeyOmitEmpty adds an array or slice to be encoded and skips if it is nil.
// Must be called inside an object as it will encode a key.
func (enc *Encoder) ArrayKeyOmitEmpty(key string, v MarshalerJSONArray) {
if enc.hasKeys {
if !enc.keyExists(key) {
return
}
}
if v.IsNil() {
return
}
enc.grow(5 + len(key))
r := enc.getPreviousRune()
if r != '{' {
enc.writeByte(',')
}
enc.writeByte('"')
enc.writeStringEscape(key)
enc.writeBytes(objKeyArr)
v.MarshalJSONArray(enc)
enc.writeByte(']')
}
// ArrayKeyNullEmpty adds an array or slice to be encoded and encodes `null`` if it is nil.
// Must be called inside an object as it will encode a key.
func (enc *Encoder) ArrayKeyNullEmpty(key string, v MarshalerJSONArray) {
if enc.hasKeys {
if !enc.keyExists(key) {
return
}
}
enc.grow(5 + len(key))
r := enc.getPreviousRune()
if r != '{' {
enc.writeByte(',')
}
if v.IsNil() {
enc.writeBytes(nullBytes)
return
}
enc.writeByte('"')
enc.writeStringEscape(key)
enc.writeBytes(objKeyArr)
v.MarshalJSONArray(enc)
enc.writeByte(']')
}
// EncodeArrayFunc is a custom func type implementing MarshaleArray.
// Use it to cast a func(*Encoder) to Marshal an object.
//
// enc := gojay.NewEncoder(io.Writer)
// enc.EncodeArray(gojay.EncodeArrayFunc(func(enc *gojay.Encoder) {
// enc.AddStringKey("hello", "world")
// }))
type EncodeArrayFunc func(*Encoder)
// MarshalJSONArray implements MarshalerJSONArray.
func (f EncodeArrayFunc) MarshalJSONArray(enc *Encoder) {
f(enc)
}
// IsNil implements MarshalerJSONArray.
func (f EncodeArrayFunc) IsNil() bool {
return f == nil
}