处理日期数组

This commit is contained in:
qianhao.xu 2025-02-19 09:45:07 +08:00
parent 4aed6294ad
commit 90e940b2a1
2 changed files with 22 additions and 0 deletions

View File

@ -71,6 +71,7 @@ builder.Services.AddMvc(options =>
options.JsonSerializerOptions.WriteIndented = true;
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 StringConverter());
//PropertyNamingPolicy属性用于前端传过来的属性的格式策略目前内置的仅有一种策略CamelCase
options.JsonSerializerOptions.PropertyNamingPolicy = JsonNamingPolicy.CamelCase;

View File

@ -33,5 +33,26 @@ namespace Infrastructure.Converter
return dateVal;
return null;
}
//******************************DOAN****************************************
public class DateTimeArrayConverter : JsonConverter<DateTime>
{
public override DateTime Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
if (reader.TokenType == JsonTokenType.String)
{
if (DateTime.TryParse(reader.GetString(), out var date))
{
return date;
}
}
throw new JsonException("Invalid date format");
}
public override void Write(Utf8JsonWriter writer, DateTime value, JsonSerializerOptions options)
{
writer.WriteStringValue(value.ToString("yyyy-MM-ddTHH:mm:ss"));
}
}
}
}