处理日期
This commit is contained in:
@@ -13,6 +13,7 @@ using DOAN.Infrastructure.WebExtensions;
|
||||
using DOAN.ServiceCore.Signalr;
|
||||
using DOAN.ServiceCore.SqlSugar;
|
||||
using Microsoft.Extensions.FileProviders;
|
||||
using static Infrastructure.Converter.JsonConverterUtil;
|
||||
|
||||
var builder = WebApplication.CreateBuilder(args);
|
||||
// NLog: Setup NLog for Dependency injection
|
||||
@@ -72,6 +73,7 @@ builder.Services.AddMvc(options =>
|
||||
options.JsonSerializerOptions.Converters.Add(new JsonConverterUtil.DateTimeConverter());
|
||||
options.JsonSerializerOptions.Converters.Add(new JsonConverterUtil.DateTimeNullConverter());
|
||||
options.JsonSerializerOptions.Converters.Add(new JsonConverterUtil.DateTimeArrayConverter());
|
||||
options.JsonSerializerOptions.Converters.Add(new JsonConverterUtil.DateTimeNullableArrayConverter());
|
||||
options.JsonSerializerOptions.Converters.Add(new StringConverter());
|
||||
//PropertyNamingPolicy属性用于前端传过来的属性的格式策略,目前内置的仅有一种策略CamelCase
|
||||
options.JsonSerializerOptions.PropertyNamingPolicy = JsonNamingPolicy.CamelCase;
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text.Json;
|
||||
using System.Text.Json.Serialization;
|
||||
|
||||
@@ -6,6 +7,9 @@ namespace Infrastructure.Converter
|
||||
{
|
||||
public class JsonConverterUtil
|
||||
{
|
||||
/// <summary>
|
||||
/// 你提供的转换器是针对单个 DateTime 或 DateTime? 的,而日期数组是一个集合类型(如 DateTime[] 或 List<DateTime>)。JSON 转换器需要明确知道如何处理集合中的每个元素。
|
||||
/// </summary>
|
||||
public class DateTimeNullConverter : JsonConverter<DateTime?>
|
||||
{
|
||||
public override DateTime? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
|
||||
@@ -34,7 +38,7 @@ namespace Infrastructure.Converter
|
||||
return null;
|
||||
}
|
||||
|
||||
//******************************DOAN****************************************
|
||||
//******************************DOAN JSON 转换器处理日期数组****************************************
|
||||
public class DateTimeArrayConverter : JsonConverter<DateTime>
|
||||
{
|
||||
public override DateTime Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
|
||||
@@ -54,5 +58,66 @@ namespace Infrastructure.Converter
|
||||
writer.WriteStringValue(value.ToString("yyyy-MM-ddTHH:mm:ss"));
|
||||
}
|
||||
}
|
||||
|
||||
public class DateTimeNullableArrayConverter : JsonConverter<DateTime?[]>
|
||||
{
|
||||
public override DateTime?[] Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
|
||||
{
|
||||
if (reader.TokenType != JsonTokenType.StartArray)
|
||||
{
|
||||
throw new JsonException("Expected start of array.");
|
||||
}
|
||||
|
||||
var dates = new List<DateTime?>();
|
||||
|
||||
while (reader.Read())
|
||||
{
|
||||
if (reader.TokenType == JsonTokenType.EndArray)
|
||||
{
|
||||
return dates.ToArray();
|
||||
}
|
||||
|
||||
if (reader.TokenType == JsonTokenType.String)
|
||||
{
|
||||
var dateString = reader.GetString();
|
||||
if (string.IsNullOrEmpty(dateString))
|
||||
{
|
||||
dates.Add(null);
|
||||
}
|
||||
else if (DateTime.TryParse(dateString, out var date))
|
||||
{
|
||||
dates.Add(date);
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new JsonException($"Invalid date format: {dateString}");
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new JsonException("Expected string value in array.");
|
||||
}
|
||||
}
|
||||
|
||||
throw new JsonException("Unexpected end of JSON.");
|
||||
}
|
||||
|
||||
public override void Write(Utf8JsonWriter writer, DateTime?[] value, JsonSerializerOptions options)
|
||||
{
|
||||
writer.WriteStartArray();
|
||||
foreach (var date in value)
|
||||
{
|
||||
if (date.HasValue)
|
||||
{
|
||||
writer.WriteStringValue(date.Value.ToString("yyyy-MM-dd HH:mm:ss"));
|
||||
}
|
||||
else
|
||||
{
|
||||
writer.WriteNullValue();
|
||||
}
|
||||
}
|
||||
writer.WriteEndArray();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user