Explorar o código

移除返回数据对Null的过滤

jiangjc %!s(int64=2) %!d(string=hai) anos
pai
achega
975fefc7ff

+ 0 - 22
OASystem/OASystem.Api/OAMethodLib/DateTimeJsonConverter.cs

@@ -1,22 +0,0 @@
-using System.Text.Json;
-using System.Text.Json.Serialization;
-
-namespace OASystem.API.OAMethodLib
-{
-    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);
-        }
-    }
-}

+ 87 - 0
OASystem/OASystem.Api/OAMethodLib/JsonConvertOverride.cs

@@ -0,0 +1,87 @@
+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 null值转空字符串 NewtonsoftJson
+
+    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
+    {
+        private readonly PropertyInfo _memberInfo;
+        /// <summary>
+        /// 构造函数
+        /// </summary>
+        /// <param name="memberInfo"></param>
+        public NullToEmptyStringValueProvider(PropertyInfo memberInfo)
+        {
+            _memberInfo = memberInfo;
+        }
+
+        /// <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;
+        }
+
+        /// <summary>
+        /// 设置Value
+        /// </summary>
+        /// <param name="target"></param>
+        /// <param name="value"></param>
+        public void SetValue(object target, object value)
+        {
+            _memberInfo.SetValue(target, value);
+        }
+    }
+
+    #endregion
+
+}

+ 2 - 2
OASystem/OASystem.Api/Program.cs

@@ -23,12 +23,12 @@ builder.Services.AddControllers()
     .AddJsonOptions(options =>
     {
         //空字段不响应Response
-        options.JsonSerializerOptions.DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull;
+        //options.JsonSerializerOptions.DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull;
+
         //时间格式化响应
         options.JsonSerializerOptions.Converters.Add(new DateTimeJsonConverter("yyyy-MM-dd HH:mm:ss"));
     });
 
-
 #region Cors
 
 builder.Services.AddCors(policy =>