From 58b0ae6a80964416297081cbf7844bc9cbe302a5 Mon Sep 17 00:00:00 2001 From: ngfrolov Date: Tue, 31 May 2022 12:30:03 +0500 Subject: [PATCH] Add DateOnly, TimeOnly support --- AsbCloudWebApi/AsbCloudWebApi.csproj | 1 + .../Converters/TimeOnlyTypeConverter.cs | 46 +++++++++++++++++++ AsbCloudWebApi/Startup.cs | 12 +++-- 3 files changed, 54 insertions(+), 5 deletions(-) create mode 100644 AsbCloudWebApi/Converters/TimeOnlyTypeConverter.cs diff --git a/AsbCloudWebApi/AsbCloudWebApi.csproj b/AsbCloudWebApi/AsbCloudWebApi.csproj index 92304360..f416f4ac 100644 --- a/AsbCloudWebApi/AsbCloudWebApi.csproj +++ b/AsbCloudWebApi/AsbCloudWebApi.csproj @@ -8,6 +8,7 @@ + diff --git a/AsbCloudWebApi/Converters/TimeOnlyTypeConverter.cs b/AsbCloudWebApi/Converters/TimeOnlyTypeConverter.cs new file mode 100644 index 00000000..484d2f3e --- /dev/null +++ b/AsbCloudWebApi/Converters/TimeOnlyTypeConverter.cs @@ -0,0 +1,46 @@ +using System; +using System.ComponentModel; +using System.Globalization; + +namespace AsbCloudWebApi.Converters +{ +#nullable enable + public class DateOnlyTypeConverter : TypeConverter + { + public override bool CanConvertFrom(ITypeDescriptorContext? context, Type sourceType) + { + if (sourceType == typeof(string)) + { + return true; + } + return base.CanConvertFrom(context, sourceType); + } + + public override object? ConvertFrom(ITypeDescriptorContext? context, CultureInfo? culture, object value) + { + if (value is string str) + { + return TimeOnly.Parse(str); + } + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(ITypeDescriptorContext? context, Type? destinationType) + { + if (destinationType == typeof(string)) + { + return true; + } + return base.CanConvertTo(context, destinationType); + } + public override object? ConvertTo(ITypeDescriptorContext? context, CultureInfo? culture, object? value, Type destinationType) + { + if (destinationType == typeof(string) && value is TimeOnly date) + { + return date.ToString("O"); + } + return base.ConvertTo(context, culture, value, destinationType); + } + } +#nullable disable +} diff --git a/AsbCloudWebApi/Startup.cs b/AsbCloudWebApi/Startup.cs index d65805e0..ce8ac383 100644 --- a/AsbCloudWebApi/Startup.cs +++ b/AsbCloudWebApi/Startup.cs @@ -19,17 +19,19 @@ namespace AsbCloudWebApi public void ConfigureServices(IServiceCollection services) { - services.AddControllers() - .AddJsonOptions(new System.Action(opts => + services.AddControllers(options => options.UseDateOnlyTimeOnlyStringConverters()) + .AddJsonOptions(new System.Action(options => { - opts.JsonSerializerOptions.NumberHandling = System.Text.Json.Serialization.JsonNumberHandling.AllowNamedFloatingPointLiterals | System.Text.Json.Serialization.JsonNumberHandling.AllowReadingFromString; + options.JsonSerializerOptions.NumberHandling = System.Text.Json.Serialization.JsonNumberHandling.AllowNamedFloatingPointLiterals | System.Text.Json.Serialization.JsonNumberHandling.AllowReadingFromString; + options.UseDateOnlyTimeOnlyStringConverters(); })) - .AddProtoBufNet(); //adds mediaType "application/protobuf" + .AddProtoBufNet() + ; //adds mediaType "application/protobuf" ProtobufModel.EnshureRegistered(); services.AddSwagger(); - + services.AddInfrastructure(Configuration); services.AddJWTAuthentication();