Forráskód Böngészése

发送给客户端的Json String类型的null值转换为空字符串“”

jiangjc 2 éve%!(EXTRA string=óta)
szülő
commit
a7d2c8ea3b

+ 14 - 50
OASystem/OASystem.Api/OAMethodLib/JsonConvertOverride.cs

@@ -25,61 +25,25 @@ namespace OASystem.API.OAMethodLib
     }
     #endregion
 
-    #region null值转空字符串 NewtonsoftJson
+    #region String null值转换(读/写)
 
-    public class NullToEmptyStringResolver : CamelCasePropertyNamesContractResolver
-    {
-        /// <summary>
-        /// 创建属性
-        /// </summary>
-        /// <param name="type">类型</param>
-        /// <param name="memberSerialization">序列化成员</param>
-        /// <returns></returns>
-        protected override IList<Newtonsoft.Json.Serialization.JsonProperty> CreateProperties(Type type, MemberSerialization memberSerialization)
-        {
-            return type.GetProperties().Select(c =>
-            {
-                var jsonProperty = base.CreateProperty(c, memberSerialization);
-                jsonProperty.ValueProvider = new NullToEmptyStringValueProvider(c);
-                return jsonProperty;
-            }).ToList();
-        }
-    }
 
-    public class NullToEmptyStringValueProvider : IValueProvider
+    public class NullJsonConverter : System.Text.Json.Serialization.JsonConverter<string>
     {
-        private readonly PropertyInfo _memberInfo;
-        /// <summary>
-        /// 构造函数
-        /// </summary>
-        /// <param name="memberInfo"></param>
-        public NullToEmptyStringValueProvider(PropertyInfo memberInfo)
-        {
-            _memberInfo = memberInfo;
-        }
+        public override bool HandleNull => true;
 
-        /// <summary>
-        /// 获取Value
-        /// </summary>
-        /// <param name="target"></param>
-        /// <returns></returns>
-        public object GetValue(object target)
-        {
-            var result = _memberInfo.GetValue(target);
-            if (_memberInfo.PropertyType == typeof(string) && result == null)
-                result = string.Empty;
-            return result;
-        }
+        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 ?? "");
 
-        /// <summary>
-        /// 设置Value
-        /// </summary>
-        /// <param name="target"></param>
-        /// <param name="value"></param>
-        public void SetValue(object target, object value)
-        {
-            _memberInfo.SetValue(target, value);
-        }
     }
 
     #endregion

+ 4 - 3
OASystem/OASystem.Api/Program.cs

@@ -25,6 +25,7 @@ builder.Services.AddControllers()
         //空字段不响应Response
         //options.JsonSerializerOptions.DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull;
 
+        options.JsonSerializerOptions.Converters.Add(new NullJsonConverter());
         //时间格式化响应
         options.JsonSerializerOptions.Converters.Add(new DateTimeJsonConverter("yyyy-MM-dd HH:mm:ss"));
     });
@@ -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 }
     });
 });