ofbapi/OFBButte.Api/JsonStringBooleanConverter.cs

24 lines
628 B
C#

using System.Text.Json;
using System.Text.Json.Serialization;
public class JsonStringBooleanConverter : JsonConverter<bool>
{
public override bool Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
var val = reader.GetString();
if (val == "true")
return true;
if (val == "false")
return false;
throw new Exception($"Cannot convert {val} to boolean");
}
public override void Write(Utf8JsonWriter writer, bool value, JsonSerializerOptions options)
{
writer.WriteBooleanValue(value);
}
}