lith 1 anno fa
parent
commit
07f8601019

+ 6 - 6
dotnet/Library/Vit/Vit.Core/Test/Vit.Core.MsTest/Extensions/TypeExtensionsTest.cs

@@ -12,7 +12,7 @@ namespace Vit.Core.MsTest.Extensions
     {
         [TestMethod]
         public void GetUnderlyingTypeIfNullable_Test()
-        {   
+        {
 
             Assert.AreEqual(typeof(int?).GetUnderlyingTypeIfNullable(), typeof(int));
 
@@ -29,16 +29,16 @@ namespace Vit.Core.MsTest.Extensions
         [TestMethod]
         public void Convert_Test()
         {
-           
+
             #region (x.1)string
-            
+
             int int1 = "5".Convert<int>();
             Assert.AreEqual(int1, 5);
 
             int int2 = (int)("5.1".Convert<float>());
             Assert.AreEqual(int2, 5);
 
-            int int3 =  "5.1".Convert<float>().Convert<int>();
+            int int3 = "5.1".Convert<float>().Convert<int>();
             Assert.AreEqual(int3, 5);
 
             try
@@ -47,10 +47,10 @@ namespace Vit.Core.MsTest.Extensions
                 //将会抛异常           
                 "5.1".Convert<int?>();
 
-                "5.1".Convert<int>();             
+                "5.1".Convert<int>();
             }
             catch (Exception ex)
-            {                
+            {
             }
 
 

+ 21 - 9
dotnet/Library/Vit/Vit.Core/Test/Vit.Core.MsTest/Module/SerializationTest.cs

@@ -1,4 +1,5 @@
 using System;
+using System.Reflection;
 
 using Microsoft.VisualStudio.TestTools.UnitTesting;
 
@@ -17,16 +18,16 @@ namespace Vit.Core.MsTest.Module
         {
             public int? id;
             public string name;
-            public DateTime time;         
+            public DateTime time;
         }
-        
+
 
         [TestMethod]
         public void TestMethod()
         {
-            string testString = "testString中文12—=¥$《》<> \n\r\t😀"+ DateTime.Now;
+            string testString = "testString中文12—=¥$《》<> \n\r\t😀" + DateTime.Now;
 
-            var modelA = new ModelA { id=1,name = testString, time=DateTime.Now };
+            var modelA = new ModelA { id = 1, name = testString, time = DateTime.Now };
 
             #region (x.1)bytes <--> String      
             Assert.AreEqual(testString.StringToBytes().BytesToString(), testString);
@@ -43,25 +44,26 @@ namespace Vit.Core.MsTest.Module
             Assert.AreEqual(Json.DeserializeFromBytes<ModelA>(Json.SerializeToBytes(modelA))?.name, testString);
             Assert.AreEqual(modelA.SerializeToBytes().DeserializeFromBytes<ModelA>()?.name, testString);
             #endregion
- 
+
 
             #region (x.5)ConvertBySerialize
             var obj_ori = new { id = 1, name = testString, time = DateTime.Now };
- 
+
             Assert.AreEqual(obj_ori.ConvertBySerialize<ModelA>()?.name, testString);
             #endregion
 
 
             #region (x.6)DateTimeFormat           
 
-            var obj = new {
+            var obj = new
+            {
                 Date = DateTime.Parse("2019-01-01 01:01:01"),
-                obj =new { Date2 = DateTime.Parse("2019-02-02 01:01:01") }
+                obj = new { Date2 = DateTime.Parse("2019-02-02 01:01:01") }
             };
 
             string str = obj.Serialize();
 
-            Serialization_Newtonsoft.Instance.serializeSetting.DateFormatString="yyyy-MM-dd";
+            Serialization_Newtonsoft.Instance.serializeSetting.DateFormatString = "yyyy-MM-dd";
 
             string str2 = obj.Serialize();
             var jtObj = str2.Deserialize<JObject>();
@@ -76,5 +78,15 @@ namespace Vit.Core.MsTest.Module
         }
 
 
+
+        [TestMethod]
+        public void TestMethod_DateTime()
+        {
+            var time = DateTime.Now;
+            var str = Json.SerializeToString(time);
+            Assert.AreEqual(time.ToString("yyyy-MM-dd HH:mm:ss"), str);
+        }
+
+
     }
 }

+ 3 - 3
dotnet/Library/Vit/Vit.Core/Vit.Core/Extensions/Json/TypeExtensions.cs

@@ -59,14 +59,15 @@ namespace Vit.Extensions.Json_Extensions
         #region GetUnderlyingTypeIfNullable
 
         /// <summary>
-        /// 若为Nullable类型(例如 long?)则获取对应的值类型(例如long),否则返回自身。
+        /// If it is NULLABLE, then get the underlying type. eg if "Nullable&lt;long&gt;" then this will return just "long"
         /// </summary>
         /// <param name="type"></param>
         /// <returns></returns>
         [MethodImpl(MethodImplOptions.AggressiveInlining)]
         public static Type GetUnderlyingTypeIfNullable(this Type type)
         {
-            return type.IsNullable() ? type.GetGenericArguments()[0] : type;
+            //return type.IsNullable() ? type.GetGenericArguments()[0] : type;
+            return type.IsNullable() ? Nullable.GetUnderlyingType(type) : type;
 
 
             //if (type == null)
@@ -77,7 +78,6 @@ namespace Vit.Extensions.Json_Extensions
             ////Nullable.GetUnderlyingType(type);
 
             //// We need to check whether the property is NULLABLE
-            //// If it is NULLABLE, then get the underlying type. eg if "Nullable<long>" then this will return just "long"
             //return (type.IsGenericType && type.GetGenericTypeDefinition() == typeof(Nullable<>)) ? type.GetGenericArguments()[0] : type;
         }
         #endregion

+ 1 - 1
dotnet/Library/Vit/Vit.Core/Vit.Core/Module/Serialization/ISerialization.cs

@@ -28,7 +28,7 @@ namespace Vit.Core.Module.Serialization
         /// <summary>
         /// 使用Newtonsoft反序列化。T也可为值类型(例如 int?、bool) 
         /// </summary>
-        /// <param name="value"></param>    
+        /// <param name="value"></param>
         /// <returns></returns>
         T DeserializeFromString<T>(string value);
 

+ 19 - 14
dotnet/Library/Vit/Vit.Core/Vit.Core/Module/Serialization/Serialization_Newtonsoft.cs

@@ -9,7 +9,7 @@ using Vit.Extensions.Newtonsoft_Extensions;
 
 namespace Vit.Core.Module.Serialization
 {
-    public class Serialization_Newtonsoft: ISerialization
+    public class Serialization_Newtonsoft : ISerialization
     {
         #region defaultEncoding
         public static Encoding defaultEncoding { get; set; } =
@@ -20,12 +20,12 @@ namespace Vit.Core.Module.Serialization
 
 
         public static readonly Serialization_Newtonsoft Instance = new Serialization_Newtonsoft();
-        
+
 
 
         #region 基础对象
 
-      
+
 
         #region 成员对象 
 
@@ -65,7 +65,7 @@ namespace Vit.Core.Module.Serialization
 
 
         [MethodImpl(MethodImplOptions.AggressiveInlining)]
-        public  byte[] StringToBytes(string data, Encoding encoding = null)
+        public byte[] StringToBytes(string data, Encoding encoding = null)
         {
             return (encoding ?? this.encoding).GetBytes(data);
         }
@@ -119,7 +119,7 @@ namespace Vit.Core.Module.Serialization
 
         public readonly Newtonsoft.Json.JsonSerializerSettings serializeSetting = new Newtonsoft.Json.JsonSerializerSettings();
 
-        public Serialization_Newtonsoft() 
+        public Serialization_Newtonsoft()
         {
 
             //忽略空值
@@ -154,8 +154,8 @@ namespace Vit.Core.Module.Serialization
         public string SerializeToString<T>(T value)
         {
             if (null == value) return null;
-         
-            return SerializeToString(value, value.GetType()); 
+
+            return SerializeToString(value, value.GetType());
         }
 
         /// <summary>
@@ -170,12 +170,17 @@ namespace Vit.Core.Module.Serialization
             if (null == value) return null;
 
             if (value is Newtonsoft.Json.Linq.JToken token)
-            {             
-                if(token.TypeMatch(JTokenType.String)) return token.ToString();
+            {
+                if (token.TypeMatch(JTokenType.String)) return token.ToString();
 
                 return token.ToString(serializeSetting.Formatting);
             }
 
+            if (value is DateTime time)
+            {
+                return time.ToString(serializeSetting.DateFormatString);
+            }
+
             if (type.TypeIsValueTypeOrStringType())
             {
                 //return value.Convert<string>();
@@ -191,7 +196,7 @@ namespace Vit.Core.Module.Serialization
         /// <summary>
         /// 使用Newtonsoft反序列化。T也可为值类型(例如 int?、bool) 
         /// </summary>
-        /// <param name="value"></param>    
+        /// <param name="value"></param>
         /// <returns></returns>
         [MethodImpl(MethodImplOptions.AggressiveInlining)]
         public T DeserializeFromString<T>(string value)
@@ -256,9 +261,9 @@ namespace Vit.Core.Module.Serialization
         /// <param name="obj"></param>
         /// <returns></returns>
         [MethodImpl(MethodImplOptions.AggressiveInlining)]
-        public byte[] SerializeToBytes<T>(T obj) 
+        public byte[] SerializeToBytes<T>(T obj)
         {
-            return SerializeToBytes<T>(obj,null);
+            return SerializeToBytes<T>(obj, null);
         }
 
         /// <summary>
@@ -409,10 +414,10 @@ namespace Vit.Core.Module.Serialization
 
 
         #region DeserializeFromArraySegmentByte
- 
+
 
         [MethodImpl(MethodImplOptions.AggressiveInlining)]
-        public  T DeserializeFromArraySegmentByte<T>(ArraySegment<byte> bytes)
+        public T DeserializeFromArraySegmentByte<T>(ArraySegment<byte> bytes)
         {
             if (bytes.Count == 0) return default;
             return (T)DeserializeFromArraySegmentByte(bytes, typeof(T));