Skip to content
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
11 changes: 3 additions & 8 deletions src/Ramstack.HtmxToolkit/HtmxBinaryTypeJsonConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,8 @@ internal sealed class HtmxBinaryTypeJsonConverter : JsonConverter<HtmxBinaryType
/// <inheritdoc />
public override void Write(Utf8JsonWriter writer, HtmxBinaryType? value, JsonSerializerOptions options)
{
if (value is null)
{
writer.WriteNullValue();
}
else
{
writer.WriteStringValue(value.GetValueOrDefault().GetWsBinaryTypeValue());
}
// NOTE: value is never null here: null-valued properties are omitted
// by JsonIgnoreCondition.WhenWritingNull before this converter is invoked.
writer.WriteStringValue(value.GetValueOrDefault().GetWsBinaryTypeValue());
}
}
33 changes: 30 additions & 3 deletions src/Ramstack.HtmxToolkit/HtmxConfig.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
using System.Text.Json.Serialization;

using Microsoft.AspNetCore.Html;

namespace Ramstack.HtmxToolkit;

/// <summary>
/// Represents configuration for a specific major version of HTMX.
/// </summary>
public abstract class HtmxConfig
{
private HtmlString? _json;

/// <summary>
/// Gets the configured HTMX major version.
/// </summary>
Expand All @@ -23,10 +27,33 @@ internal HtmxConfig(HtmxTargetVersion version) =>
TargetVersion = version;

/// <summary>
/// Serializes this configuration to JSON.
/// Returns this configuration serialized as JSON,
/// cached and reused until the configuration changes.
/// </summary>
/// <returns>
/// An <see cref="HtmlString" /> containing the configuration serialized as JSON.
/// </returns>
internal HtmlString ToJson() =>
_json ??= new HtmlString(Serialize());

/// <summary>
/// Serializes this configuration to a JSON string.
/// </summary>
/// <returns>
/// A string containing this configuration serialized as JSON.
/// A JSON string representing this configuration.
/// </returns>
internal abstract string ToJson();
protected abstract string Serialize();

/// <summary>
/// Assigns <paramref name="value" /> to <paramref name="field" /> and invalidates
/// the cached JSON so it is regenerated on the next serialization.
/// </summary>
/// <typeparam name="T">The type of the field.</typeparam>
/// <param name="field">The backing field to update.</param>
/// <param name="value">The value to assign.</param>
protected void SetField<T>(ref T field, T value)
{
field = value;
_json = null;
}
}
11 changes: 3 additions & 8 deletions src/Ramstack.HtmxToolkit/HtmxFetchModeJsonConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,8 @@ internal sealed class HtmxFetchModeJsonConverter : JsonConverter<HtmxFetchMode?>
/// <inheritdoc />
public override void Write(Utf8JsonWriter writer, HtmxFetchMode? value, JsonSerializerOptions options)
{
if (value is null)
{
writer.WriteNullValue();
}
else
{
writer.WriteStringValue(value.GetValueOrDefault().GetFetchModeValue());
}
// NOTE: value is never null here: null-valued properties are omitted
// by JsonIgnoreCondition.WhenWritingNull before this converter is invoked.
writer.WriteStringValue(value.GetValueOrDefault().GetFetchModeValue());
}
}
29 changes: 12 additions & 17 deletions src/Ramstack.HtmxToolkit/HtmxHistoryModeJsonConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,24 +21,19 @@ internal sealed class HtmxHistoryModeJsonConverter : JsonConverter<HtmxHistoryMo
/// <inheritdoc />
public override void Write(Utf8JsonWriter writer, HtmxHistoryMode? value, JsonSerializerOptions options)
{
if (value is null)
// NOTE: value is never null here: null-valued properties are omitted
// by JsonIgnoreCondition.WhenWritingNull before this converter is invoked.
switch (value.GetValueOrDefault())
{
writer.WriteNullValue();
}
else
{
switch (value.GetValueOrDefault())
{
case HtmxHistoryMode.Enabled:
writer.WriteBooleanValue(true);
break;
case HtmxHistoryMode.Disabled:
writer.WriteBooleanValue(false);
break;
default:
writer.WriteStringValue(s_reload);
break;
}
case HtmxHistoryMode.Enabled:
writer.WriteBooleanValue(true);
break;
case HtmxHistoryMode.Disabled:
writer.WriteBooleanValue(false);
break;
default:
writer.WriteStringValue(s_reload);
break;
}
}
}
11 changes: 3 additions & 8 deletions src/Ramstack.HtmxToolkit/HtmxScrollBehaviorJsonConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,8 @@ internal sealed class HtmxScrollBehaviorJsonConverter : JsonConverter<HtmxScroll
/// <inheritdoc />
public override void Write(Utf8JsonWriter writer, HtmxScrollBehavior? value, JsonSerializerOptions options)
{
if (value is null)
{
writer.WriteNullValue();
}
else
{
writer.WriteStringValue(value.GetValueOrDefault().GetScrollBehaviorValue());
}
// NOTE: value is never null here: null-valued properties are omitted
// by JsonIgnoreCondition.WhenWritingNull before this converter is invoked.
writer.WriteStringValue(value.GetValueOrDefault().GetScrollBehaviorValue());
}
}
11 changes: 3 additions & 8 deletions src/Ramstack.HtmxToolkit/HtmxSwapJsonConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,8 @@ internal sealed class HtmxSwapJsonConverter : JsonConverter<HtmxSwap?>
/// <inheritdoc />
public override void Write(Utf8JsonWriter writer, HtmxSwap? value, JsonSerializerOptions options)
{
if (value is null)
{
writer.WriteNullValue();
}
else
{
writer.WriteStringValue(value.GetValueOrDefault().GetSwapValue());
}
// NOTE: value is never null here: null-valued properties are omitted
// by JsonIgnoreCondition.WhenWritingNull before this converter is invoked.
writer.WriteStringValue(value.GetValueOrDefault().GetSwapValue());
}
}
Loading
Loading