Skip to content

Commit d4a2bbf

Browse files
Merge pull request #10 from KristofferStrube/feature/switch-to-specific-converter-for-endingtype
Added a specific converter for `EndingType` instead of using generic description based converter.
2 parents 17ef39a + 87b333d commit d4a2bbf

File tree

3 files changed

+29
-49
lines changed

3 files changed

+29
-49
lines changed
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: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using System.ComponentModel;
1+
using KristofferStrube.Blazor.FileAPI.Converters;
22
using System.Text.Json.Serialization;
33

44
namespace KristofferStrube.Blazor.FileAPI;
@@ -9,18 +9,16 @@ namespace KristofferStrube.Blazor.FileAPI;
99
/// <remarks>
1010
/// <see href="https://www.w3.org/TR/FileAPI/#enumdef-endingtype">EndingType browser specs</see>
1111
/// </remarks>
12-
[JsonConverter(typeof(EnumDescriptionConverter<EndingType>))]
12+
[JsonConverter(typeof(EndingTypeConverter))]
1313
public enum EndingType
1414
{
1515
/// <summary>
1616
/// Line endings in <see cref="string"/> elements within <see cref="BlobPart"/>s remain unchanged, with no conversion applied, preserving the original format.
1717
/// </summary>
18-
[Description("transparent")]
1918
Transparent,
2019

2120
/// <summary>
2221
/// 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.
2322
/// </summary>
24-
[Description("native")]
2523
Native,
2624
}

0 commit comments

Comments
 (0)