< 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
80%
Covered lines: 21
Uncovered lines: 5
Coverable lines: 26
Total lines: 90
Line coverage: 80.7%
Branch coverage
68%
Covered branches: 11
Total branches: 16
Branch coverage: 68.7%
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(...)100%88100%
Write(...)37.5%19844.44%

File(s)

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

#LineLine coverage
 1using System;
 2using System.Collections.Generic;
 3using System.ComponentModel;
 4using System.Text.Json;
 5using System.Text.Json.Serialization;
 6
 7namespace Jellyfin.Extensions.Json.Converters
 8{
 9    /// <summary>
 10    /// Convert delimited string to array of type.
 11    /// </summary>
 12    /// <typeparam name="T">Type to convert to.</typeparam>
 13    public abstract class JsonDelimitedArrayConverter<T> : JsonConverter<T[]>
 14    {
 15        private readonly TypeConverter _typeConverter;
 16
 17        /// <summary>
 18        /// Initializes a new instance of the <see cref="JsonDelimitedArrayConverter{T}"/> class.
 19        /// </summary>
 1820        protected JsonDelimitedArrayConverter()
 21        {
 1822            _typeConverter = TypeDescriptor.GetConverter(typeof(T));
 1823        }
 24
 25        /// <summary>
 26        /// Gets the array delimiter.
 27        /// </summary>
 28        protected virtual char Delimiter { get; }
 29
 30        /// <inheritdoc />
 31        public override T[]? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
 32        {
 1533            if (reader.TokenType == JsonTokenType.String)
 34            {
 35                // null got handled higher up the call stack
 1136                var stringEntries = reader.GetString()!.Split(Delimiter, StringSplitOptions.RemoveEmptyEntries);
 1137                if (stringEntries.Length == 0)
 38                {
 139                    return [];
 40                }
 41
 1042                var typedValues = new List<T>();
 7043                for (var i = 0; i < stringEntries.Length; i++)
 44                {
 45                    try
 46                    {
 2547                        var parsedValue = _typeConverter.ConvertFromInvariantString(stringEntries[i].Trim());
 2448                        if (parsedValue is not null)
 49                        {
 2450                            typedValues.Add((T)parsedValue);
 51                        }
 2452                    }
 153                    catch (FormatException)
 54                    {
 55                        // Ignore unconvertible inputs
 156                    }
 57                }
 58
 1059                return typedValues.ToArray();
 60            }
 61
 462            return JsonSerializer.Deserialize<T[]>(ref reader, options);
 63        }
 64
 65        /// <inheritdoc />
 66        public override void Write(Utf8JsonWriter writer, T[]? value, JsonSerializerOptions options)
 67        {
 3068            if (value is not null)
 69            {
 3070                writer.WriteStartArray();
 3071                if (value.Length > 0)
 72                {
 073                    foreach (var it in value)
 74                    {
 075                        if (it is not null)
 76                        {
 077                            writer.WriteStringValue(it.ToString());
 78                        }
 79                    }
 80                }
 81
 3082                writer.WriteEndArray();
 83            }
 84            else
 85            {
 086                writer.WriteNullValue();
 87            }
 088        }
 89    }
 90}