mirror of
https://github.com/ckaczor/HomeMonitor.git
synced 2026-03-12 03:51:38 -04:00
Fix parsing of boolean (false) excerpts
This commit is contained in:
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