< Summary

Information
Line coverage
0%
Covered lines: 0
Uncovered lines: 30
Coverable lines: 30
Total lines: 65
Line coverage: 0%
Branch coverage
0%
Covered branches: 0
Total branches: 6
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(...)100%110%
GetValueKindCore()0%220%
DeepCloneCore()100%110%
WriteTo(...)0%440%
ComputeValueKind()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\JsonValueOfTCustomized.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.Text.Json.Serialization.Metadata;
 6
 7namespace System.Text.Json.Nodes
 8{
 9    /// <summary>
 10    /// A JsonValue that encapsulates arbitrary .NET type configurations.
 11    /// Paradoxically, instances of this type can be of any JsonValueKind
 12    /// (including objects and arrays) and introspecting these values is
 13    /// generally slower compared to the other JsonValue implementations.
 14    /// </summary>
 15    internal sealed class JsonValueCustomized<TValue> : JsonValue<TValue>
 16    {
 17        private readonly JsonTypeInfo<TValue> _jsonTypeInfo;
 18        private JsonValueKind? _valueKind;
 19
 020        public JsonValueCustomized(TValue value, JsonTypeInfo<TValue> jsonTypeInfo, JsonNodeOptions? options = null): ba
 021        {
 022            Debug.Assert(jsonTypeInfo.IsConfigured);
 023            _jsonTypeInfo = jsonTypeInfo;
 024        }
 25
 026        private protected override JsonValueKind GetValueKindCore() => _valueKind ??= ComputeValueKind();
 027        internal override JsonNode DeepCloneCore() => JsonSerializer.SerializeToNode(Value, _jsonTypeInfo)!;
 28
 29        public override void WriteTo(Utf8JsonWriter writer, JsonSerializerOptions? options = null)
 030        {
 031            ArgumentNullException.ThrowIfNull(writer);
 32
 033            JsonTypeInfo<TValue> jsonTypeInfo = _jsonTypeInfo;
 34
 035            if (options != null && options != jsonTypeInfo.Options)
 036            {
 037                options.MakeReadOnly();
 038                jsonTypeInfo = (JsonTypeInfo<TValue>)options.GetTypeInfoInternal(typeof(TValue));
 039            }
 40
 041            jsonTypeInfo.Serialize(writer, Value);
 042        }
 43
 44        /// <summary>
 45        /// Computes the JsonValueKind of the value by serializing it and reading the resultant JSON.
 46        /// </summary>
 47        private JsonValueKind ComputeValueKind()
 048        {
 049            Utf8JsonWriter writer = Utf8JsonWriterCache.RentWriterAndBuffer(options: default, JsonSerializerOptions.Buff
 50            try
 051            {
 052                WriteTo(writer);
 053                writer.Flush();
 054                Utf8JsonReader reader = new(output.WrittenSpan);
 055                bool success = reader.Read();
 056                Debug.Assert(success);
 057                return JsonReaderHelper.ToValueKind(reader.TokenType);
 58            }
 59            finally
 060            {
 061                Utf8JsonWriterCache.ReturnWriterAndBuffer(writer, output);
 062            }
 063        }
 64    }
 65}