< Summary

Information
Line coverage
0%
Covered lines: 0
Uncovered lines: 127
Coverable lines: 127
Total lines: 204
Line coverage: 0%
Branch coverage
0%
Covered branches: 0
Total branches: 87
Branch coverage: 0%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Cyclomatic complexity NPath complexity Sequence coverage
.ctor(...)0%220%
DeepCloneCore()100%110%
GetValueKindCore()100%110%
DeepEqualsCore(...)0%660%
GetValue()0%220%
TryGetValue(...)0%77770%
WriteTo(...)100%110%

File(s)

C:\h\w\B31A098C\w\BB5A0A33\e\runtime-utils\Runner\runtime\src\libraries\System.Text.Json\src\System\Text\Json\Nodes\JsonValueOfElement.cs

#LineLine coverage
 1// Licensed to the .NET Foundation under one or more agreements.
 2// The .NET Foundation licenses this file to you under the MIT license.
 3
 4using System.Diagnostics;
 5using System.Diagnostics.CodeAnalysis;
 6
 7namespace System.Text.Json.Nodes
 8{
 9    /// <summary>
 10    /// Defines a primitive JSON value that is wrapping a <see cref="JsonElement"/>.
 11    /// </summary>
 12    internal sealed class JsonValueOfElement : JsonValue<JsonElement>
 13    {
 014        public JsonValueOfElement(JsonElement value, JsonNodeOptions? options) : base(value, options)
 015        {
 016            Debug.Assert(value.ValueKind is JsonValueKind.False or JsonValueKind.True or JsonValueKind.Number or JsonVal
 017        }
 18
 019        internal override JsonElement? UnderlyingElement => Value;
 020        internal override JsonNode DeepCloneCore() => new JsonValueOfElement(Value.Clone(), Options);
 021        private protected override JsonValueKind GetValueKindCore() => Value.ValueKind;
 22
 23        internal override bool DeepEqualsCore(JsonNode otherNode)
 024        {
 025            if (otherNode.UnderlyingElement is JsonElement otherElement)
 026            {
 027                return JsonElement.DeepEquals(Value, otherElement);
 28            }
 29
 030            if (otherNode is JsonValue)
 031            {
 32                // Dispatch to the other value in case it knows
 33                // how to convert JsonElement to its own type.
 034                return otherNode.DeepEqualsCore(this);
 35            }
 36
 037            return base.DeepEqualsCore(otherNode);
 038        }
 39
 40        public override TypeToConvert GetValue<TypeToConvert>()
 041        {
 042            if (!TryGetValue(out TypeToConvert? value))
 043            {
 044                ThrowHelper.ThrowInvalidOperationException_NodeUnableToConvertElement(Value.ValueKind, typeof(TypeToConv
 45            }
 46
 047            return value;
 048        }
 49
 50        public override bool TryGetValue<TypeToConvert>([NotNullWhen(true)] out TypeToConvert value)
 051        {
 52            bool success;
 53
 054            if (Value is TypeToConvert element)
 055            {
 056                value = element;
 057                return true;
 58            }
 59
 060            switch (Value.ValueKind)
 61            {
 62                case JsonValueKind.Number:
 063                    if (typeof(TypeToConvert) == typeof(int) || typeof(TypeToConvert) == typeof(int?))
 064                    {
 065                        success = Value.TryGetInt32(out int result);
 066                        value = (TypeToConvert)(object)result;
 067                        return success;
 68                    }
 69
 070                    if (typeof(TypeToConvert) == typeof(long) || typeof(TypeToConvert) == typeof(long?))
 071                    {
 072                        success = Value.TryGetInt64(out long result);
 073                        value = (TypeToConvert)(object)result;
 074                        return success;
 75                    }
 76
 077                    if (typeof(TypeToConvert) == typeof(double) || typeof(TypeToConvert) == typeof(double?))
 078                    {
 079                        success = Value.TryGetDouble(out double result);
 080                        value = (TypeToConvert)(object)result;
 081                        return success;
 82                    }
 83
 084                    if (typeof(TypeToConvert) == typeof(short) || typeof(TypeToConvert) == typeof(short?))
 085                    {
 086                        success = Value.TryGetInt16(out short result);
 087                        value = (TypeToConvert)(object)result;
 088                        return success;
 89                    }
 90
 091                    if (typeof(TypeToConvert) == typeof(decimal) || typeof(TypeToConvert) == typeof(decimal?))
 092                    {
 093                        success = Value.TryGetDecimal(out decimal result);
 094                        value = (TypeToConvert)(object)result;
 095                        return success;
 96                    }
 97
 098                    if (typeof(TypeToConvert) == typeof(byte) || typeof(TypeToConvert) == typeof(byte?))
 099                    {
 0100                        success = Value.TryGetByte(out byte result);
 0101                        value = (TypeToConvert)(object)result;
 0102                        return success;
 103                    }
 104
 0105                    if (typeof(TypeToConvert) == typeof(float) || typeof(TypeToConvert) == typeof(float?))
 0106                    {
 0107                        success = Value.TryGetSingle(out float result);
 0108                        value = (TypeToConvert)(object)result;
 0109                        return success;
 110                    }
 111
 0112                    if (typeof(TypeToConvert) == typeof(uint) || typeof(TypeToConvert) == typeof(uint?))
 0113                    {
 0114                        success = Value.TryGetUInt32(out uint result);
 0115                        value = (TypeToConvert)(object)result;
 0116                        return success;
 117                    }
 118
 0119                    if (typeof(TypeToConvert) == typeof(ushort) || typeof(TypeToConvert) == typeof(ushort?))
 0120                    {
 0121                        success = Value.TryGetUInt16(out ushort result);
 0122                        value = (TypeToConvert)(object)result;
 0123                        return success;
 124                    }
 125
 0126                    if (typeof(TypeToConvert) == typeof(ulong) || typeof(TypeToConvert) == typeof(ulong?))
 0127                    {
 0128                        success = Value.TryGetUInt64(out ulong result);
 0129                        value = (TypeToConvert)(object)result;
 0130                        return success;
 131                    }
 132
 0133                    if (typeof(TypeToConvert) == typeof(sbyte) || typeof(TypeToConvert) == typeof(sbyte?))
 0134                    {
 0135                        success = Value.TryGetSByte(out sbyte result);
 0136                        value = (TypeToConvert)(object)result;
 0137                        return success;
 138                    }
 0139                    break;
 140
 141                case JsonValueKind.String:
 0142                    if (typeof(TypeToConvert) == typeof(string))
 0143                    {
 0144                        string? result = Value.GetString();
 0145                        Debug.Assert(result != null);
 0146                        value = (TypeToConvert)(object)result;
 0147                        return true;
 148                    }
 149
 0150                    if (typeof(TypeToConvert) == typeof(DateTime) || typeof(TypeToConvert) == typeof(DateTime?))
 0151                    {
 0152                        success = Value.TryGetDateTime(out DateTime result);
 0153                        value = (TypeToConvert)(object)result;
 0154                        return success;
 155                    }
 156
 0157                    if (typeof(TypeToConvert) == typeof(DateTimeOffset) || typeof(TypeToConvert) == typeof(DateTimeOffse
 0158                    {
 0159                        success = Value.TryGetDateTimeOffset(out DateTimeOffset result);
 0160                        value = (TypeToConvert)(object)result;
 0161                        return success;
 162                    }
 163
 0164                    if (typeof(TypeToConvert) == typeof(Guid) || typeof(TypeToConvert) == typeof(Guid?))
 0165                    {
 0166                        success = Value.TryGetGuid(out Guid result);
 0167                        value = (TypeToConvert)(object)result;
 0168                        return success;
 169                    }
 170
 0171                    if (typeof(TypeToConvert) == typeof(char) || typeof(TypeToConvert) == typeof(char?))
 0172                    {
 0173                        string? result = Value.GetString();
 0174                        Debug.Assert(result != null);
 0175                        if (result.Length == 1)
 0176                        {
 0177                            value = (TypeToConvert)(object)result[0];
 0178                            return true;
 179                        }
 0180                    }
 0181                    break;
 182
 183                case JsonValueKind.True:
 184                case JsonValueKind.False:
 0185                    if (typeof(TypeToConvert) == typeof(bool) || typeof(TypeToConvert) == typeof(bool?))
 0186                    {
 0187                        value = (TypeToConvert)(object)Value.GetBoolean();
 0188                        return true;
 189                    }
 0190                    break;
 191            }
 192
 0193            value = default!;
 0194            return false;
 0195        }
 196
 197        public override void WriteTo(Utf8JsonWriter writer, JsonSerializerOptions? options = null)
 0198        {
 0199            ArgumentNullException.ThrowIfNull(writer);
 200
 0201            Value.WriteTo(writer);
 0202        }
 203    }
 204}