Skip to content

armgit / Filee

Public Imported from GitHub
0

Free, open-source file converter. Convert 263 formats offline by drag & drop — images, PDF, Word, Excel, PowerPoint, HWP/HWPX, video, audio, e-books and archives. No upload, no Office needed.

Presets: saved enum values are read by their whole name

KnifeLemonacd7f055b0d8
diff --git a/src/Filee.Core/Presets/Preset.cs b/src/Filee.Core/Presets/Preset.cs
index 6f377ff7..72550948 100644
--- a/src/Filee.Core/Presets/Preset.cs
+++ b/src/Filee.Core/Presets/Preset.cs
@@ -27,14 +27,22 @@ public class LenientEnumJsonConverter<TEnum> : JsonConverter<TEnum> where TEnum
{
protected virtual TEnum Unknown => default;
- public override TEnum Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) =>
- reader.TokenType == JsonTokenType.String && Enum.TryParse<TEnum>(reader.GetString(), ignoreCase: true, out var value) &&
- Enum.IsDefined(value)
- ? value
- : reader.TokenType == JsonTokenType.Number && reader.TryGetInt32(out var number) &&
- Enum.IsDefined((TEnum)Enum.ToObject(typeof(TEnum), number))
+ public override TEnum Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
+ {
+ if (reader.TokenType == JsonTokenType.String)
+ {
+ // A name, matched whole: Enum.TryParse would also take "Left90, Rotate180" (the flags OR'd into another
+ // value) or "2".
+ var text = reader.GetString();
+ return Enum.GetNames<TEnum>().FirstOrDefault(n => string.Equals(n, text, StringComparison.OrdinalIgnoreCase)) is { } name
+ ? Enum.Parse<TEnum>(name)
+ : Unknown;
+ }
+ return reader.TokenType == JsonTokenType.Number && reader.TryGetInt32(out var number) &&
+ Enum.IsDefined((TEnum)Enum.ToObject(typeof(TEnum), number))
? (TEnum)Enum.ToObject(typeof(TEnum), number)
: Unknown;
+ }
public override void Write(Utf8JsonWriter writer, TEnum value, JsonSerializerOptions options) =>
writer.WriteStringValue(value.ToString());
diff --git a/tests/Filee.Core.Tests/SmallParsersTests.cs b/tests/Filee.Core.Tests/SmallParsersTests.cs
index c576f7ac..b33fe152 100644
--- a/tests/Filee.Core.Tests/SmallParsersTests.cs
+++ b/tests/Filee.Core.Tests/SmallParsersTests.cs
@@ -53,6 +53,20 @@ public class BuiltInDataTests
Assert.All(profile.PresetIds, id => Assert.Contains(id, ids));
}
+ [Theory]
+ [InlineData("\"left90\"", ImageRotation.Left90)]
+ [InlineData("\"Rotate180\"", ImageRotation.Rotate180)]
+ [InlineData("3", ImageRotation.Rotate180)]
+ [InlineData("\"Left90, Rotate180\"", ImageRotation.None)] // flags OR'd into another value
+ [InlineData("\"2\"", ImageRotation.None)] // a number in a string isn't a name
+ [InlineData("\"Sideways\"", ImageRotation.None)]
+ [InlineData("null", ImageRotation.None)]
+ public void Saved_enum_values_are_read_by_name_and_anything_else_is_the_default(string json, ImageRotation expected)
+ {
+ var options = new System.Text.Json.JsonSerializerOptions { Converters = { new LenientEnumJsonConverter<ImageRotation>() } };
+ Assert.Equal(expected, System.Text.Json.JsonSerializer.Deserialize<ImageRotation>(json, options));
+ }
+
[Fact]
public void A_group_clone_keeps_its_kind_and_members()
{