using Newtonsoft.Json; using System.Text.Json; using System.Text.Json.Serialization; namespace OASystem.API.OAMethodLib { #region 日期格式转换 public class DateTimeJsonConverter : System.Text.Json.Serialization.JsonConverter { private readonly string Format; public DateTimeJsonConverter(string format) { Format = format; } public override DateTime Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) { if (string.IsNullOrEmpty(reader.GetString())) { return default(DateTime); } return DateTime.ParseExact(reader.GetString(), Format, null); } 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); //} } #endregion #region String null值转换(读/写) public class NullJsonConverter : System.Text.Json.Serialization.JsonConverter { public override bool HandleNull => true; public override string Read( ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) => reader.GetString() ?? ""; public override void Write( Utf8JsonWriter writer, string value, JsonSerializerOptions options) => writer.WriteStringValue(value ?? ""); } #endregion #region decimal格式化 #endregion }