< Summary

Information
Line coverage
0%
Covered lines: 0
Uncovered lines: 27
Coverable lines: 27
Total lines: 59
Line coverage: 0%
Branch coverage
0%
Covered branches: 0
Total branches: 14
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%660%
GetValueKindCore()100%110%
DeepCloneCore()100%110%
DeepEqualsCore(...)0%440%
WriteTo(...)0%440%

File(s)

C:\h\w\B31A098C\w\BB5A0A33\e\runtime-utils\Runner\runtime\src\libraries\System.Text.Json\src\System\Text\Json\Nodes\JsonValueOfTPrimitive.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.Collections.Generic;
 5using System.Diagnostics;
 6using System.Text.Json.Serialization;
 7
 8namespace System.Text.Json.Nodes
 9{
 10    /// <summary>
 11    /// A JsonValue encapsulating a primitive value using a built-in converter for the type.
 12    /// </summary>
 13    internal sealed class JsonValuePrimitive<TValue> : JsonValue<TValue>
 14    {
 15        private readonly JsonConverter<TValue> _converter;
 16        private readonly JsonValueKind _valueKind;
 17
 018        public JsonValuePrimitive(TValue value, JsonConverter<TValue> converter, JsonNodeOptions? options) : base(value,
 019        {
 020            Debug.Assert(TypeIsSupportedPrimitive, $"The type {typeof(TValue)} is not a supported primitive.");
 021            Debug.Assert(converter is { IsInternalConverter: true, ConverterStrategy: ConverterStrategy.Value });
 22
 023            _converter = converter;
 024            _valueKind = DetermineValueKind(value);
 025        }
 26
 027        private protected override JsonValueKind GetValueKindCore() => _valueKind;
 028        internal override JsonNode DeepCloneCore() => new JsonValuePrimitive<TValue>(Value, _converter, Options);
 29
 30        internal override bool DeepEqualsCore(JsonNode otherNode)
 031        {
 032            if (otherNode is JsonValue otherValue && otherValue.TryGetValue(out TValue? v))
 033            {
 34                // Because TValue is equatable and otherNode returns a matching
 35                // type we can short circuit the comparison in this case.
 036                return EqualityComparer<TValue>.Default.Equals(Value, v);
 37            }
 38
 039            return base.DeepEqualsCore(otherNode);
 040        }
 41
 42        public override void WriteTo(Utf8JsonWriter writer, JsonSerializerOptions? options = null)
 043        {
 044            ArgumentNullException.ThrowIfNull(writer);
 45
 046            JsonConverter<TValue> converter = _converter;
 047            options ??= s_defaultOptions;
 48
 049            if (converter.IsInternalConverterForNumberType)
 050            {
 051                converter.WriteNumberWithCustomHandling(writer, Value, options.NumberHandling);
 052            }
 53            else
 054            {
 055                converter.Write(writer, Value, options);
 056            }
 057        }
 58    }
 59}