-
Notifications
You must be signed in to change notification settings - Fork 35
/
Copy pathJsonSerializationWriter.cs
648 lines (607 loc) · 25.9 KB
/
JsonSerializationWriter.cs
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
// ------------------------------------------------------------------------------
// Copyright (c) Microsoft Corporation. All Rights Reserved. Licensed under the MIT License. See License in the project root for license information.
// ------------------------------------------------------------------------------
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Reflection;
using System.Text;
using System.Text.Json;
using System.Xml;
using Microsoft.Kiota.Abstractions;
using Microsoft.Kiota.Abstractions.Helpers;
using Microsoft.Kiota.Abstractions.Serialization;
#if NET5_0_OR_GREATER
using System.Diagnostics.CodeAnalysis;
#endif
namespace Microsoft.Kiota.Serialization.Json
{
/// <summary>
/// The <see cref="ISerializationWriter"/> implementation for json content types.
/// </summary>
public class JsonSerializationWriter : ISerializationWriter, IDisposable
{
private readonly MemoryStream _stream = new MemoryStream();
private readonly KiotaJsonSerializationContext _kiotaJsonSerializationContext;
/// <summary>
/// The <see cref="Utf8JsonWriter"/> instance for writing json content
/// </summary>
public readonly Utf8JsonWriter writer;
/// <summary>
/// The <see cref="JsonSerializationWriter"/> constructor
/// </summary>
public JsonSerializationWriter()
: this(KiotaJsonSerializationContext.Default)
{
}
/// <summary>
/// The <see cref="JsonSerializationWriter"/> constructor
/// </summary>
/// <param name="kiotaJsonSerializationContext">The KiotaJsonSerializationContext to use.</param>
public JsonSerializationWriter(KiotaJsonSerializationContext kiotaJsonSerializationContext)
{
_kiotaJsonSerializationContext = kiotaJsonSerializationContext;
writer = new Utf8JsonWriter(_stream, new JsonWriterOptions
{
Encoder = kiotaJsonSerializationContext.Options.Encoder,
Indented = kiotaJsonSerializationContext.Options.WriteIndented
});
}
/// <summary>
/// The action to perform before object serialization
/// </summary>
public Action<IParsable>? OnBeforeObjectSerialization { get; set; }
/// <summary>
/// The action to perform after object serialization
/// </summary>
public Action<IParsable>? OnAfterObjectSerialization { get; set; }
/// <summary>
/// The action to perform on the start of object serialization
/// </summary>
public Action<IParsable, ISerializationWriter>? OnStartObjectSerialization { get; set; }
/// <summary>
/// Get the stream of the serialized content
/// </summary>
/// <returns>The <see cref="Stream"/> of the serialized content</returns>
public Stream GetSerializedContent()
{
writer.Flush();
_stream.Position = 0;
return _stream;
}
/// <summary>
/// Write the string value
/// </summary>
/// <param name="key">The key of the json node</param>
/// <param name="value">The string value</param>
public void WriteStringValue(string? key, string? value)
{
if(value != null)
{
// we want to keep empty string because they are meaningful
if(!string.IsNullOrEmpty(key))
writer.WritePropertyName(key!);
JsonSerializer.Serialize(writer, value, TypeConstants.StringType, _kiotaJsonSerializationContext);
}
}
/// <summary>
/// Write the boolean value
/// </summary>
/// <param name="key">The key of the json node</param>
/// <param name="value">The boolean value</param>
public void WriteBoolValue(string? key, bool? value)
{
if(!string.IsNullOrEmpty(key) && value.HasValue)
writer.WritePropertyName(key!);
if(value.HasValue)
JsonSerializer.Serialize(writer, value.Value, TypeConstants.BooleanType, _kiotaJsonSerializationContext);
}
/// <summary>
/// Write the byte value
/// </summary>
/// <param name="key">The key of the json node</param>
/// <param name="value">The byte value</param>
public void WriteByteValue(string? key, byte? value)
{
if(!string.IsNullOrEmpty(key) && value.HasValue)
writer.WritePropertyName(key!);
if(value.HasValue)
JsonSerializer.Serialize(writer, value.Value, TypeConstants.ByteType, _kiotaJsonSerializationContext);
}
/// <summary>
/// Write the sbyte value
/// </summary>
/// <param name="key">The key of the json node</param>
/// <param name="value">The sbyte value</param>
public void WriteSbyteValue(string? key, sbyte? value)
{
if(!string.IsNullOrEmpty(key) && value.HasValue)
writer.WritePropertyName(key!);
if(value.HasValue)
JsonSerializer.Serialize(writer, value.Value, TypeConstants.SbyteType, _kiotaJsonSerializationContext);
}
/// <summary>
/// Write the int value
/// </summary>
/// <param name="key">The key of the json node</param>
/// <param name="value">The int value</param>
public void WriteIntValue(string? key, int? value)
{
if(!string.IsNullOrEmpty(key) && value.HasValue)
writer.WritePropertyName(key!);
if(value.HasValue)
JsonSerializer.Serialize(writer, value, TypeConstants.IntType, _kiotaJsonSerializationContext);
}
/// <summary>
/// Write the float value
/// </summary>
/// <param name="key">The key of the json node</param>
/// <param name="value">The float value</param>
public void WriteFloatValue(string? key, float? value)
{
if(!string.IsNullOrEmpty(key) && value.HasValue)
writer.WritePropertyName(key!);
if(value.HasValue)
JsonSerializer.Serialize(writer, value.Value, TypeConstants.FloatType, _kiotaJsonSerializationContext);
}
/// <summary>
/// Write the long value
/// </summary>
/// <param name="key">The key of the json node</param>
/// <param name="value">The long value</param>
public void WriteLongValue(string? key, long? value)
{
if(!string.IsNullOrEmpty(key) && value.HasValue)
writer.WritePropertyName(key!);
if(value.HasValue)
JsonSerializer.Serialize(writer, value.Value, TypeConstants.LongType, _kiotaJsonSerializationContext);
}
/// <summary>
/// Write the double value
/// </summary>
/// <param name="key">The key of the json node</param>
/// <param name="value">The double value</param>
public void WriteDoubleValue(string? key, double? value)
{
if(!string.IsNullOrEmpty(key) && value.HasValue)
writer.WritePropertyName(key!);
if(value.HasValue)
JsonSerializer.Serialize(writer, value.Value, TypeConstants.DoubleType, _kiotaJsonSerializationContext);
}
/// <summary>
/// Write the decimal value
/// </summary>
/// <param name="key">The key of the json node</param>
/// <param name="value">The decimal value</param>
public void WriteDecimalValue(string? key, decimal? value)
{
if(!string.IsNullOrEmpty(key) && value.HasValue)
writer.WritePropertyName(key!);
if(value.HasValue)
JsonSerializer.Serialize(writer, value.Value, TypeConstants.DecimalType, _kiotaJsonSerializationContext);
}
/// <summary>
/// Write the Guid value
/// </summary>
/// <param name="key">The key of the json node</param>
/// <param name="value">The Guid value</param>
public void WriteGuidValue(string? key, Guid? value)
{
if(!string.IsNullOrEmpty(key) && value.HasValue)
writer.WritePropertyName(key!);
if(value.HasValue)
JsonSerializer.Serialize(writer, value.Value, TypeConstants.GuidType, _kiotaJsonSerializationContext);
}
/// <summary>
/// Write the DateTimeOffset value
/// </summary>
/// <param name="key">The key of the json node</param>
/// <param name="value">The DateTimeOffset value</param>
public void WriteDateTimeOffsetValue(string? key, DateTimeOffset? value)
{
if(!string.IsNullOrEmpty(key) && value.HasValue)
writer.WritePropertyName(key!);
if(value.HasValue)
JsonSerializer.Serialize(writer, value.Value, TypeConstants.DateTimeOffsetType, _kiotaJsonSerializationContext);
}
/// <summary>
/// Write the TimeSpan(An ISO8601 duration.For example, PT1M is "period time of 1 minute") value.
/// </summary>
/// <param name="key">The key of the json node</param>
/// <param name="value">The TimeSpan value</param>
public void WriteTimeSpanValue(string? key, TimeSpan? value)
{
if(value.HasValue)
WriteStringValue(key, XmlConvert.ToString(value.Value));
}
/// <summary>
/// Write the Date value
/// </summary>
/// <param name="key">The key of the json node</param>
/// <param name="value">The Date value</param>
public void WriteDateValue(string? key, Date? value)
=> WriteStringValue(key, value?.ToString());
/// <summary>
/// Write the Time value
/// </summary>
/// <param name="key">The key of the json node</param>
/// <param name="value">The Time value</param>
public void WriteTimeValue(string? key, Time? value)
=> WriteStringValue(key, value?.ToString());
/// <summary>
/// Write the null value
/// </summary>
/// <param name="key">The key of the json node</param>
public void WriteNullValue(string? key)
{
if(!string.IsNullOrEmpty(key))
writer.WritePropertyName(key!);
writer.WriteNullValue();
}
/// <summary>
/// Write the enumeration value of type <typeparam name="T"/>
/// </summary>
/// <param name="key">The key of the json node</param>
/// <param name="value">The enumeration value</param>
#if NET5_0_OR_GREATER
public void WriteEnumValue<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicFields)] T>(string? key, T? value) where T : struct, Enum
#else
public void WriteEnumValue<T>(string? key, T? value) where T : struct, Enum
#endif
{
if(!string.IsNullOrEmpty(key) && value.HasValue)
writer.WritePropertyName(key!);
if(value.HasValue)
{
if(typeof(T).IsDefined(typeof(FlagsAttribute)))
{
var values =
#if NET5_0_OR_GREATER
Enum.GetValues<T>();
#else
(T[])Enum.GetValues(typeof(T));
#endif
StringBuilder valueNames = new StringBuilder();
foreach(var x in values)
{
if(value.Value.HasFlag(x) && EnumHelpers.GetEnumStringValue(x) is string valueName)
{
if(valueNames.Length > 0)
valueNames.Append(",");
valueNames.Append(valueName);
}
}
WriteStringValue(null, valueNames.ToString());
}
else WriteStringValue(null, EnumHelpers.GetEnumStringValue(value.Value));
}
}
/// <summary>
/// Write the collection of primitives of type <typeparam name="T"/>
/// </summary>
/// <param name="key">The key of the json node</param>
/// <param name="values">The primitive collection</param>
public void WriteCollectionOfPrimitiveValues<T>(string? key, IEnumerable<T>? values)
{
if(values != null)
{ //empty array is meaningful
if(!string.IsNullOrEmpty(key))
writer.WritePropertyName(key!);
writer.WriteStartArray();
foreach(var collectionValue in values)
WriteAnyValue(null, collectionValue);
writer.WriteEndArray();
}
}
/// <summary>
/// Write the collection of objects of type <typeparam name="T"/>
/// </summary>
/// <param name="key">The key of the json node</param>
/// <param name="values">The object collection</param>
public void WriteCollectionOfObjectValues<T>(string? key, IEnumerable<T>? values) where T : IParsable
{
if(values != null)
{
// empty array is meaningful
if(!string.IsNullOrEmpty(key))
writer.WritePropertyName(key!);
writer.WriteStartArray();
foreach(var item in values)
WriteObjectValue<T>(null, item);
writer.WriteEndArray();
}
}
/// <summary>
/// Writes the specified collection of enum values to the stream with an optional given key.
/// </summary>
/// <param name="key">The key to be used for the written value. May be null.</param>
/// <param name="values">The enum values to be written.</param>
#if NET5_0_OR_GREATER
public void WriteCollectionOfEnumValues<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicFields)] T>(string? key, IEnumerable<T?>? values) where T : struct, Enum
#else
public void WriteCollectionOfEnumValues<T>(string? key, IEnumerable<T?>? values) where T : struct, Enum
#endif
{
if(values != null)
{ //empty array is meaningful
if(!string.IsNullOrEmpty(key))
writer.WritePropertyName(key!);
writer.WriteStartArray();
foreach(var item in values)
WriteEnumValue<T>(null, item);
writer.WriteEndArray();
}
}
/// <summary>
/// Writes the specified dictionary to the stream with an optional given key.
/// </summary>
/// <param name="key">The key to be used for the written value. May be null.</param>
/// <param name="values">The dictionary of values to be written.</param>
#if NET5_0_OR_GREATER
private void WriteDictionaryValue<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicFields)] T>(string? key, T values) where T : IDictionary
#else
private void WriteDictionaryValue<T>(string? key, T values) where T : IDictionary
#endif
{
if(values != null)
{
if(!string.IsNullOrEmpty(key))
writer.WritePropertyName(key!);
writer.WriteStartObject();
foreach(DictionaryEntry entry in values)
{
if(entry.Key is not string keyStr)
throw new InvalidOperationException($"Error serializing dictionary value with key {key}, only string keyed dictionaries are supported.");
WriteAnyValue(keyStr, entry.Value);
}
writer.WriteEndObject();
}
}
/// <summary>
/// Writes the specified byte array as a base64 string to the stream with an optional given key.
/// </summary>
/// <param name="key">The key to be used for the written value. May be null.</param>
/// <param name="value">The byte array to be written.</param>
public void WriteByteArrayValue(string? key, byte[]? value)
{
//empty array is meaningful
if(value != null)
{
if(string.IsNullOrEmpty(key))
writer.WriteBase64StringValue(value);
else
writer.WriteBase64String(key!, value);
}
}
/// <summary>
/// Write the object of type <typeparam name="T"/>
/// </summary>
/// <param name="key">The key of the json node</param>
/// <param name="value">The object instance to write</param>
/// <param name="additionalValuesToMerge">The additional values to merge to the main value when serializing an intersection wrapper.</param>
public void WriteObjectValue<T>(string? key, T? value, params IParsable?[] additionalValuesToMerge) where T : IParsable
{
var filteredAdditionalValuesToMerge = (IParsable[])Array.FindAll(additionalValuesToMerge, static x => x is not null);
if(value != null || filteredAdditionalValuesToMerge.Length > 0)
{
// until interface exposes WriteUntypedValue()
var serializingUntypedNode = value is UntypedNode;
if(!serializingUntypedNode && !string.IsNullOrEmpty(key))
writer.WritePropertyName(key!);
if(value != null)
OnBeforeObjectSerialization?.Invoke(value);
if(serializingUntypedNode)
{
var untypedNode = value as UntypedNode;
OnStartObjectSerialization?.Invoke(untypedNode!, this);
WriteUntypedValue(key, untypedNode);
OnAfterObjectSerialization?.Invoke(untypedNode!);
}
else
{
var serializingScalarValue = value is IComposedTypeWrapper;
if(!serializingScalarValue)
writer.WriteStartObject();
if(value != null)
{
OnStartObjectSerialization?.Invoke(value, this);
value.Serialize(this);
}
foreach(var additionalValueToMerge in filteredAdditionalValuesToMerge)
{
OnBeforeObjectSerialization?.Invoke(additionalValueToMerge!);
OnStartObjectSerialization?.Invoke(additionalValueToMerge!, this);
additionalValueToMerge!.Serialize(this);
OnAfterObjectSerialization?.Invoke(additionalValueToMerge);
}
if(!serializingScalarValue)
writer.WriteEndObject();
}
if(value != null) OnAfterObjectSerialization?.Invoke(value);
}
}
/// <summary>
/// Write the additional data property bag
/// </summary>
/// <param name="value">The additional data dictionary</param>
public void WriteAdditionalData(IDictionary<string, object> value)
{
if(value == null)
return;
foreach(var dataValue in value)
WriteAnyValue(dataValue.Key, dataValue.Value);
}
#if NET5_0_OR_GREATER
private void WriteNonParsableObjectValue<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicProperties)] T>(string? key, T value)
#else
private void WriteNonParsableObjectValue<T>(string? key, T value)
#endif
{
if(!string.IsNullOrEmpty(key))
writer.WritePropertyName(key!);
writer.WriteStartObject();
if(value == null)
writer.WriteNullValue();
else
foreach(var oProp in value.GetType().GetProperties())
WriteAnyValue(oProp.Name, oProp.GetValue(value));
writer.WriteEndObject();
}
private void WriteAnyValue<T>(string? key, T value)
{
switch(value)
{
case string s:
WriteStringValue(key, s);
break;
case bool b:
WriteBoolValue(key, b);
break;
case byte b:
WriteByteValue(key, b);
break;
case sbyte b:
WriteSbyteValue(key, b);
break;
case int i:
WriteIntValue(key, i);
break;
case float f:
WriteFloatValue(key, f);
break;
case long l:
WriteLongValue(key, l);
break;
case double d:
WriteDoubleValue(key, d);
break;
case decimal dec:
WriteDecimalValue(key, dec);
break;
case Guid g:
WriteGuidValue(key, g);
break;
case DateTimeOffset dto:
WriteDateTimeOffsetValue(key, dto);
break;
case TimeSpan timeSpan:
WriteTimeSpanValue(key, timeSpan);
break;
case IEnumerable<object> coll:
WriteCollectionOfPrimitiveValues(key, coll);
break;
case UntypedNode node:
WriteUntypedValue(key, node);
break;
case IParsable parseable:
WriteObjectValue(key, parseable);
break;
case Date date:
WriteDateValue(key, date);
break;
case DateTime dateTime:
WriteDateTimeOffsetValue(key, new DateTimeOffset(dateTime));
break;
case Time time:
WriteTimeValue(key, time);
break;
case JsonElement jsonElement:
if(!string.IsNullOrEmpty(key))
writer.WritePropertyName(key!);
jsonElement.WriteTo(writer);
break;
case IDictionary dictionary:
WriteDictionaryValue(key, dictionary);
break;
case object o:
WriteNonParsableObjectValue(key, o);
break;
case null:
WriteNullValue(key);
break;
default:
throw new InvalidOperationException($"Error serializing additional data value with key {key}, unknown type {value?.GetType()}");
}
}
/// <inheritdoc />
public void Dispose()
{
writer.Dispose();
GC.SuppressFinalize(this);
}
/// <summary>
/// Writes a untyped value for the specified key.
/// </summary>
/// <param name="key">The key to be used for the written value. May be null.</param>
/// <param name="value">The untyped node.</param>
private void WriteUntypedValue(string? key, UntypedNode? value)
{
switch(value)
{
case UntypedString untypedString:
WriteStringValue(key, untypedString.GetValue());
break;
case UntypedBoolean untypedBoolean:
WriteBoolValue(key, untypedBoolean.GetValue());
break;
case UntypedInteger untypedInteger:
WriteIntValue(key, untypedInteger.GetValue());
break;
case UntypedLong untypedLong:
WriteLongValue(key, untypedLong.GetValue());
break;
case UntypedDecimal untypedDecimal:
WriteDecimalValue(key, untypedDecimal.GetValue());
break;
case UntypedFloat untypedFloat:
WriteFloatValue(key, untypedFloat.GetValue());
break;
case UntypedDouble untypedDouble:
WriteDoubleValue(key, untypedDouble.GetValue());
break;
case UntypedObject untypedObject:
WriteUntypedObject(key, untypedObject);
break;
case UntypedArray array:
WriteUntypedArray(key, array);
break;
case UntypedNull:
WriteNullValue(key);
break;
}
}
/// <summary>
/// Write a untyped object for the specified key.
/// </summary>
/// <param name="key">The key to be used for the written value. May be null.</param>
/// <param name="value">The untyped object.</param>
private void WriteUntypedObject(string? key, UntypedObject? value)
{
if(value != null)
{
if(!string.IsNullOrEmpty(key)) writer.WritePropertyName(key!);
writer.WriteStartObject();
foreach(var item in value.GetValue())
WriteUntypedValue(item.Key, item.Value);
writer.WriteEndObject();
}
}
/// <summary>
/// Writes the specified collection of untyped values.
/// </summary>
/// <param name="key">The key to be used for the written value. May be null.</param>
/// <param name="array">The collection of untyped values.</param>
private void WriteUntypedArray(string? key, UntypedArray? array)
{
if(array != null)
{
if(!string.IsNullOrEmpty(key)) writer.WritePropertyName(key!);
writer.WriteStartArray();
foreach(var item in array.GetValue())
WriteUntypedValue(null, item);
writer.WriteEndArray();
}
}
}
}