< Summary

Class:Azure.Core.JsonElementExtensions
Assembly:Azure.Search.Documents
File(s):C:\Git\azure-sdk-for-net\sdk\core\Azure.Core\src\Shared\AutoRest\JsonElementExtensions.cs
Covered lines:11
Uncovered lines:25
Coverable lines:36
Total lines:90
Line coverage:30.5% (11 of 36)
Covered branches:6
Total branches:29
Branch coverage:20.6% (6 of 29)

Metrics

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

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        {
 118            switch (element.ValueKind)
 19            {
 20                case JsonValueKind.String:
 121                    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
 2464        public static DateTimeOffset GetDateTimeOffset(in this JsonElement element, string format) => format switch
 2465        {
 066            "U" when element.ValueKind == JsonValueKind.Number => DateTimeOffset.FromUnixTimeSeconds(element.GetInt64())
 4867            _ => TypeFormatters.ParseDateTimeOffset(element.GetString(), format)
 2468        };
 69
 70        public static TimeSpan GetTimeSpan(in this JsonElement element, string format) =>
 571            TypeFormatters.ParseTimeSpan(element.GetString(), format);
 72
 73        public static char GetChar(this in JsonElement element)
 74        {
 275            if (element.ValueKind == JsonValueKind.String)
 76            {
 277                var text = element.GetString();
 278                if (text == null || text.Length != 1)
 79                {
 080                    throw new NotSupportedException($"Cannot convert \"{text}\" to a Char");
 81                }
 282                return text[0];
 83            }
 84            else
 85            {
 086                throw new NotSupportedException($"Cannot convert {element.ValueKind} to a Char");
 87            }
 88        }
 89    }
 90}