浏览代码

Merge branch 'develop' of http://132.232.92.186:3000/XinXiBu/OA2023 into develop

leiy 2 年之前
父节点
当前提交
fcb4144ba4
共有 2 个文件被更改,包括 36 次插入6 次删除
  1. 30 1
      OASystem/OASystem.Api/OAMethodLib/DateTimeJsonConverter.cs
  2. 6 5
      OASystem/OASystem.Api/Program.cs

+ 30 - 1
OASystem/OASystem.Api/OAMethodLib/DateTimeJsonConverter.cs

@@ -1,8 +1,12 @@
-using System.Text.Json;
+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;
@@ -19,4 +23,29 @@ namespace OASystem.API.OAMethodLib
             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
+
 }

+ 6 - 5
OASystem/OASystem.Api/Program.cs

@@ -23,12 +23,13 @@ builder.Services.AddControllers()
     .AddJsonOptions(options =>
     {
         //空字段不响应Response
-        options.JsonSerializerOptions.DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull;
+        //options.JsonSerializerOptions.DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull;
+
+        options.JsonSerializerOptions.Converters.Add(new NullJsonConverter());
         //时间格式化响应
         options.JsonSerializerOptions.Converters.Add(new DateTimeJsonConverter("yyyy-MM-dd HH:mm:ss"));
     });
 
-
 #region Cors
 
 builder.Services.AddCors(policy =>
@@ -55,9 +56,9 @@ builder.Services.AddScoped(options =>
 {
     return new SqlSugarClient(new List<ConnectionConfig>()
     {
-        new ConnectionConfig() { 
-            ConfigId = DBEnum.OA2023DB, 
-            ConnectionString = _config.GetConnectionString("OA2023DB"), 
+        new ConnectionConfig() {
+            ConfigId = DBEnum.OA2023DB,
+            ConnectionString = _config.GetConnectionString("OA2023DB"),
             DbType = DbType.SqlServer, IsAutoCloseConnection = true }
     });
 });