< Summary - Jellyfin

Information
Class: Jellyfin.Extensions.Json.Converters.JsonDefaultStringEnumConverter<T>
Assembly: Jellyfin.Extensions
File(s): /srv/git/jellyfin/src/Jellyfin.Extensions/Json/Converters/JsonDefaultStringEnumConverter.cs
Line coverage
90%
Covered lines: 10
Uncovered lines: 1
Coverable lines: 11
Total lines: 49
Line coverage: 90.9%
Branch coverage
75%
Covered branches: 6
Total branches: 8
Branch coverage: 75%
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(...)75%8.3883.33%
Write(...)100%11100%

File(s)

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

#LineLine coverage
 1using System;
 2using System.ComponentModel;
 3using System.Reflection;
 4using System.Text.Json;
 5using System.Text.Json.Serialization;
 6
 7namespace Jellyfin.Extensions.Json.Converters;
 8
 9/// <summary>
 10/// Json unknown enum converter.
 11/// </summary>
 12/// <typeparam name="T">The type of enum.</typeparam>
 13public class JsonDefaultStringEnumConverter<T> : JsonConverter<T>
 14    where T : struct, Enum
 15{
 16    private readonly JsonConverter<T> _baseConverter;
 17
 18    /// <summary>
 19    /// Initializes a new instance of the <see cref="JsonDefaultStringEnumConverter{T}"/> class.
 20    /// </summary>
 21    /// <param name="baseConverter">The base json converter.</param>
 1922    public JsonDefaultStringEnumConverter(JsonConverter<T> baseConverter)
 23    {
 1924        _baseConverter = baseConverter;
 1925    }
 26
 27    /// <inheritdoc />
 28    public override T Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
 29    {
 195830        if (reader.IsNull() || reader.IsEmptyString())
 31        {
 5032            var customValueAttribute = typeToConvert.GetCustomAttribute<DefaultValueAttribute>();
 5033            if (customValueAttribute?.Value is null)
 34            {
 035                throw new InvalidOperationException($"Default value not set for '{typeToConvert.Name}'");
 36            }
 37
 5038            return (T)customValueAttribute.Value;
 39        }
 40
 190841        return _baseConverter.Read(ref reader, typeToConvert, options);
 42    }
 43
 44    /// <inheritdoc />
 45    public override void Write(Utf8JsonWriter writer, T value, JsonSerializerOptions options)
 46    {
 1047        _baseConverter.Write(writer, value, options);
 1048    }
 49}