mirror of
https://github.com/ckaczor/HomeMonitor.git
synced 2026-03-07 02:51:39 -05:00
Fix parsing of boolean (false) excerpts
This commit is contained in:
@@ -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;
|
||||
}
|
||||
30
Calendar/Service/StringOrBooleanConverter.cs
Normal file
30
Calendar/Service/StringOrBooleanConverter.cs
Normal 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);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user