forked from francoispqt/gojay
-
Notifications
You must be signed in to change notification settings - Fork 1
/
encoding.go
309 lines (256 loc) · 6.39 KB
/
encoding.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
// Code generated by Gojay. DO NOT EDIT.
package embedded_struct
import (
"github.com/francoispqt/gojay"
"time"
)
type Ints []int
// UnmarshalJSONArray decodes JSON array elements into slice
func (a *Ints) UnmarshalJSONArray(dec *gojay.Decoder) error {
var value int
if err := dec.Int(&value); err != nil {
return err
}
*a = append(*a, value)
return nil
}
// MarshalJSONArray encodes arrays into JSON
func (a Ints) MarshalJSONArray(enc *gojay.Encoder) {
for _, item := range a {
enc.Int(item)
}
}
// IsNil checks if array is nil
func (a Ints) IsNil() bool {
return len(a) == 0
}
type Float64s []float64
// UnmarshalJSONArray decodes JSON array elements into slice
func (a *Float64s) UnmarshalJSONArray(dec *gojay.Decoder) error {
var value float64
if err := dec.Float64(&value); err != nil {
return err
}
*a = append(*a, value)
return nil
}
// MarshalJSONArray encodes arrays into JSON
func (a Float64s) MarshalJSONArray(enc *gojay.Encoder) {
for _, item := range a {
enc.Float64(item)
}
}
// IsNil checks if array is nil
func (a Float64s) IsNil() bool {
return len(a) == 0
}
type SubMessagesPtr []*SubMessage
func (s *SubMessagesPtr) UnmarshalJSONArray(dec *gojay.Decoder) error {
var value = &SubMessage{}
if err := dec.Object(value); err != nil {
return err
}
*s = append(*s, value)
return nil
}
func (s SubMessagesPtr) MarshalJSONArray(enc *gojay.Encoder) {
for i := range s {
enc.Object(s[i])
}
}
func (s SubMessagesPtr) IsNil() bool {
return len(s) == 0
}
type SubMessages []SubMessage
func (s *SubMessages) UnmarshalJSONArray(dec *gojay.Decoder) error {
var value = SubMessage{}
if err := dec.Object(&value); err != nil {
return err
}
*s = append(*s, value)
return nil
}
func (s SubMessages) MarshalJSONArray(enc *gojay.Encoder) {
for i := range s {
enc.Object(&s[i])
}
}
func (s SubMessages) IsNil() bool {
return len(s) == 0
}
// MarshalJSONObject implements MarshalerJSONObject
func (m *SubMessage) MarshalJSONObject(enc *gojay.Encoder) {
enc.StringKey("Description", m.Description)
enc.TimeKey("StartTime", &m.StartTime, time.RFC3339)
if m.EndTime != nil {
enc.TimeKey("EndTime", m.EndTime, time.RFC3339)
}
}
// IsNil checks if instance is nil
func (m *SubMessage) IsNil() bool {
return m == nil
}
// UnmarshalJSONObject implements gojay's UnmarshalerJSONObject
func (m *SubMessage) UnmarshalJSONObject(dec *gojay.Decoder, k string) error {
switch k {
case "Description":
return dec.String(&m.Description)
case "StartTime":
var format = time.RFC3339
var value = time.Time{}
err := dec.Time(&value, format)
if err == nil {
m.StartTime = value
}
return err
case "EndTime":
var format = time.RFC3339
var value = &time.Time{}
err := dec.Time(value, format)
if err == nil {
m.EndTime = value
}
return err
}
return nil
}
// NKeys returns the number of keys to unmarshal
func (m *SubMessage) NKeys() int { return 3 }
// MarshalJSONObject implements MarshalerJSONObject
func (m *Message) MarshalJSONObject(enc *gojay.Encoder) {
if m.BaseId != nil {
enc.IntKey("Id", m.Id)
enc.StringKey("Name", m.Name)
}
enc.StringKey("Description", m.Description)
enc.TimeKey("StartTime", &m.StartTime, time.RFC3339)
if m.EndTime != nil {
enc.TimeKey("EndTime", m.EndTime, time.RFC3339)
}
enc.Float64Key("Price", m.Price)
var intsSlice = Ints(m.Ints)
enc.ArrayKey("Ints", intsSlice)
var floatsSlice = Float64s(m.Floats)
enc.ArrayKey("Floats", floatsSlice)
enc.ObjectKey("SubMessageX", m.SubMessageX)
var messagesXSlice = SubMessagesPtr(m.MessagesX)
enc.ArrayKey("MessagesX", messagesXSlice)
enc.ObjectKey("SubMessageY", &m.SubMessageY)
var messagesYSlice = SubMessages(m.MessagesY)
enc.ArrayKey("MessagesY", messagesYSlice)
enc.BoolKey("IsTrue", *m.IsTrue)
var payloadSlice = gojay.EmbeddedJSON(m.Payload)
enc.AddEmbeddedJSONKey("Payload", &payloadSlice)
}
// IsNil checks if instance is nil
func (m *Message) IsNil() bool {
return m == nil
}
// UnmarshalJSONObject implements gojay's UnmarshalerJSONObject
func (m *Message) UnmarshalJSONObject(dec *gojay.Decoder, k string) error {
if m.BaseId == nil {
m.BaseId = &BaseId{}
}
switch k {
case "Id":
return dec.Int(&m.Id)
case "Name":
return dec.String(&m.Name)
case "Description":
return dec.String(&m.Description)
case "StartTime":
var format = time.RFC3339
var value = time.Time{}
err := dec.Time(&value, format)
if err == nil {
m.StartTime = value
}
return err
case "EndTime":
var format = time.RFC3339
var value = &time.Time{}
err := dec.Time(value, format)
if err == nil {
m.EndTime = value
}
return err
case "Price":
return dec.Float64(&m.Price)
case "Ints":
var aSlice = Ints{}
err := dec.Array(&aSlice)
if err == nil && len(aSlice) > 0 {
m.Ints = []int(aSlice)
}
return err
case "Floats":
var aSlice = Float64s{}
err := dec.Array(&aSlice)
if err == nil && len(aSlice) > 0 {
m.Floats = []float64(aSlice)
}
return err
case "SubMessageX":
var value = &SubMessage{}
err := dec.Object(value)
if err == nil {
m.SubMessageX = value
}
return err
case "MessagesX":
var aSlice = SubMessagesPtr{}
err := dec.Array(&aSlice)
if err == nil && len(aSlice) > 0 {
m.MessagesX = []*SubMessage(aSlice)
}
return err
case "SubMessageY":
err := dec.Object(&m.SubMessageY)
return err
case "MessagesY":
var aSlice = SubMessages{}
err := dec.Array(&aSlice)
if err == nil && len(aSlice) > 0 {
m.MessagesY = []SubMessage(aSlice)
}
return err
case "IsTrue":
var value bool
err := dec.Bool(&value)
if err == nil {
m.IsTrue = &value
}
return err
case "Payload":
var value = gojay.EmbeddedJSON{}
err := dec.AddEmbeddedJSON(&value)
if err == nil && len(value) > 0 {
m.Payload = []byte(value)
}
return err
}
return nil
}
// NKeys returns the number of keys to unmarshal
func (m *Message) NKeys() int { return 14 }
// MarshalJSONObject implements MarshalerJSONObject
func (i *BaseId) MarshalJSONObject(enc *gojay.Encoder) {
enc.IntKey("Id", i.Id)
enc.StringKey("Name", i.Name)
}
// IsNil checks if instance is nil
func (i *BaseId) IsNil() bool {
return i == nil
}
// UnmarshalJSONObject implements gojay's UnmarshalerJSONObject
func (i *BaseId) UnmarshalJSONObject(dec *gojay.Decoder, k string) error {
switch k {
case "Id":
return dec.Int(&i.Id)
case "Name":
return dec.String(&i.Name)
}
return nil
}
// NKeys returns the number of keys to unmarshal
func (i *BaseId) NKeys() int { return 2 }