Fix parsing of boolean (false) excerpts

This commit is contained in:
2026-02-28 10:47:39 -05:00
parent ad70456a66
commit 8de424087f
2 changed files with 35 additions and 0 deletions

View File

@@ -1,4 +1,5 @@
using JetBrains.Annotations;
using System.Text.Json.Serialization;
namespace ChrisKaczor.HomeMonitor.Calendar.Service.Models.NationalDays;
@@ -6,7 +7,11 @@ namespace ChrisKaczor.HomeMonitor.Calendar.Service.Models.NationalDays;
public class Entry
{
public string Name { get; set; } = string.Empty;
public string Url { get; set; } = string.Empty;
[JsonConverter(typeof(StringOrBooleanConverter))]
public string Excerpt { get; set; } = string.Empty;
public string Type { get; set; } = string.Empty;
}

View File

@@ -0,0 +1,30 @@
using System.Text.Json;
using System.Text.Json.Serialization;
namespace ChrisKaczor.HomeMonitor.Calendar.Service;
public class StringOrBooleanConverter : JsonConverter<string>
{
public override string Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
return reader.TokenType switch
{
JsonTokenType.True => string.Empty,
JsonTokenType.False => string.Empty,
JsonTokenType.String => reader.GetString() ?? string.Empty,
_ => throw new JsonException($"Unexpected token type: {reader.TokenType}")
};
}
public override void Write(Utf8JsonWriter writer, string value, JsonSerializerOptions options)
{
if (bool.TryParse(value, out var boolValue))
{
writer.WriteBooleanValue(boolValue);
}
else
{
writer.WriteStringValue(value);
}
}
}