This repository has been archived by the owner on Jul 9, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathJsonParseNode.cs
388 lines (358 loc) · 17.4 KB
/
JsonParseNode.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
// ------------------------------------------------------------------------------
// 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.Linq;
using System.Collections.Generic;
using System.Diagnostics;
using System.Reflection;
using System.Text.Json;
using Microsoft.Kiota.Abstractions.Serialization;
using Microsoft.Kiota.Abstractions;
using System.Xml;
using Microsoft.Kiota.Abstractions.Extensions;
namespace Microsoft.Kiota.Serialization.Json
{
/// <summary>
/// The <see cref="IParseNode"/> implementation for the json content type
/// </summary>
public class JsonParseNode : IParseNode
{
private readonly JsonElement _jsonNode;
/// <summary>
/// The <see cref="JsonParseNode"/> constructor.
/// </summary>
/// <param name="node">The JsonElement to initialize the node with</param>
public JsonParseNode(JsonElement node)
{
_jsonNode = node;
}
/// <summary>
/// Get the string value from the json node
/// </summary>
/// <returns>A string value</returns>
public string? GetStringValue() => _jsonNode.ValueKind == JsonValueKind.String ? _jsonNode.GetString() : null;
/// <summary>
/// Get the boolean value from the json node
/// </summary>
/// <returns>A boolean value</returns>
public bool? GetBoolValue() => _jsonNode.ValueKind == JsonValueKind.True || _jsonNode.ValueKind == JsonValueKind.False ? _jsonNode.GetBoolean() : null;
/// <summary>
/// Get the byte value from the json node
/// </summary>
/// <returns>A byte value</returns>
public byte? GetByteValue() => _jsonNode.ValueKind == JsonValueKind.Number ? _jsonNode.GetByte() : null;
/// <summary>
/// Get the sbyte value from the json node
/// </summary>
/// <returns>A sbyte value</returns>
public sbyte? GetSbyteValue() => _jsonNode.ValueKind == JsonValueKind.Number ? _jsonNode.GetSByte() : null;
/// <summary>
/// Get the int value from the json node
/// </summary>
/// <returns>A int value</returns>
public int? GetIntValue() => _jsonNode.ValueKind == JsonValueKind.Number ? _jsonNode.GetInt32() : null;
/// <summary>
/// Get the float value from the json node
/// </summary>
/// <returns>A float value</returns>
public float? GetFloatValue() => _jsonNode.ValueKind == JsonValueKind.Number ? _jsonNode.GetSingle() : null;
/// <summary>
/// Get the Long value from the json node
/// </summary>
/// <returns>A Long value</returns>
public long? GetLongValue() => _jsonNode.ValueKind == JsonValueKind.Number ? _jsonNode.GetInt64() : null;
/// <summary>
/// Get the double value from the json node
/// </summary>
/// <returns>A double value</returns>
public double? GetDoubleValue() => _jsonNode.ValueKind == JsonValueKind.Number ? _jsonNode.GetDouble() : null;
/// <summary>
/// Get the decimal value from the json node
/// </summary>
/// <returns>A decimal value</returns>
public decimal? GetDecimalValue() => _jsonNode.ValueKind == JsonValueKind.Number ? _jsonNode.GetDecimal() : null;
/// <summary>
/// Get the guid value from the json node
/// </summary>
/// <returns>A guid value</returns>
public Guid? GetGuidValue() => _jsonNode.ValueKind == JsonValueKind.String ? _jsonNode.GetGuid() : null;
/// <summary>
/// Get the <see cref="DateTimeOffset"/> value from the json node
/// </summary>
/// <returns>A <see cref="DateTimeOffset"/> value</returns>
public DateTimeOffset? GetDateTimeOffsetValue()
{
// JsonElement.GetDateTimeOffset is super strict so try to be more lenient if it fails(e.g. when we have whitespace or other variant formats).
// ref - https://docs.microsoft.com/en-us/dotnet/standard/datetime/system-text-json-support
if(!_jsonNode.TryGetDateTimeOffset(out var value))
value = DateTimeOffset.Parse(_jsonNode.GetString()!);
return value;
}
/// <summary>
/// Get the <see cref="TimeSpan"/> value from the json node
/// </summary>
/// <returns>A <see cref="TimeSpan"/> value</returns>
public TimeSpan? GetTimeSpanValue()
{
var jsonString = _jsonNode.GetString();
if(string.IsNullOrEmpty(jsonString))
return null;
// Parse an ISO8601 duration.http://en.wikipedia.org/wiki/ISO_8601#Durations to a TimeSpan
return XmlConvert.ToTimeSpan(jsonString);
}
/// <summary>
/// Get the <see cref="Date"/> value from the json node
/// </summary>
/// <returns>A <see cref="Date"/> value</returns>
public Date? GetDateValue()
{
var dateString = _jsonNode.GetString();
if(!DateTime.TryParse(dateString,out var result))
return null;
return new Date(result);
}
/// <summary>
/// Get the <see cref="Time"/> value from the json node
/// </summary>
/// <returns>A <see cref="Time"/> value</returns>
public Time? GetTimeValue()
{
var dateString = _jsonNode.GetString();
if(!DateTime.TryParse(dateString,out var result))
return null;
return new Time(result);
}
/// <summary>
/// Get the enumeration value of type <typeparam name="T"/>from the json node
/// </summary>
/// <returns>An enumeration value or null</returns>
public T? GetEnumValue<T>() where T : struct, Enum
{
var rawValue = _jsonNode.GetString();
if(string.IsNullOrEmpty(rawValue)) return null;
if(typeof(T).GetCustomAttributes<FlagsAttribute>().Any())
{
return (T)(object)rawValue!
.Split(',')
.Select(x => Enum.TryParse<T>(x, true, out var result) ? result : (T?)null)
.Where(x => !x.Equals(null))
.Select(x => (int)(object)x!)
.Sum();
}
else
return Enum.TryParse<T>(rawValue, true,out var result) ? result : null;
}
/// <summary>
/// Get the collection of type <typeparam name="T"/>from the json node
/// </summary>
/// <param name="factory">The factory to use to create the model object.</param>
/// <returns>A collection of objects</returns>
public IEnumerable<T> GetCollectionOfObjectValues<T>(ParsableFactory<T> factory) where T : IParsable
{
if (_jsonNode.ValueKind == JsonValueKind.Array) {
var enumerator = _jsonNode.EnumerateArray();
while(enumerator.MoveNext())
{
var currentParseNode = new JsonParseNode(enumerator.Current)
{
OnAfterAssignFieldValues = OnAfterAssignFieldValues,
OnBeforeAssignFieldValues = OnBeforeAssignFieldValues
};
yield return currentParseNode.GetObjectValue<T>(factory);
}
}
}
/// <summary>
/// Gets the collection of enum values of the node.
/// </summary>
/// <returns>The collection of enum values.</returns>
public IEnumerable<T?> GetCollectionOfEnumValues<T>() where T : struct, Enum
{
if (_jsonNode.ValueKind == JsonValueKind.Array) {
var enumerator = _jsonNode.EnumerateArray();
while(enumerator.MoveNext())
{
var currentParseNode = new JsonParseNode(enumerator.Current)
{
OnAfterAssignFieldValues = OnAfterAssignFieldValues,
OnBeforeAssignFieldValues = OnBeforeAssignFieldValues
};
yield return currentParseNode.GetEnumValue<T>();
}
}
}
/// <summary>
/// Gets the byte array value of the node.
/// </summary>
/// <returns>The byte array value of the node.</returns>
public byte[]? GetByteArrayValue() {
var rawValue = _jsonNode.GetString();
if(string.IsNullOrEmpty(rawValue)) return null;
return Convert.FromBase64String(rawValue);
}
private static readonly Type booleanType = typeof(bool?);
private static readonly Type byteType = typeof(byte?);
private static readonly Type sbyteType = typeof(sbyte?);
private static readonly Type stringType = typeof(string);
private static readonly Type intType = typeof(int?);
private static readonly Type floatType = typeof(float?);
private static readonly Type doubleType = typeof(double?);
private static readonly Type guidType = typeof(Guid?);
private static readonly Type dateTimeOffsetType = typeof(DateTimeOffset?);
private static readonly Type timeSpanType = typeof(TimeSpan?);
private static readonly Type dateType = typeof(Date?);
private static readonly Type timeType = typeof(Time?);
/// <summary>
/// Get the collection of primitives of type <typeparam name="T"/>from the json node
/// </summary>
/// <returns>A collection of objects</returns>
public IEnumerable<T> GetCollectionOfPrimitiveValues<T>()
{
if (_jsonNode.ValueKind == JsonValueKind.Array) {
var genericType = typeof(T);
foreach(var collectionValue in _jsonNode.EnumerateArray())
{
var currentParseNode = new JsonParseNode(collectionValue)
{
OnBeforeAssignFieldValues = OnBeforeAssignFieldValues,
OnAfterAssignFieldValues = OnAfterAssignFieldValues
};
if(genericType == booleanType)
yield return (T)(object)currentParseNode.GetBoolValue()!;
else if(genericType == byteType)
yield return (T)(object)currentParseNode.GetByteValue()!;
else if(genericType == sbyteType)
yield return (T)(object)currentParseNode.GetSbyteValue()!;
else if(genericType == stringType)
yield return (T)(object)currentParseNode.GetStringValue()!;
else if(genericType == intType)
yield return (T)(object)currentParseNode.GetIntValue()!;
else if(genericType == floatType)
yield return (T)(object)currentParseNode.GetFloatValue()!;
else if(genericType == doubleType)
yield return (T)(object)currentParseNode.GetDoubleValue()!;
else if(genericType == guidType)
yield return (T)(object)currentParseNode.GetGuidValue()!;
else if(genericType == dateTimeOffsetType)
yield return (T)(object)currentParseNode.GetDateTimeOffsetValue()!;
else if(genericType == timeSpanType)
yield return (T)(object)currentParseNode.GetTimeSpanValue()!;
else if(genericType == dateType)
yield return (T)(object)currentParseNode.GetDateValue()!;
else if(genericType == timeType)
yield return (T)(object)currentParseNode.GetTimeValue()!;
else
throw new InvalidOperationException($"unknown type for deserialization {genericType.FullName}");
}
}
}
/// <summary>
/// The action to perform before assigning field values.
/// </summary>
public Action<IParsable>? OnBeforeAssignFieldValues { get; set; }
/// <summary>
/// The action to perform after assigning field values.
/// </summary>
public Action<IParsable>? OnAfterAssignFieldValues { get; set; }
/// <summary>
/// Get the object of type <typeparam name="T"/>from the json node
/// </summary>
/// <param name="factory">The factory to use to create the model object.</param>
/// <returns>A object of the specified type</returns>
public T GetObjectValue<T>(ParsableFactory<T> factory) where T : IParsable
{
var item = factory(this);
OnBeforeAssignFieldValues?.Invoke(item);
AssignFieldValues(item);
OnAfterAssignFieldValues?.Invoke(item);
return item;
}
private void AssignFieldValues<T>(T item) where T : IParsable
{
if(_jsonNode.ValueKind != JsonValueKind.Object) return;
IDictionary<string, object>? itemAdditionalData = null;
if(item is IAdditionalDataHolder holder)
{
holder.AdditionalData ??= new Dictionary<string, object>();
itemAdditionalData = holder.AdditionalData;
}
var fieldDeserializers = item.GetFieldDeserializers();
foreach(var fieldValue in _jsonNode.EnumerateObject())
{
if(fieldDeserializers.ContainsKey(fieldValue.Name))
{
if(fieldValue.Value.ValueKind == JsonValueKind.Null)
continue;// If the property is already null just continue. As calling functions like GetDouble,GetBoolValue do not process JsonValueKind.Null.
var fieldDeserializer = fieldDeserializers[fieldValue.Name];
Debug.WriteLine($"found property {fieldValue.Name} to deserialize");
fieldDeserializer.Invoke(new JsonParseNode(fieldValue.Value)
{
OnBeforeAssignFieldValues = OnBeforeAssignFieldValues,
OnAfterAssignFieldValues = OnAfterAssignFieldValues
});
}
else if (itemAdditionalData != null)
{
Debug.WriteLine($"found additional property {fieldValue.Name} to deserialize");
IDictionaryExtensions.TryAdd(itemAdditionalData, fieldValue.Name, TryGetAnything(fieldValue.Value)!);
}
else
{
Debug.WriteLine($"found additional property {fieldValue.Name} to deserialize but the model doesn't support additional data");
}
}
}
private static object? TryGetAnything(JsonElement element)
{
switch(element.ValueKind)
{
case JsonValueKind.Number:
if(element.TryGetDecimal(out var dec)) return dec;
else if(element.TryGetDouble(out var db)) return db;
else if(element.TryGetInt16(out var s)) return s;
else if(element.TryGetInt32(out var i)) return i;
else if(element.TryGetInt64(out var l)) return l;
else if(element.TryGetSingle(out var f)) return f;
else if(element.TryGetUInt16(out var us)) return us;
else if(element.TryGetUInt32(out var ui)) return ui;
else if(element.TryGetUInt64(out var ul)) return ul;
else throw new InvalidOperationException("unexpected additional value type during number deserialization");
case JsonValueKind.String:
if(element.TryGetDateTime(out var dt)) return dt;
else if(element.TryGetDateTimeOffset(out var dto)) return dto;
else if(element.TryGetGuid(out var g)) return g;
else return element.GetString();
case JsonValueKind.Array:
case JsonValueKind.Object:
return element;
case JsonValueKind.True:
return true;
case JsonValueKind.False:
return false;
case JsonValueKind.Null:
case JsonValueKind.Undefined:
return null;
default:
throw new InvalidOperationException($"unexpected additional value type during deserialization json kind : {element.ValueKind}");
}
}
/// <summary>
/// Get the child node of the specified identifier
/// </summary>
/// <param name="identifier">The identifier of the child node</param>
/// <returns>An instance of <see cref="IParseNode"/></returns>
public IParseNode? GetChildNode(string identifier)
{
if(_jsonNode.ValueKind == JsonValueKind.Object && _jsonNode.TryGetProperty(identifier ?? throw new ArgumentNullException(nameof(identifier)), out var jsonElement))
{
return new JsonParseNode(jsonElement)
{
OnBeforeAssignFieldValues = OnBeforeAssignFieldValues,
OnAfterAssignFieldValues = OnAfterAssignFieldValues
};
}
return default;
}
}
}