using System; using System.Text.Json; using System.Text.Json.Serialization; namespace Win.Utils { public class DatetimeJsonConverter : JsonConverter { 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; } } return reader.GetDateTime(); } public override void Write(Utf8JsonWriter writer, DateTime value, JsonSerializerOptions options) { if (value.Hour == 0 && value.Minute == 0 && value.Second == 0) { writer.WriteStringValue(value.ToString("yyyy-MM-dd")); } else { writer.WriteStringValue(value.ToString("yyyy-MM-dd HH:mm:ss")); } } } }