Convert string to enum and string to boolean
parent
e40fe7e37f
commit
c51bdf1221
|
|
@ -0,0 +1,23 @@
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -1,3 +1,4 @@
|
||||||
|
using System.Text.Json.Serialization;
|
||||||
using Microsoft.AspNetCore.Cors.Infrastructure;
|
using Microsoft.AspNetCore.Cors.Infrastructure;
|
||||||
using Microsoft.Extensions.Options;
|
using Microsoft.Extensions.Options;
|
||||||
using OFBButte.Application.Configuration;
|
using OFBButte.Application.Configuration;
|
||||||
|
|
@ -21,6 +22,11 @@ builder.Services.AddCors(options =>
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
builder.Services.ConfigureHttpJsonOptions(options => {
|
||||||
|
options.SerializerOptions.Converters.Add(new JsonStringEnumConverter());
|
||||||
|
options.SerializerOptions.Converters.Add(new JsonStringBooleanConverter());
|
||||||
|
});
|
||||||
|
|
||||||
// App Settings
|
// App Settings
|
||||||
var appSettings = builder.Configuration.GetSection("App").Get<AppSettings>();
|
var appSettings = builder.Configuration.GetSection("App").Get<AppSettings>();
|
||||||
builder.Services.AddSingleton(appSettings);
|
builder.Services.AddSingleton(appSettings);
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue