< Summary

Line coverage
23%
Covered lines: 8
Uncovered lines: 26
Coverable lines: 34
Total lines: 72
Line coverage: 23.5%
Branch coverage
33%
Covered branches: 4
Total branches: 12
Branch coverage: 33.3%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Cyclomatic complexity NPath complexity Sequence coverage
Write(...)0%220%
Read(...)66.66%6675%
ReadAsJsonElement(...)100%1150%
ReadAsJsonNode(...)0%440%
GetSupportedJsonValueTypes(...)100%110%
GetSchema(...)100%110%

File(s)

https://raw.githubusercontent.com/dotnet/runtime/811a7eabb75c42db53440e8ba3f60c07511cfd1f/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/Converters/Node/JsonArrayConverter.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.Nodes;
 6using System.Text.Json.Schema;
 7
 8namespace System.Text.Json.Serialization.Converters
 9{
 10    internal sealed class JsonArrayConverter : JsonConverter<JsonArray?>
 11    {
 12        public override void Write(Utf8JsonWriter writer, JsonArray? value, JsonSerializerOptions options)
 013        {
 014            if (value is null)
 015            {
 016                writer.WriteNullValue();
 017                return;
 18            }
 19
 020            value.WriteTo(writer, options);
 021        }
 22
 23        public override JsonArray? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
 1224        {
 1225            switch (reader.TokenType)
 26            {
 27                case JsonTokenType.StartArray:
 828                    return options.AllowDuplicateProperties
 829                        ? ReadAsJsonElement(ref reader, options.GetNodeOptions())
 830                        : ReadAsJsonNode(ref reader, options.GetNodeOptions());
 31                case JsonTokenType.Null:
 032                    return null;
 33                default:
 434                    throw ThrowHelper.GetInvalidOperationException_ExpectedArray(reader.TokenType);
 35            }
 036        }
 37
 38        internal static JsonArray ReadAsJsonElement(ref Utf8JsonReader reader, JsonNodeOptions options)
 1639        {
 1640            JsonElement jElement = JsonElement.ParseValue(ref reader);
 041            return new JsonArray(jElement, options);
 042        }
 43
 44        internal static JsonArray ReadAsJsonNode(ref Utf8JsonReader reader, JsonNodeOptions options)
 045        {
 046            Debug.Assert(reader.TokenType == JsonTokenType.StartArray);
 47
 048            JsonArray jArray = new JsonArray(options);
 49
 050            while (reader.Read())
 051            {
 052                if (reader.TokenType == JsonTokenType.EndArray)
 053                {
 054                    return jArray;
 55                }
 56
 057                JsonNode? item = JsonNodeConverter.ReadAsJsonNode(ref reader, options);
 058                jArray.Add(item);
 059            }
 60
 61            // JSON is invalid so reader would have already thrown.
 062            Debug.Fail("End array token not found.");
 63            ThrowHelper.ThrowJsonException();
 64            return null;
 065        }
 66
 067        internal override JsonValueType GetSupportedJsonValueTypes(JsonNumberHandling _) => JsonValueType.Array;
 68
 069        internal override JsonSchema? GetSchema(JsonNumberHandling _) => new() { Type = JsonSchemaType.Array };
 70    }
 71}
 72