| | 1 | | // Copyright (c) Microsoft Corporation. All rights reserved. |
| | 2 | | // Licensed under the MIT License. |
| | 3 | |
|
| | 4 | | #nullable enable |
| | 5 | |
|
| | 6 | | using System; |
| | 7 | | using System.Collections.Generic; |
| | 8 | | using System.Globalization; |
| | 9 | | using System.Text.Json; |
| | 10 | | using System.Xml; |
| | 11 | |
|
| | 12 | | namespace Azure.Core |
| | 13 | | { |
| | 14 | | internal static class JsonElementExtensions |
| | 15 | | { |
| | 16 | | public static object? GetObject(in this JsonElement element) |
| | 17 | | { |
| 1 | 18 | | switch (element.ValueKind) |
| | 19 | | { |
| | 20 | | case JsonValueKind.String: |
| 1 | 21 | | return element.GetString(); |
| | 22 | | case JsonValueKind.Number: |
| 0 | 23 | | if (element.TryGetInt32(out int intValue)) |
| | 24 | | { |
| 0 | 25 | | return intValue; |
| | 26 | | } |
| 0 | 27 | | if (element.TryGetInt64(out long longValue)) |
| | 28 | | { |
| 0 | 29 | | return longValue; |
| | 30 | | } |
| 0 | 31 | | return element.GetDouble(); |
| | 32 | | case JsonValueKind.True: |
| 0 | 33 | | return true; |
| | 34 | | case JsonValueKind.False: |
| 0 | 35 | | return false; |
| | 36 | | case JsonValueKind.Undefined: |
| | 37 | | case JsonValueKind.Null: |
| 0 | 38 | | return null; |
| | 39 | | case JsonValueKind.Object: |
| 0 | 40 | | var dictionary = new Dictionary<string, object?>(); |
| 0 | 41 | | foreach (JsonProperty jsonProperty in element.EnumerateObject()) |
| | 42 | | { |
| 0 | 43 | | dictionary.Add(jsonProperty.Name, jsonProperty.Value.GetObject()); |
| | 44 | | } |
| 0 | 45 | | return dictionary; |
| | 46 | | case JsonValueKind.Array: |
| 0 | 47 | | var list = new List<object?>(); |
| 0 | 48 | | foreach (JsonElement item in element.EnumerateArray()) |
| | 49 | | { |
| 0 | 50 | | list.Add(item.GetObject()); |
| | 51 | | } |
| 0 | 52 | | return list.ToArray(); |
| | 53 | | default: |
| 0 | 54 | | throw new NotSupportedException("Not supported value kind " + element.ValueKind); |
| | 55 | | } |
| | 56 | | } |
| | 57 | |
|
| 0 | 58 | | public static byte[] GetBytesFromBase64(in this JsonElement element, string format) => format switch |
| 0 | 59 | | { |
| 0 | 60 | | "U" => TypeFormatters.FromBase64UrlString(element.GetString()), |
| 0 | 61 | | _ => throw new ArgumentException($"Format is not supported: '{format}'", nameof(format)) |
| 0 | 62 | | }; |
| | 63 | |
|
| 24 | 64 | | public static DateTimeOffset GetDateTimeOffset(in this JsonElement element, string format) => format switch |
| 24 | 65 | | { |
| 0 | 66 | | "U" when element.ValueKind == JsonValueKind.Number => DateTimeOffset.FromUnixTimeSeconds(element.GetInt64()) |
| 48 | 67 | | _ => TypeFormatters.ParseDateTimeOffset(element.GetString(), format) |
| 24 | 68 | | }; |
| | 69 | |
|
| | 70 | | public static TimeSpan GetTimeSpan(in this JsonElement element, string format) => |
| 5 | 71 | | TypeFormatters.ParseTimeSpan(element.GetString(), format); |
| | 72 | |
|
| | 73 | | public static char GetChar(this in JsonElement element) |
| | 74 | | { |
| 2 | 75 | | if (element.ValueKind == JsonValueKind.String) |
| | 76 | | { |
| 2 | 77 | | var text = element.GetString(); |
| 2 | 78 | | if (text == null || text.Length != 1) |
| | 79 | | { |
| 0 | 80 | | throw new NotSupportedException($"Cannot convert \"{text}\" to a Char"); |
| | 81 | | } |
| 2 | 82 | | return text[0]; |
| | 83 | | } |
| | 84 | | else |
| | 85 | | { |
| 0 | 86 | | throw new NotSupportedException($"Cannot convert {element.ValueKind} to a Char"); |
| | 87 | | } |
| | 88 | | } |
| | 89 | | } |
| | 90 | | } |