Skip to content

Commit abdb8c7

Browse files
Added a specific converter for EndingType instead of using generic description based converter.
1 parent 17ef39a commit abdb8c7

3 files changed

Lines changed: 30 additions & 49 deletions

File tree

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
using System.Text.Json;
2+
using System.Text.Json.Serialization;
3+
4+
namespace KristofferStrube.Blazor.FileAPI.Converters;
5+
6+
internal class EndingTypeConverter : JsonConverter<EndingType>
7+
{
8+
public override EndingType Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
9+
{
10+
return reader.GetString() switch
11+
{
12+
"transparent" => EndingType.Transparent,
13+
"native" => EndingType.Native,
14+
var value => throw new ArgumentException($"Value '{value}' was not a valid {nameof(EndingType)}."),
15+
};
16+
}
17+
18+
public override void Write(Utf8JsonWriter writer, EndingType value, JsonSerializerOptions options)
19+
{
20+
writer.WriteStringValue(value switch
21+
{
22+
EndingType.Transparent => "transparent",
23+
EndingType.Native => "native",
24+
_ => throw new ArgumentException($"Value '{value}' was not a valid {nameof(EndingType)}.")
25+
});
26+
}
27+
}

src/KristofferStrube.Blazor.FileAPI/Converters/EnumDescriptionConverter.cs

Lines changed: 0 additions & 45 deletions
This file was deleted.
Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using System.ComponentModel;
1+
using KristofferStrube.Blazor.FileAPI.Converters;
2+
using System.ComponentModel;
23
using System.Text.Json.Serialization;
34

45
namespace KristofferStrube.Blazor.FileAPI;
@@ -9,18 +10,16 @@ namespace KristofferStrube.Blazor.FileAPI;
910
/// <remarks>
1011
/// <see href="https://www.w3.org/TR/FileAPI/#enumdef-endingtype">EndingType browser specs</see>
1112
/// </remarks>
12-
[JsonConverter(typeof(EnumDescriptionConverter<EndingType>))]
13+
[JsonConverter(typeof(EndingTypeConverter))]
1314
public enum EndingType
1415
{
1516
/// <summary>
1617
/// Line endings in <see cref="string"/> elements within <see cref="BlobPart"/>s remain unchanged, with no conversion applied, preserving the original format.
1718
/// </summary>
18-
[Description("transparent")]
1919
Transparent,
2020

2121
/// <summary>
2222
/// Line endings in <see cref="string"/> elements within <see cref="BlobPart"/>s are converted to the platform's native format (<c>\n</c> or <c>\r\n</c>), ensuring consistency with the system's convention.
2323
/// </summary>
24-
[Description("native")]
2524
Native,
2625
}

0 commit comments

Comments
 (0)