12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576 |
- using Newtonsoft.Json.Serialization;
- using System.Text.Json;
- using System.Text.Json.Serialization;
- namespace OASystem.API.OAMethodLib
- {
- #region 日期格式转换
- public class DateTimeJsonConverter : System.Text.Json.Serialization.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);
- }
- }
- #endregion
- #region String null值转换(读/写)
- public class NullJsonConverter : System.Text.Json.Serialization.JsonConverter<string>
- {
- 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 String null值转换(读/写)
- //public class Int32JsonConverter : System.Text.Json.Serialization.JsonConverter<int?>
- //{
- // private readonly string Format;
- // public Int32JsonConverter(string format)
- // {
- // Format = format;
- // }
- // public override void Write(Utf8JsonWriter writer, int? value, JsonSerializerOptions options)
- // {
- // writer.WriteStringValue((value ?? 0).ToString());
- // }
- // public override int Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
- // {
- // return Convert.ToInt32(reader.GetString()) ?? 0;
- // }
- //}
- #endregion
- }
|