Files
Yi.Framework/Yi.Furion.Net6/Yi.Framework.Infrastructure/Data/Json/DateTimeJsonConverter.cs
2023-04-16 14:30:56 +08:00

30 lines
848 B
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.Json;
using System.Text.Json.Serialization;
using System.Threading.Tasks;
namespace Yi.Framework.Infrastructure.Data.Json
{
public class DateTimeJsonConverter : JsonConverter<DateTime>
{
private readonly string Format;
public DateTimeJsonConverter(string format)
{
Format = format;
}
public override void Write(Utf8JsonWriter writer, DateTime date, JsonSerializerOptions options)
{
writer.WriteStringValue(date.ToString(Format));
}
public override DateTime Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
return DateTime.ParseExact(reader.GetString(), Format, null);
}
}
}