Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(serialization): replace JsonCamelCaseStringEnumConverter with custom SwapStyleEnumConverter #52

Merged
merged 2 commits into from
May 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/Htmxor/Serialization/HtmxorJsonSerializerContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ namespace Htmxor.Serialization;
GenerationMode = JsonSourceGenerationMode.Default,
Converters = [
typeof(TimespanMillisecondJsonConverter),
typeof(JsonCamelCaseStringEnumConverter<SwapStyle>),
typeof(SwapStyleEnumConverter),
typeof(JsonCamelCaseStringEnumConverter<ScrollBehavior>),
])]
[JsonSerializable(typeof(HtmxConfig))]
Expand Down
35 changes: 35 additions & 0 deletions src/Htmxor/Serialization/SwapStyleEnumConverter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
using System.Runtime.CompilerServices;
using System.Text.Json;
using System.Text.Json.Serialization;

namespace Htmxor.Serialization;

internal sealed class SwapStyleEnumConverter : JsonConverter<SwapStyle>
{
public override SwapStyle Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
var value = reader.GetString();

var style = value switch
{
null => SwapStyle.Default,
Constants.SwapStyles.Default => SwapStyle.Default,
Constants.SwapStyles.InnerHTML => SwapStyle.InnerHTML,
Constants.SwapStyles.OuterHTML => SwapStyle.OuterHTML,
Constants.SwapStyles.BeforeBegin => SwapStyle.BeforeBegin,
Constants.SwapStyles.AfterBegin => SwapStyle.AfterBegin,
Constants.SwapStyles.BeforeEnd => SwapStyle.BeforeEnd,
Constants.SwapStyles.AfterEnd => SwapStyle.AfterEnd,
Constants.SwapStyles.Delete => SwapStyle.Delete,
Constants.SwapStyles.None => SwapStyle.None,
_ => throw new SwitchExpressionException(value)
};

return style;
}

public override void Write(Utf8JsonWriter writer, SwapStyle value, JsonSerializerOptions options)
{
writer?.WriteStringValue(value.ToHtmxString());
}
}
2 changes: 1 addition & 1 deletion test/Htmxor.Tests/Configuration/HtmxHeadOutletTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public void HtmxConfig_serializer()
"attr2"
],
"defaultFocusScroll": true,
"defaultSwapStyle": "beforeBegin",
"defaultSwapStyle": "beforebegin",
"defaultSwapDelay": 60000,
"defaultSettleDelay": 3600000,
"disableSelector": "disable-selector",
Expand Down
Loading