< Summary

Class:Azure.Core.JsonElementExtensions
Assembly:Azure.ResourceManager.KeyVault
File(s):C:\Git\azure-sdk-for-net\sdk\core\Azure.Core\src\Shared\AutoRest\JsonElementExtensions.cs
Covered lines:4
Uncovered lines:32
Coverable lines:36
Total lines:90
Line coverage:11.1% (4 of 36)
Covered branches:2
Total branches:29
Branch coverage:6.8% (2 of 29)

Metrics

MethodCyclomatic complexity Line coverage Branch coverage
GetObject(...)-0%0%
GetBytesFromBase64(...)-0%0%
GetDateTimeOffset(...)-80%50%
GetTimeSpan(...)-0%100%
GetChar(...)-0%0%

File(s)

C:\Git\azure-sdk-for-net\sdk\core\Azure.Core\src\Shared\AutoRest\JsonElementExtensions.cs

#LineLine coverage
 1// Copyright (c) Microsoft Corporation. All rights reserved.
 2// Licensed under the MIT License.
 3
 4#nullable enable
 5
 6using System;
 7using System.Collections.Generic;
 8using System.Globalization;
 9using System.Text.Json;
 10using System.Xml;
 11
 12namespace Azure.Core
 13{
 14    internal static class JsonElementExtensions
 15    {
 16        public static object? GetObject(in this JsonElement element)
 17        {
 018            switch (element.ValueKind)
 19            {
 20                case JsonValueKind.String:
 021                    return element.GetString();
 22                case JsonValueKind.Number:
 023                    if (element.TryGetInt32(out int intValue))
 24                    {
 025                        return intValue;
 26                    }
 027                    if (element.TryGetInt64(out long longValue))
 28                    {
 029                        return longValue;
 30                    }
 031                    return element.GetDouble();
 32                case JsonValueKind.True:
 033                    return true;
 34                case JsonValueKind.False:
 035                    return false;
 36                case JsonValueKind.Undefined:
 37                case JsonValueKind.Null:
 038                    return null;
 39                case JsonValueKind.Object:
 040                    var dictionary = new Dictionary<string, object?>();
 041                    foreach (JsonProperty jsonProperty in element.EnumerateObject())
 42                    {
 043                        dictionary.Add(jsonProperty.Name, jsonProperty.Value.GetObject());
 44                    }
 045                    return dictionary;
 46                case JsonValueKind.Array:
 047                    var list = new List<object?>();
 048                    foreach (JsonElement item in element.EnumerateArray())
 49                    {
 050                        list.Add(item.GetObject());
 51                    }
 052                    return list.ToArray();
 53                default:
 054                    throw new NotSupportedException("Not supported value kind " + element.ValueKind);
 55            }
 56        }
 57
 058        public static byte[] GetBytesFromBase64(in this JsonElement element, string format) => format switch
 059        {
 060            "U" => TypeFormatters.FromBase64UrlString(element.GetString()),
 061            _ => throw new ArgumentException($"Format is not supported: '{format}'", nameof(format))
 062        };
 63
 37664        public static DateTimeOffset GetDateTimeOffset(in this JsonElement element, string format) => format switch
 37665        {
 066            "U" when element.ValueKind == JsonValueKind.Number => DateTimeOffset.FromUnixTimeSeconds(element.GetInt64())
 75267            _ => TypeFormatters.ParseDateTimeOffset(element.GetString(), format)
 37668        };
 69
 70        public static TimeSpan GetTimeSpan(in this JsonElement element, string format) =>
 071            TypeFormatters.ParseTimeSpan(element.GetString(), format);
 72
 73        public static char GetChar(this in JsonElement element)
 74        {
 075            if (element.ValueKind == JsonValueKind.String)
 76            {
 077                var text = element.GetString();
 078                if (text == null || text.Length != 1)
 79                {
 080                    throw new NotSupportedException($"Cannot convert \"{text}\" to a Char");
 81                }
 082                return text[0];
 83            }
 84            else
 85            {
 086                throw new NotSupportedException($"Cannot convert {element.ValueKind} to a Char");
 87            }
 88        }
 89    }
 90}