forked from microsoft/kiota-serialization-json-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
json_serialization_writer.go
698 lines (645 loc) · 18.1 KB
/
json_serialization_writer.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
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
package jsonserialization
import (
"encoding/base64"
"fmt"
"strconv"
"strings"
"time"
"github.com/google/uuid"
absser "github.com/microsoft/kiota-abstractions-go/serialization"
)
// JsonSerializationWriter implements SerializationWriter for JSON.
type JsonSerializationWriter struct {
writer []string
}
// NewJsonSerializationWriter creates a new instance of the JsonSerializationWriter.
func NewJsonSerializationWriter() *JsonSerializationWriter {
return &JsonSerializationWriter{
writer: make([]string, 0),
}
}
func (w *JsonSerializationWriter) writeRawValue(value string) {
w.writer = append(w.writer, value)
}
func (w *JsonSerializationWriter) writeStringValue(value string) {
value = strings.ReplaceAll(
strings.ReplaceAll(
strings.ReplaceAll(value,
"\\", "\\\\",
),
"\"",
"\\\""),
"\n",
"\\n")
value = strings.ReplaceAll(strings.ReplaceAll(value, "\t", "\\t"), "\r", "\\r")
w.writeRawValue("\"" + value + "\"")
}
func (w *JsonSerializationWriter) writePropertyName(key string) {
w.writeRawValue("\"" + key + "\":")
}
func (w *JsonSerializationWriter) writePropertySeparator() {
w.writeRawValue(",")
}
func (w *JsonSerializationWriter) trimLastPropertySeparator() {
for idx, s := range w.writer {
writerLen := len(w.writer)
if s == "," && (idx == writerLen-1 || (idx < writerLen-1 && (w.writer[idx+1] == "]" || w.writer[idx+1] == "}" || w.writer[idx+1] == ","))) {
w.writer[idx] = ""
}
}
}
func (w *JsonSerializationWriter) writeArrayStart() {
w.writeRawValue("[")
}
func (w *JsonSerializationWriter) writeArrayEnd() {
w.writeRawValue("]")
}
func (w *JsonSerializationWriter) writeObjectStart() {
w.writeRawValue("{")
}
func (w *JsonSerializationWriter) writeObjectEnd() {
w.writeRawValue("}")
}
// WriteStringValue writes a String value to underlying the byte array.
func (w *JsonSerializationWriter) WriteStringValue(key string, value *string) error {
if key != "" && value != nil {
w.writePropertyName(key)
}
if value != nil {
w.writeStringValue(*value)
}
if key != "" && value != nil {
w.writePropertySeparator()
}
return nil
}
// WriteBoolValue writes a Bool value to underlying the byte array.
func (w *JsonSerializationWriter) WriteBoolValue(key string, value *bool) error {
if key != "" && value != nil {
w.writePropertyName(key)
}
if value != nil {
w.writeRawValue(strconv.FormatBool(*value))
}
if key != "" && value != nil {
w.writePropertySeparator()
}
return nil
}
// WriteByteValue writes a Byte value to underlying the byte array.
func (w *JsonSerializationWriter) WriteByteValue(key string, value *byte) error {
if value != nil {
cast := int64(*value)
return w.WriteInt64Value(key, &cast)
}
return nil
}
// WriteInt8Value writes a int8 value to underlying the byte array.
func (w *JsonSerializationWriter) WriteInt8Value(key string, value *int8) error {
if value != nil {
cast := int64(*value)
return w.WriteInt64Value(key, &cast)
}
return nil
}
// WriteInt32Value writes a Int32 value to underlying the byte array.
func (w *JsonSerializationWriter) WriteInt32Value(key string, value *int32) error {
if value != nil {
cast := int64(*value)
return w.WriteInt64Value(key, &cast)
}
return nil
}
// WriteInt64Value writes a Int64 value to underlying the byte array.
func (w *JsonSerializationWriter) WriteInt64Value(key string, value *int64) error {
if key != "" && value != nil {
w.writePropertyName(key)
}
if value != nil {
w.writeRawValue(strconv.FormatInt(*value, 10))
}
if key != "" && value != nil {
w.writePropertySeparator()
}
return nil
}
// WriteFloat32Value writes a Float32 value to underlying the byte array.
func (w *JsonSerializationWriter) WriteFloat32Value(key string, value *float32) error {
if value != nil {
cast := float64(*value)
return w.WriteFloat64Value(key, &cast)
}
return nil
}
// WriteFloat64Value writes a Float64 value to underlying the byte array.
func (w *JsonSerializationWriter) WriteFloat64Value(key string, value *float64) error {
if key != "" && value != nil {
w.writePropertyName(key)
}
if value != nil {
w.writeRawValue(strconv.FormatFloat(*value, 'f', -1, 64))
}
if key != "" && value != nil {
w.writePropertySeparator()
}
return nil
}
// WriteTimeValue writes a Time value to underlying the byte array.
func (w *JsonSerializationWriter) WriteTimeValue(key string, value *time.Time) error {
if key != "" && value != nil {
w.writePropertyName(key)
}
if value != nil {
w.writeStringValue((*value).Format(time.RFC3339))
}
if key != "" && value != nil {
w.writePropertySeparator()
}
return nil
}
// WriteISODurationValue writes a ISODuration value to underlying the byte array.
func (w *JsonSerializationWriter) WriteISODurationValue(key string, value *absser.ISODuration) error {
if key != "" && value != nil {
w.writePropertyName(key)
}
if value != nil {
w.writeStringValue((*value).String())
}
if key != "" && value != nil {
w.writePropertySeparator()
}
return nil
}
// WriteTimeOnlyValue writes a TimeOnly value to underlying the byte array.
func (w *JsonSerializationWriter) WriteTimeOnlyValue(key string, value *absser.TimeOnly) error {
if key != "" && value != nil {
w.writePropertyName(key)
}
if value != nil {
w.writeStringValue((*value).String())
}
if key != "" && value != nil {
w.writePropertySeparator()
}
return nil
}
// WriteDateOnlyValue writes a DateOnly value to underlying the byte array.
func (w *JsonSerializationWriter) WriteDateOnlyValue(key string, value *absser.DateOnly) error {
if key != "" && value != nil {
w.writePropertyName(key)
}
if value != nil {
w.writeStringValue((*value).String())
}
if key != "" && value != nil {
w.writePropertySeparator()
}
return nil
}
// WriteUUIDValue writes a UUID value to underlying the byte array.
func (w *JsonSerializationWriter) WriteUUIDValue(key string, value *uuid.UUID) error {
if key != "" && value != nil {
w.writePropertyName(key)
}
if value != nil {
w.writeStringValue((*value).String())
}
if key != "" && value != nil {
w.writePropertySeparator()
}
return nil
}
// WriteByteArrayValue writes a ByteArray value to underlying the byte array.
func (w *JsonSerializationWriter) WriteByteArrayValue(key string, value []byte) error {
if key != "" && value != nil {
w.writePropertyName(key)
}
if value != nil {
w.writeStringValue(base64.StdEncoding.EncodeToString(value))
}
if key != "" && value != nil {
w.writePropertySeparator()
}
return nil
}
// WriteObjectValue writes a Parsable value to underlying the byte array.
func (w *JsonSerializationWriter) WriteObjectValue(key string, item absser.Parsable) error {
if item != nil {
if key != "" {
w.writePropertyName(key)
}
//TODO onBefore for backing store
w.writeObjectStart()
//TODO onStart for backing store
err := item.Serialize(w)
//TODO onAfter for backing store
if err != nil {
return err
}
w.writeObjectEnd()
if key != "" {
w.writePropertySeparator()
}
}
return nil
}
// WriteCollectionOfObjectValues writes a collection of Parsable values to underlying the byte array.
func (w *JsonSerializationWriter) WriteCollectionOfObjectValues(key string, collection []absser.Parsable) error {
if collection != nil { // empty collections are meaningful
if key != "" {
w.writePropertyName(key)
}
w.writeArrayStart()
for _, item := range collection {
err := w.WriteObjectValue("", item)
if err != nil {
return err
}
w.writePropertySeparator()
}
w.writeArrayEnd()
if key != "" {
w.writePropertySeparator()
}
}
return nil
}
// WriteCollectionOfStringValues writes a collection of String values to underlying the byte array.
func (w *JsonSerializationWriter) WriteCollectionOfStringValues(key string, collection []string) error {
if collection != nil { // empty collections are meaningful
if key != "" {
w.writePropertyName(key)
}
w.writeArrayStart()
for _, item := range collection {
err := w.WriteStringValue("", &item)
if err != nil {
return err
}
w.writePropertySeparator()
}
w.writeArrayEnd()
if key != "" {
w.writePropertySeparator()
}
}
return nil
}
// WriteCollectionOfInt32Values writes a collection of Int32 values to underlying the byte array.
func (w *JsonSerializationWriter) WriteCollectionOfInt32Values(key string, collection []int32) error {
if collection != nil { // empty collections are meaningful
if key != "" {
w.writePropertyName(key)
}
w.writeArrayStart()
for _, item := range collection {
err := w.WriteInt32Value("", &item)
if err != nil {
return err
}
w.writePropertySeparator()
}
w.writeArrayEnd()
if key != "" {
w.writePropertySeparator()
}
}
return nil
}
// WriteCollectionOfInt64Values writes a collection of Int64 values to underlying the byte array.
func (w *JsonSerializationWriter) WriteCollectionOfInt64Values(key string, collection []int64) error {
if collection != nil { // empty collections are meaningful
if key != "" {
w.writePropertyName(key)
}
w.writeArrayStart()
for _, item := range collection {
err := w.WriteInt64Value("", &item)
if err != nil {
return err
}
w.writePropertySeparator()
}
w.writeArrayEnd()
if key != "" {
w.writePropertySeparator()
}
}
return nil
}
// WriteCollectionOfFloat32Values writes a collection of Float32 values to underlying the byte array.
func (w *JsonSerializationWriter) WriteCollectionOfFloat32Values(key string, collection []float32) error {
if collection != nil { // empty collections are meaningful
if key != "" {
w.writePropertyName(key)
}
w.writeArrayStart()
for _, item := range collection {
err := w.WriteFloat32Value("", &item)
if err != nil {
return err
}
w.writePropertySeparator()
}
w.writeArrayEnd()
if key != "" {
w.writePropertySeparator()
}
}
return nil
}
// WriteCollectionOfFloat64Values writes a collection of Float64 values to underlying the byte array.
func (w *JsonSerializationWriter) WriteCollectionOfFloat64Values(key string, collection []float64) error {
if collection != nil { // empty collections are meaningful
if key != "" {
w.writePropertyName(key)
}
w.writeArrayStart()
for _, item := range collection {
err := w.WriteFloat64Value("", &item)
if err != nil {
return err
}
w.writePropertySeparator()
}
w.writeArrayEnd()
if key != "" {
w.writePropertySeparator()
}
}
return nil
}
// WriteCollectionOfTimeValues writes a collection of Time values to underlying the byte array.
func (w *JsonSerializationWriter) WriteCollectionOfTimeValues(key string, collection []time.Time) error {
if collection != nil { // empty collections are meaningful
if key != "" {
w.writePropertyName(key)
}
w.writeArrayStart()
for _, item := range collection {
err := w.WriteTimeValue("", &item)
if err != nil {
return err
}
w.writePropertySeparator()
}
w.writeArrayEnd()
if key != "" {
w.writePropertySeparator()
}
}
return nil
}
// WriteCollectionOfISODurationValues writes a collection of ISODuration values to underlying the byte array.
func (w *JsonSerializationWriter) WriteCollectionOfISODurationValues(key string, collection []absser.ISODuration) error {
if collection != nil { // empty collections are meaningful
if key != "" {
w.writePropertyName(key)
}
w.writeArrayStart()
for _, item := range collection {
err := w.WriteISODurationValue("", &item)
if err != nil {
return err
}
w.writePropertySeparator()
}
w.writeArrayEnd()
if key != "" {
w.writePropertySeparator()
}
}
return nil
}
// WriteCollectionOfTimeOnlyValues writes a collection of TimeOnly values to underlying the byte array.
func (w *JsonSerializationWriter) WriteCollectionOfTimeOnlyValues(key string, collection []absser.TimeOnly) error {
if collection != nil { // empty collections are meaningful
if key != "" {
w.writePropertyName(key)
}
w.writeArrayStart()
for _, item := range collection {
err := w.WriteTimeOnlyValue("", &item)
if err != nil {
return err
}
w.writePropertySeparator()
}
w.writeArrayEnd()
if key != "" {
w.writePropertySeparator()
}
}
return nil
}
// WriteCollectionOfDateOnlyValues writes a collection of DateOnly values to underlying the byte array.
func (w *JsonSerializationWriter) WriteCollectionOfDateOnlyValues(key string, collection []absser.DateOnly) error {
if collection != nil { // empty collections are meaningful
if key != "" {
w.writePropertyName(key)
}
w.writeArrayStart()
for _, item := range collection {
err := w.WriteDateOnlyValue("", &item)
if err != nil {
return err
}
w.writePropertySeparator()
}
w.writeArrayEnd()
if key != "" {
w.writePropertySeparator()
}
}
return nil
}
// WriteCollectionOfUUIDValues writes a collection of UUID values to underlying the byte array.
func (w *JsonSerializationWriter) WriteCollectionOfUUIDValues(key string, collection []uuid.UUID) error {
if collection != nil { // empty collections are meaningful
if key != "" {
w.writePropertyName(key)
}
w.writeArrayStart()
for _, item := range collection {
err := w.WriteUUIDValue("", &item)
if err != nil {
return err
}
w.writePropertySeparator()
}
w.writeArrayEnd()
if key != "" {
w.writePropertySeparator()
}
}
return nil
}
// WriteCollectionOfBoolValues writes a collection of Bool values to underlying the byte array.
func (w *JsonSerializationWriter) WriteCollectionOfBoolValues(key string, collection []bool) error {
if collection != nil { // empty collections are meaningful
if key != "" {
w.writePropertyName(key)
}
w.writeArrayStart()
for _, item := range collection {
err := w.WriteBoolValue("", &item)
if err != nil {
return err
}
w.writePropertySeparator()
}
w.writeArrayEnd()
if key != "" {
w.writePropertySeparator()
}
}
return nil
}
// WriteCollectionOfByteValues writes a collection of Byte values to underlying the byte array.
func (w *JsonSerializationWriter) WriteCollectionOfByteValues(key string, collection []byte) error {
if collection != nil { // empty collections are meaningful
if key != "" {
w.writePropertyName(key)
}
w.writeArrayStart()
for _, item := range collection {
err := w.WriteByteValue("", &item)
if err != nil {
return err
}
w.writePropertySeparator()
}
w.writeArrayEnd()
if key != "" {
w.writePropertySeparator()
}
}
return nil
}
// WriteCollectionOfInt8Values writes a collection of int8 values to underlying the byte array.
func (w *JsonSerializationWriter) WriteCollectionOfInt8Values(key string, collection []int8) error {
if collection != nil { // empty collections are meaningful
if key != "" {
w.writePropertyName(key)
}
w.writeArrayStart()
for _, item := range collection {
err := w.WriteInt8Value("", &item)
if err != nil {
return err
}
w.writePropertySeparator()
}
w.writeArrayEnd()
if key != "" {
w.writePropertySeparator()
}
}
return nil
}
// GetSerializedContent returns the resulting byte array from the serialization writer.
func (w *JsonSerializationWriter) GetSerializedContent() ([]byte, error) {
w.trimLastPropertySeparator()
resultStr := strings.Join(w.writer, "")
return []byte(resultStr), nil
}
// WriteAdditionalData writes additional data to underlying the byte array.
func (w *JsonSerializationWriter) WriteAdditionalData(value map[string]interface{}) error {
var err error
if len(value) != 0 {
for key, input := range value {
switch value := input.(type) {
case absser.Parsable:
err = w.WriteObjectValue(key, value)
case []absser.Parsable:
err = w.WriteCollectionOfObjectValues(key, value)
case []string:
err = w.WriteCollectionOfStringValues(key, value)
case []bool:
err = w.WriteCollectionOfBoolValues(key, value)
case []byte:
err = w.WriteCollectionOfByteValues(key, value)
case []int8:
err = w.WriteCollectionOfInt8Values(key, value)
case []int32:
err = w.WriteCollectionOfInt32Values(key, value)
case []int64:
err = w.WriteCollectionOfInt64Values(key, value)
case []float32:
err = w.WriteCollectionOfFloat32Values(key, value)
case []float64:
err = w.WriteCollectionOfFloat64Values(key, value)
case []uuid.UUID:
err = w.WriteCollectionOfUUIDValues(key, value)
case []time.Time:
err = w.WriteCollectionOfTimeValues(key, value)
case []absser.ISODuration:
err = w.WriteCollectionOfISODurationValues(key, value)
case []absser.TimeOnly:
err = w.WriteCollectionOfTimeOnlyValues(key, value)
case []absser.DateOnly:
err = w.WriteCollectionOfDateOnlyValues(key, value)
case *string:
err = w.WriteStringValue(key, value)
case string:
err = w.WriteStringValue(key, &value)
case *bool:
err = w.WriteBoolValue(key, value)
case bool:
err = w.WriteBoolValue(key, &value)
case *byte:
err = w.WriteByteValue(key, value)
case byte:
err = w.WriteByteValue(key, &value)
case *int8:
err = w.WriteInt8Value(key, value)
case int8:
err = w.WriteInt8Value(key, &value)
case *int32:
err = w.WriteInt32Value(key, value)
case int32:
err = w.WriteInt32Value(key, &value)
case *int64:
err = w.WriteInt64Value(key, value)
case int64:
err = w.WriteInt64Value(key, &value)
case *float32:
err = w.WriteFloat32Value(key, value)
case float32:
err = w.WriteFloat32Value(key, &value)
case *float64:
err = w.WriteFloat64Value(key, value)
case float64:
err = w.WriteFloat64Value(key, &value)
case *uuid.UUID:
err = w.WriteUUIDValue(key, value)
case uuid.UUID:
err = w.WriteUUIDValue(key, &value)
case *time.Time:
err = w.WriteTimeValue(key, value)
case time.Time:
err = w.WriteTimeValue(key, &value)
case *absser.ISODuration:
err = w.WriteISODurationValue(key, value)
case absser.ISODuration:
err = w.WriteISODurationValue(key, &value)
case *absser.TimeOnly:
err = w.WriteTimeOnlyValue(key, value)
case absser.TimeOnly:
err = w.WriteTimeOnlyValue(key, &value)
case *absser.DateOnly:
err = w.WriteDateOnlyValue(key, value)
case absser.DateOnly:
err = w.WriteDateOnlyValue(key, &value)
default:
return fmt.Errorf("unsupported AdditionalData type: %T", value)
}
}
}
return err
}
// Close clears the internal buffer.
func (w *JsonSerializationWriter) Close() error {
w.writer = make([]string, 0)
return nil
}