< Summary - Jellyfin

Information
Class: Jellyfin.Extensions.Json.Converters.JsonDelimitedArrayConverter<T>
Assembly: Jellyfin.Extensions
File(s): /srv/git/jellyfin/src/Jellyfin.Extensions/Json/Converters/JsonDelimitedArrayConverter.cs
Line coverage
95%
Covered lines: 23
Uncovered lines: 1
Coverable lines: 24
Total lines: 81
Line coverage: 95.8%
Branch coverage
91%
Covered branches: 11
Total branches: 12
Branch coverage: 91.6%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Coverage history

Coverage history 0 25 50 75 100

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor()100%11100%
Read(...)91.66%1212100%
Write(...)100%210%

File(s)

/srv/git/jellyfin/src/Jellyfin.Extensions/Json/Converters/JsonDelimitedArrayConverter.cs

#LineLine coverage
 1using System;
 2using System.ComponentModel;
 3using System.Text.Json;
 4using System.Text.Json.Serialization;
 5
 6namespace Jellyfin.Extensions.Json.Converters
 7{
 8    /// <summary>
 9    /// Convert delimited string to array of type.
 10    /// </summary>
 11    /// <typeparam name="T">Type to convert to.</typeparam>
 12    public abstract class JsonDelimitedArrayConverter<T> : JsonConverter<T[]>
 13    {
 14        private readonly TypeConverter _typeConverter;
 15
 16        /// <summary>
 17        /// Initializes a new instance of the <see cref="JsonDelimitedArrayConverter{T}"/> class.
 18        /// </summary>
 1619        protected JsonDelimitedArrayConverter()
 20        {
 1621            _typeConverter = TypeDescriptor.GetConverter(typeof(T));
 1622        }
 23
 24        /// <summary>
 25        /// Gets the array delimiter.
 26        /// </summary>
 27        protected virtual char Delimiter { get; }
 28
 29        /// <inheritdoc />
 30        public override T[]? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
 31        {
 1532            if (reader.TokenType == JsonTokenType.String)
 33            {
 34                // null got handled higher up the call stack
 1135                var stringEntries = reader.GetString()!.Split(Delimiter, StringSplitOptions.RemoveEmptyEntries);
 1136                if (stringEntries.Length == 0)
 37                {
 138                    return Array.Empty<T>();
 39                }
 40
 1041                var parsedValues = new object[stringEntries.Length];
 1042                var convertedCount = 0;
 7043                for (var i = 0; i < stringEntries.Length; i++)
 44                {
 45                    try
 46                    {
 2547                        parsedValues[i] = _typeConverter.ConvertFromInvariantString(stringEntries[i].Trim()) ?? throw ne
 2448                        convertedCount++;
 2449                    }
 150                    catch (FormatException)
 51                    {
 52                        // TODO log when upgraded to .Net6
 53                        // https://github.com/dotnet/runtime/issues/42975
 54                        // _logger.LogDebug(e, "Error converting value.");
 155                    }
 56                }
 57
 1058                var typedValues = new T[convertedCount];
 1059                var typedValueIndex = 0;
 7060                for (var i = 0; i < stringEntries.Length; i++)
 61                {
 2562                    if (parsedValues[i] is not null)
 63                    {
 2464                        typedValues.SetValue(parsedValues[i], typedValueIndex);
 2465                        typedValueIndex++;
 66                    }
 67                }
 68
 1069                return typedValues;
 70            }
 71
 472            return JsonSerializer.Deserialize<T[]>(ref reader, options);
 73        }
 74
 75        /// <inheritdoc />
 76        public override void Write(Utf8JsonWriter writer, T[]? value, JsonSerializerOptions options)
 77        {
 078            throw new NotImplementedException();
 79        }
 80    }
 81}