Bladeren bron

fix Serialization_Text serialize Type issue (if type is object will not serialize any properties)
and refactoring ISerializztion

Lith 1 jaar geleden
bovenliggende
commit
8938b4ee63
25 gewijzigde bestanden met toevoegingen van 654 en 552 verwijderingen
  1. 4 0
      dotnet/.editorconfig
  2. 2 144
      dotnet/Library/Sers/Sers.Core/Sers.Core/Module/Api/RouteMap/GenericRouteMap.cs
  3. 144 0
      dotnet/Library/Sers/Sers.Core/Sers.Core/Module/Api/RouteMap/Tree.cs
  4. 23 39
      dotnet/Library/Sers/Sers.Core/Sers.Core/Module/Serialization/Text/Serialization_Text.cs
  5. 1 3
      dotnet/Library/Sers/Sers.Core/Test/Sers.Core.Module.LocalApi.Qps/LocalApi/LocalApiTest.cs
  6. 7 5
      dotnet/Library/Sers/Sers.Core/Test/Sers.Core.Module.Rpc.MsTest/UnitTest1.cs
  7. 0 1
      dotnet/Library/Sers/Sers.Core/Test/Sers.Core.Module.Serialization.Qps/Program.cs
  8. 126 0
      dotnet/Library/Sers/Sers.Core/Test/Sers.Core.MsTest/Module/Serialization_Text_Test.cs
  9. 17 14
      dotnet/Library/Sers/Sers.Core/Test/Sers.Core.Util.Consumer.Test/Queue/Program_ConcurrentLinkedQueue.cs
  10. 22 19
      dotnet/Library/Sers/Sers.Core/Test/Sers.Core.Util.Consumer.Test/Queue/Program_QueueQps.cs
  11. 1 8
      dotnet/Library/Sers/Sers.Hardware/Net46/Sers.Hardware.Net46.Exe/Program.cs
  12. 3 4
      dotnet/Library/Vit/Vit.Core/Test/Vit.Core.MsTest/Extensions/TypeExtensionsTest.cs
  13. 0 98
      dotnet/Library/Vit/Vit.Core/Test/Vit.Core.MsTest/Module/SerializationTest.cs
  14. 50 0
      dotnet/Library/Vit/Vit.Core/Test/Vit.Core.MsTest/Module/Serialization_Extensions_Test.cs
  15. 135 0
      dotnet/Library/Vit/Vit.Core/Test/Vit.Core.MsTest/Module/Serialization_Newtonsoft_Test.cs
  16. 10 11
      dotnet/Library/Vit/Vit.Core/Vit.Core/Extensions/Newtonsoft/JTokenExtensions.cs
  17. 1 3
      dotnet/Library/Vit/Vit.Core/Vit.Core/Module/Log/Logger.cs
  18. 6 7
      dotnet/Library/Vit/Vit.Core/Vit.Core/Module/Serialization/Extensions/Object_Serialize_Extensions.cs
  19. 16 36
      dotnet/Library/Vit/Vit.Core/Vit.Core/Module/Serialization/ISerialization.cs
  20. 13 52
      dotnet/Library/Vit/Vit.Core/Vit.Core/Module/Serialization/Json.cs
  21. 65 99
      dotnet/Library/Vit/Vit.Core/Vit.Core/Module/Serialization/Serialization_Newtonsoft.cs
  22. 1 3
      dotnet/Library/Vit/Vit.Core/Vit.Core/Util/Shell/OsShell.cs
  23. 1 3
      dotnet/Library/Vit/Vit.Core/Vit.Core/Util/Shell/ShellStream.cs
  24. 0 2
      dotnet/Library/Vit/Vit.Core/Vit.Core/Util/XmlComment/XmlCommentHelp.cs
  25. 6 1
      dotnet/Sers.sln

+ 4 - 0
dotnet/.editorconfig

@@ -0,0 +1,4 @@
+[*.cs]
+
+# CS1591: 缺少对公共可见类型或成员的 XML 注释
+dotnet_diagnostic.CS1591.severity = none

+ 2 - 144
dotnet/Library/Sers/Sers.Core/Sers.Core/Module/Api/RouteMap/GenericRouteMap.cs

@@ -8,11 +8,11 @@ namespace Sers.Core.Module.Api.RouteMap
          where T : class
     {
 
-       
+
 
         Tree<T> tree = new Tree<T>();
 
- 
+
 
         /// <summary>
         /// path demo:  "/station1/fold2/*"
@@ -59,147 +59,5 @@ namespace Sers.Core.Module.Api.RouteMap
             return tree.QueryByPath(route);
         }
 
-        
-
-
-
-
-        #region class Tree<T>
-
-
-        public class Tree<T>
-            where T : class
-        {
-            string key;
-            public T data;
-
-
-            SortedDictionary<string, Tree<T>> children = new SortedDictionary<string, Tree<T>>();
-
-            public Tree<T> parent { get; private set; }
-
-
-            /// <summary>
-            /// path demo:  "/station1/fold2/api1"
-            /// </summary>
-            /// <param name="path"></param>
-            /// <returns></returns>
-            [MethodImpl(MethodImplOptions.AggressiveInlining)]
-            public Tree<T> BuildPath(string path)
-            {
-                if (string.IsNullOrWhiteSpace(path)) return this;
-                return BuildPath(path.Split('/'), 1);
-
-            }
-
-            [MethodImpl(MethodImplOptions.AggressiveInlining)]
-            Tree<T> BuildPath(string[] path, int index)
-            {
-                if (path.Length <= index)
-                {
-                    return this;
-                }
-
-                if (!children.TryGetValue(path[index], out var tree))
-                {
-                    tree = new Tree<T> { key = path[index], parent = this };
-
-                    children.IDictionaryTryAdd(tree.key, tree);                                    
-                }
-                return tree.BuildPath(path, index + 1);
-            }
-
-            /// <summary>
-            /// path demo:  "/station1/fold2/api1/action2"
-            /// </summary>
-            /// <param name="path"></param>
-            /// <returns></returns>
-            [MethodImpl(MethodImplOptions.AggressiveInlining)]
-            public T QueryByPath(string path)
-            {
-                return QueryByPath(path.Split('/'), 1);
-            }
-
-            [MethodImpl(MethodImplOptions.AggressiveInlining)]
-            T QueryByPath(string[] path, int index)
-            {
-                if (path.Length <= index) return data;
-
-                if (children.TryGetValue(path[index], out var value))
-                {
-                    return value.QueryByPath(path, index + 1) ?? data;
-                }
-                return data;
-            }
-
-            /// <summary>
-            /// path demo:  "/station1/fold2/api1/action2"
-            /// </summary>
-            /// <param name="path"></param>
-            /// <returns></returns>
-            [MethodImpl(MethodImplOptions.AggressiveInlining)]
-            public Tree<T> GetChildren(string path)
-            {
-                return GetChildren(path.Split('/'), 1);
-            }
-
-            [MethodImpl(MethodImplOptions.AggressiveInlining)]
-            Tree<T> GetChildren(string[] path, int index)
-            {
-                if (path.Length <= index) return this;
-
-                if (children.TryGetValue(path[index], out var value))
-                {
-                    return value.GetChildren(path, index + 1);
-                }
-                return null;
-            }
-
-
-            void TryRemove()
-            {
-                if (data == null)
-                {
-                    if (children.Count == 0 && parent != null)
-                    {
-                        parent.children.Remove(key);
-                        parent.TryRemove();
-                    }
-                }
-            }
-
-            /// <summary>
-            ///  path demo:  "/station1/fold2/api1/action2"
-            /// </summary>
-            /// <param name="path"></param>
-            /// <returns></returns>
-            public T Remove(string path)
-            {
-                T data = null;
-
-                var cur = GetChildren(path);
-                if (null != cur)
-                {
-                    data = cur.data;
-                    cur.data = null;
-                    cur.TryRemove();
-                }
-                return data;
-            }
-
-            [MethodImpl(MethodImplOptions.AggressiveInlining)]
-            public void GetAllData(List<T> list)
-            {
-                if (data != null)
-                {
-                    list.Add(data);
-                }
-                foreach (var kv in children)
-                {
-                    kv.Value.GetAllData(list);
-                }
-            }
-        }
-        #endregion
     }
 }

+ 144 - 0
dotnet/Library/Sers/Sers.Core/Sers.Core/Module/Api/RouteMap/Tree.cs

@@ -0,0 +1,144 @@
+using System;
+using System.Collections.Generic;
+using System.Runtime.CompilerServices;
+using System.Text;
+
+using Vit.Extensions;
+
+namespace Sers.Core.Module.Api.RouteMap
+{
+    public class Tree<T>
+        where T : class
+    {
+        string key;
+        public T data;
+
+
+        SortedDictionary<string, Tree<T>> children = new SortedDictionary<string, Tree<T>>();
+
+        public Tree<T> parent { get; private set; }
+
+
+        /// <summary>
+        /// path demo:  "/station1/fold2/api1"
+        /// </summary>
+        /// <param name="path"></param>
+        /// <returns></returns>
+        [MethodImpl(MethodImplOptions.AggressiveInlining)]
+        public Tree<T> BuildPath(string path)
+        {
+            if (string.IsNullOrWhiteSpace(path)) return this;
+            return BuildPath(path.Split('/'), 1);
+
+        }
+
+        [MethodImpl(MethodImplOptions.AggressiveInlining)]
+        Tree<T> BuildPath(string[] path, int index)
+        {
+            if (path.Length <= index)
+            {
+                return this;
+            }
+
+            if (!children.TryGetValue(path[index], out var tree))
+            {
+                tree = new Tree<T> { key = path[index], parent = this };
+
+                children.IDictionaryTryAdd(tree.key, tree);
+            }
+            return tree.BuildPath(path, index + 1);
+        }
+
+        /// <summary>
+        /// path demo:  "/station1/fold2/api1/action2"
+        /// </summary>
+        /// <param name="path"></param>
+        /// <returns></returns>
+        [MethodImpl(MethodImplOptions.AggressiveInlining)]
+        public T QueryByPath(string path)
+        {
+            return QueryByPath(path.Split('/'), 1);
+        }
+
+        [MethodImpl(MethodImplOptions.AggressiveInlining)]
+        T QueryByPath(string[] path, int index)
+        {
+            if (path.Length <= index) return data;
+
+            if (children.TryGetValue(path[index], out var value))
+            {
+                return value.QueryByPath(path, index + 1) ?? data;
+            }
+            return data;
+        }
+
+        /// <summary>
+        /// path demo:  "/station1/fold2/api1/action2"
+        /// </summary>
+        /// <param name="path"></param>
+        /// <returns></returns>
+        [MethodImpl(MethodImplOptions.AggressiveInlining)]
+        public Tree<T> GetChildren(string path)
+        {
+            return GetChildren(path.Split('/'), 1);
+        }
+
+        [MethodImpl(MethodImplOptions.AggressiveInlining)]
+        Tree<T> GetChildren(string[] path, int index)
+        {
+            if (path.Length <= index) return this;
+
+            if (children.TryGetValue(path[index], out var value))
+            {
+                return value.GetChildren(path, index + 1);
+            }
+            return null;
+        }
+
+
+        void TryRemove()
+        {
+            if (data == null)
+            {
+                if (children.Count == 0 && parent != null)
+                {
+                    parent.children.Remove(key);
+                    parent.TryRemove();
+                }
+            }
+        }
+
+        /// <summary>
+        ///  path demo:  "/station1/fold2/api1/action2"
+        /// </summary>
+        /// <param name="path"></param>
+        /// <returns></returns>
+        public T Remove(string path)
+        {
+            T data = null;
+
+            var cur = GetChildren(path);
+            if (null != cur)
+            {
+                data = cur.data;
+                cur.data = null;
+                cur.TryRemove();
+            }
+            return data;
+        }
+
+        [MethodImpl(MethodImplOptions.AggressiveInlining)]
+        public void GetAllData(List<T> list)
+        {
+            if (data != null)
+            {
+                list.Add(data);
+            }
+            foreach (var kv in children)
+            {
+                kv.Value.GetAllData(list);
+            }
+        }
+    }
+
+}

+ 23 - 39
dotnet/Library/Sers/Sers.Core/Sers.Core/Module/Serialization/Text/Serialization_Text.cs

@@ -3,16 +3,20 @@ using System.Runtime.CompilerServices;
 using System.Text.Json;
 using System.Text.Unicode;
 
+using Newtonsoft.Json.Linq;
+
 using Vit.Core.Module.Serialization;
 using Vit.Core.Util.ConfigurationManager;
 using Vit.Extensions;
+using Vit.Extensions.Json_Extensions;
+using Vit.Extensions.Object_Serialize_Extensions;
 
 namespace Sers.Core.Module.Serialization.Text
 {
     /// <summary>
     ///  https://github.com/dotnet/runtime/tree/main/src/libraries/System.Text.Json
     ///  
-    /// System.Text.Json 自定义Converter实现时间转换 https://my.oschina.net/u/4359742/blog/3314243
+    /// System.Text.Json convert DateTime https://my.oschina.net/u/4359742/blog/3314243
     /// </summary>
     public partial class Serialization_Text : ISerialization
     {
@@ -23,7 +27,7 @@ namespace Sers.Core.Module.Serialization.Text
 
         public readonly JsonSerializerOptions options = new JsonSerializerOptions
         {
-            //中文不转义 如 {"title":"\u4ee3\u7801\u6539\u53d8\u4e16\u754c"}
+            // Do not escape Chinese , for example {"title":"\u4ee3\u7801\u6539\u53d8\u4e16\u754c"}
             Encoder = System.Text.Encodings.Web.JavaScriptEncoder.Create(UnicodeRanges.All),
             IncludeFields = true,
             DefaultIgnoreCondition = System.Text.Json.Serialization.JsonIgnoreCondition.WhenWritingNull
@@ -37,40 +41,25 @@ namespace Sers.Core.Module.Serialization.Text
         {
             options.AddConverter_Newtonsoft();
 
-
-            //日期格式化
-            var DateTimeFormat = Appsettings.json.GetByPath<string>("Vit.Serialization.DateTimeFormat")
-              ?? "yyyy-MM-dd HH:mm:ss";
+            // format DateTime
+            var DateTimeFormat = Appsettings.json.GetByPath<string>("Vit.Serialization.DateTimeFormat") ?? "yyyy-MM-dd HH:mm:ss";
 
             jsonConverter_DateTime = options.AddConverter_DateTime(DateTimeFormat);
-
         }
 
 
 
 
 
-
-
-
-
-
-        #region (x.1)object <--> String
+        #region #1 object <--> String
 
         #region Serialize
 
         [MethodImpl(MethodImplOptions.AggressiveInlining)]
-        public string Serialize<T>(T value)
+        public string Serialize(object value, Type type = null)
         {
-            return JsonSerializer.Serialize(value, options);
-        }
-
-
-
-        [MethodImpl(MethodImplOptions.AggressiveInlining)]
-        public string Serialize(object value, Type type)
-        {
-            return JsonSerializer.Serialize(value, type, options);
+            if (value == default) return null;
+            return JsonSerializer.Serialize(value, type ?? value?.GetType(), options);
         }
 
         #endregion
@@ -89,6 +78,7 @@ namespace Sers.Core.Module.Serialization.Text
         [MethodImpl(MethodImplOptions.AggressiveInlining)]
         public object Deserialize(string value, Type type)
         {
+            if (string.IsNullOrWhiteSpace(value)) return type.DefaultValue();
             return JsonSerializer.Deserialize(value, type, options);
         }
 
@@ -98,23 +88,15 @@ namespace Sers.Core.Module.Serialization.Text
 
 
 
-        #region  (x.2)object <--> bytes
+        #region  #2 object <--> bytes
 
         #region SerializeToBytes
 
         [MethodImpl(MethodImplOptions.AggressiveInlining)]
-        public byte[] SerializeToBytes<T>(T value)
-        {
-
-            return JsonSerializer.SerializeToUtf8Bytes(value, options);
-        }
-
-
-
-        [MethodImpl(MethodImplOptions.AggressiveInlining)]
-        public byte[] SerializeToBytes(object value, Type type)
+        public byte[] SerializeToBytes(object value, Type type = null)
         {
-            return JsonSerializer.SerializeToUtf8Bytes(value, type, options);
+            if (value == default) return null;
+            return JsonSerializer.SerializeToUtf8Bytes(value, type ?? value?.GetType(), options);
         }
         #endregion
 
@@ -124,12 +106,14 @@ namespace Sers.Core.Module.Serialization.Text
         [MethodImpl(MethodImplOptions.AggressiveInlining)]
         public T DeserializeFromBytes<T>(byte[] bytes)
         {
+            if (bytes == null) return (T)typeof(T).DefaultValue();
             return JsonSerializer.Deserialize<T>(bytes, options);
         }
 
         [MethodImpl(MethodImplOptions.AggressiveInlining)]
         public object DeserializeFromBytes(byte[] bytes, Type type)
         {
+            if (bytes == null) return type.DefaultValue();
             return JsonSerializer.Deserialize(bytes, type, options);
         }
         #endregion
@@ -139,25 +123,25 @@ namespace Sers.Core.Module.Serialization.Text
 
 
 
-        #region (x.3)DeserializeFromSpan
+        #region #3 DeserializeFromSpan
 
         [MethodImpl(MethodImplOptions.AggressiveInlining)]
         public T DeserializeFromSpan<T>(ReadOnlySpan<byte> bytes)
         {
-            if (bytes.Length == 0) return default;
+            if (bytes.Length == 0) return (T)typeof(T).DefaultValue();
             return JsonSerializer.Deserialize<T>(bytes, options);
         }
 
         [MethodImpl(MethodImplOptions.AggressiveInlining)]
         public object DeserializeFromSpan(ReadOnlySpan<byte> bytes, Type type)
         {
-            if (bytes.Length == 0) return default;
+            if (bytes.Length == 0) return type.DefaultValue();
             return JsonSerializer.Deserialize(bytes, type, options);
         }
         #endregion
 
 
-        #region (x.4)DeserializeFromArraySegmentByte
+        #region #4 DeserializeFromArraySegmentByte
 
         [MethodImpl(MethodImplOptions.AggressiveInlining)]
         public T DeserializeFromArraySegmentByte<T>(ArraySegment<byte> bytes)

+ 1 - 3
dotnet/Library/Sers/Sers.Core/Test/Sers.Core.Module.LocalApi.Qps/LocalApi/LocalApiTest.cs

@@ -50,9 +50,7 @@ namespace Sers.Core.Module.LocalApi.MsTest.LocalApi
 
                         qpsInfo.RequestCount++;
                     }
-                    catch (Exception ex)
-                    {
-                    }
+                    catch { }
                 }
 
             });

+ 7 - 5
dotnet/Library/Sers/Sers.Core/Test/Sers.Core.Module.Rpc.MsTest/UnitTest1.cs

@@ -1,5 +1,7 @@
 using Microsoft.VisualStudio.TestTools.UnitTesting;
+
 using System;
+
 using Vit.Extensions;
 
 namespace Sers.Core.Module.Rpc.MsTest
@@ -16,7 +18,7 @@ namespace Sers.Core.Module.Rpc.MsTest
                 {
                     var rpcData = new RpcContextData();
                     rpcContext.rpcData = rpcData;
-                    rpcData.http.method="POST";
+                    rpcData.http.method = "POST";
 
 
                     var rpcData2 = RpcContextData.FromBytes(rpcData.ToBytes());
@@ -24,10 +26,10 @@ namespace Sers.Core.Module.Rpc.MsTest
 
                     Assert.AreEqual(rpcData.http.method, rpcData2.http.method);
                 }
-                 
+
             }
-            catch (Exception ex)
-            {              
+            catch
+            {
                 Assert.Fail();
             }
 
@@ -35,6 +37,6 @@ namespace Sers.Core.Module.Rpc.MsTest
 
         }
 
-      
+
     }
 }

+ 0 - 1
dotnet/Library/Sers/Sers.Core/Test/Sers.Core.Module.Serialization.Qps/Program.cs

@@ -71,7 +71,6 @@ namespace App
 
 
                 byte[] bytes;
-                JObject jo;
                 string str;
                 RpcContextData data2;
 

+ 126 - 0
dotnet/Library/Sers/Sers.Core/Test/Sers.Core.MsTest/Module/Serialization_Text_Test.cs

@@ -0,0 +1,126 @@
+using System;
+
+using Microsoft.VisualStudio.TestTools.UnitTesting;
+
+using Sers.Core.Module.Serialization.Text;
+
+namespace Sers.Core.MsTest.Module
+{
+    [TestClass]
+    public class Serialization_Text_Test
+    {
+        class ModelA
+        {
+            public int? id;
+            public string name;
+            public DateTime time;
+        }
+        static readonly string testString = "testString中文12—=¥$《》<> \n\r\t😀" + DateTime.Now;
+        static Serialization_Text Instance => Serialization_Text.Instance;
+
+        [TestMethod]
+        public void TestMethod_String()
+        {
+
+
+            // #1 object
+            {
+                var modelA = new ModelA { id = 1, name = testString };
+                var objA = new { id = 1, name = testString };
+
+
+                Assert.AreEqual(testString, Instance.Deserialize<ModelA>(Instance.Serialize(modelA))?.name);
+                Assert.AreEqual(testString, Instance.Deserialize<ModelA>(Instance.Serialize(objA))?.name);
+            }
+
+            // #2 ValueType
+            {
+                // string
+                TestBySerialize(testString);
+                TestBySerialize((string)null);
+
+                // int
+                TestBySerialize(10);
+                TestBySerialize((int?)10);
+                TestBySerialize((int?)null);
+
+                // long
+                TestBySerialize((long)123456789);
+                TestBySerialize((long?)123456789);
+                TestBySerialize((long?)null);
+
+                // double
+                TestBySerialize(10.4567);
+                TestBySerialize((double?)10.4567);
+                TestBySerialize((double?)null);
+
+                // bool
+                TestBySerialize(true, "true");
+                TestBySerialize((bool?)false);
+                TestBySerialize((bool?)null);
+
+                // DateTime
+                var date = DateTime.Parse("2019-01-01 01:01:01");
+                TestBySerialize(date, "\"2019-01-01 01:01:01\"");
+                TestBySerialize((DateTime?)date);
+                TestBySerialize((DateTime?)null);
+            }
+        }
+
+        public void TestBySerialize<T>(T value)
+        {
+            var str = Instance.Serialize(value);
+            var actual = Instance.Deserialize<T>(str);
+            Assert.AreEqual(value, actual);
+        }
+        public void TestBySerialize<T>(T value, string expected)
+        {
+            var str = Instance.Serialize(value);
+            Assert.AreEqual(expected, str);
+            var actual = Instance.Deserialize<T>(str);
+            Assert.AreEqual(value, actual);
+        }
+
+
+
+        [TestMethod]
+        public void TestMethod_DateTimeFormat()
+        {
+            var date = DateTime.Parse("2019-01-01 01:01:01");
+            var objA = new { time = date };
+
+
+            var dateFormatString = Instance.jsonConverter_DateTime.dateFormatString;
+            try
+            {
+                var format = "yyyy-MM-dd";
+                Instance.jsonConverter_DateTime.dateFormatString = format;
+
+                string str = Instance.Serialize(objA);
+                var obj2 = Instance.Deserialize<ModelA>(str);
+
+                Assert.AreEqual(DateTime.Parse("2019-01-01"), obj2.time);
+            }
+            finally
+            {
+                Instance.jsonConverter_DateTime.dateFormatString = dateFormatString;
+            }
+        }
+
+
+
+
+        [TestMethod]
+        public void TestMethod_Bytes()
+        {
+            var modelA = new ModelA { id = 1, name = testString, time = DateTime.Now };
+            var objA = new { id = 1, name = testString, time = DateTime.Now };
+
+            Assert.AreEqual(Instance.DeserializeFromBytes<ModelA>(Instance.SerializeToBytes(modelA))?.name, testString);
+            Assert.AreEqual(Instance.DeserializeFromBytes<ModelA>(Instance.SerializeToBytes(objA))?.name, testString);
+        }
+
+
+
+    }
+}

+ 17 - 14
dotnet/Library/Sers/Sers.Core/Test/Sers.Core.Util.Consumer.Test/Queue/Program_ConcurrentLinkedQueue.cs

@@ -1,9 +1,12 @@
 using System;
 using System.Threading;
 using System.Threading.Tasks;
+
 using CLClient.Statistics;
+
 using Sers.Core.Util.PubSub;
 using Sers.Core.Util.PubSub.Test.Queue;
+
 using Vit.Core.Module.Log;
 using Vit.Core.Util.Pool;
 
@@ -39,7 +42,7 @@ namespace CLClient1
 
 
 
-        class Product 
+        class Product
         {
             public static Product Pop()
             {
@@ -62,14 +65,14 @@ namespace CLClient1
 
         ConcurrentLinkedQueue<Product> worker;
         string name;
-        public void Start() 
+        public void Start()
         {
-    
-           
+
+
             worker = new ConcurrentLinkedQueue<Product>();
- 
 
- 
+
+
 
 
             for (int i = pubThreadCount; i > 0; i--)
@@ -85,18 +88,18 @@ namespace CLClient1
         }
 
 
-        static int pubThreadCount =4;
+        static int pubThreadCount = 4;
         static int subThreadCount = 4;
-      
+
         void StartThreadPublish()
         {
             Task.Run(() =>
             {
-                Product product=new Product();
+                Product product = new Product();
 
                 for (int i = 0; i < int.MaxValue; i++)
                 {
-                    for (var t = 1; t <10000; t++)
+                    for (var t = 1; t < 10000; t++)
                     {
 
 
@@ -113,7 +116,7 @@ namespace CLClient1
                     //worker.Publish(product);         
                     worker.Enqueue(product);
                     qpsPub.IncrementRequest(10000);
-                   
+
                     //Thread.Sleep(1);
 
                 }
@@ -126,13 +129,13 @@ namespace CLClient1
             Task.Run(() =>
             {
 
-                SpinWait spin = new SpinWait();
+                //SpinWait spin = new SpinWait();
                 for (int i = 0; i < int.MaxValue; i++)
                 {
                     worker.TryDequeue(out var product);
-     
 
-                    if (product == null) 
+
+                    if (product == null)
                     {
                         //spin.SpinOnce();
                     }

+ 22 - 19
dotnet/Library/Sers/Sers.Core/Test/Sers.Core.Util.Consumer.Test/Queue/Program_QueueQps.cs

@@ -2,9 +2,12 @@
 using System.Collections.Concurrent;
 using System.Threading;
 using System.Threading.Tasks;
+
 using CLClient.Statistics;
+
 using Sers.Core.Util.PubSub;
 using Sers.Core.Util.PubSub.Test.Queue;
+
 using Vit.Core.Module.Log;
 using Vit.Core.Util.Pool;
 
@@ -54,7 +57,7 @@ namespace CLClient1
 
             for (var t = 0; t < 1; t++)
             {
-               Start();
+                Start();
             }
 
             while (true)
@@ -65,7 +68,7 @@ namespace CLClient1
 
 
 
-        class Product 
+        class Product
         {
             public static Product Pop()
             {
@@ -91,7 +94,7 @@ namespace CLClient1
         {
 
             var worker = new Queue_Channel<Product>();
- 
+
             int pubThreadCount = 4;
             int subThreadCount = 6;
 
@@ -111,15 +114,15 @@ namespace CLClient1
                             //product = Product.Pop();
                             //product.ms = 0;
                             //product.IncrementCount = 0;
-                             worker.Publish(product);
-                   
+                            worker.Publish(product);
+
 
                         }
                         //product = Product.Pop();
                         //product.ms = 1;
                         //product.IncrementCount = 10000;
-                         worker.Publish(product);
-               
+                        worker.Publish(product);
+
                         qpsPub.IncrementRequest(10000);
 
                         //Thread.Sleep(1);
@@ -161,9 +164,9 @@ namespace CLClient1
 
         #region RingBuffer      
         static void Start_RingBuffer()
-        { 
+        {
             var worker = new RingBuffer<Product>();
-    
+
 
             int pubThreadCount = 4;
             int subThreadCount = 6;
@@ -184,13 +187,13 @@ namespace CLClient1
                             //product = Product.Pop();
                             //product.ms = 0;
                             //product.IncrementCount = 0;
-                            worker.Publish(product);                  
+                            worker.Publish(product);
 
                         }
                         //product = Product.Pop();
                         //product.ms = 1;
                         //product.IncrementCount = 10000;
-                        worker.Publish(product);       
+                        worker.Publish(product);
                         qpsPub.IncrementRequest(10000);
                         //Thread.Sleep(1);
                     }
@@ -233,8 +236,8 @@ namespace CLClient1
 
 
         #region ConcurrentQueue      
-        static void Start2() 
-        { 
+        static void Start2()
+        {
             var worker = new ConcurrentQueue<Product>();
 
             int pubThreadCount = 4;
@@ -279,7 +282,7 @@ namespace CLClient1
                 Task.Run(() =>
                 {
 
-                    SpinWait spin = new SpinWait();
+                    //SpinWait spin = new SpinWait();
                     for (; ; )
                     {
                         worker.TryDequeue(out var p);
@@ -323,10 +326,10 @@ namespace CLClient1
                     for (; ; )
                     {
                         for (var t = 1; t < 10000; t++)
-                        { 
+                        {
                             worker.Add(product);
                         }
-             
+
                         worker.Add(product);
                         qpsPub.IncrementRequest(10000);
 
@@ -341,19 +344,19 @@ namespace CLClient1
             for (int i = subThreadCount; i > 0; i--)
             {
                 Task.Run(() =>
-                {                   
+                {
                     for (; ; )
                     {
 
                         worker.TryTake(out var p);
 
 
-                      
+
                         //if (p == null) 
                         //{
                         //    spin.SpinOnce();
                         //}
- 
+
                     }
                 });
             }

+ 1 - 8
dotnet/Library/Sers/Sers.Hardware/Net46/Sers.Hardware.Net46.Exe/Program.cs

@@ -26,13 +26,6 @@ namespace Sers.Hardware.Net46.Exe
                     Console.Write("NetworkIn:" + infos[2] + ",");
                     Console.WriteLine("NetworkOut:" + infos[3] + "");
 
-                    //Console.Write("Cpu使用情况:" + infos[0] + "%   ");
-                    //Console.Write("内存使用情况:" + infos[1] + "%   ");
-                    //Console.Write("network:" + infos[2] + "/" + infos[3] + " Mbps   ");
-
-
-
-
 
                     //Console.WriteLine("");
                     //Thread.Sleep(100);
@@ -41,7 +34,7 @@ namespace Sers.Hardware.Net46.Exe
 
 
 
-            Console.ReadKey();
+            //Console.ReadKey();
         }
     }
 }

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

@@ -44,14 +44,13 @@ namespace Vit.Core.MsTest.Extensions
             try
             {
 
-                //将会抛异常           
+                // will throw error
                 "5.1".Convert<int?>();
 
                 "5.1".Convert<int>();
             }
-            catch (Exception ex)
-            {
-            }
+            catch
+            { }
 
 
             double double1 = "5.123456".Convert<double>();

+ 0 - 98
dotnet/Library/Vit/Vit.Core/Test/Vit.Core.MsTest/Module/SerializationTest.cs

@@ -1,98 +0,0 @@
-using System;
-
-using Microsoft.VisualStudio.TestTools.UnitTesting;
-
-using Newtonsoft.Json.Linq;
-
-using Vit.Core.Module.Serialization;
-using Vit.Extensions.Json_Extensions;
-using Vit.Extensions.Newtonsoft_Extensions;
-using Vit.Extensions.Object_Serialize_Extensions;
-
-namespace Vit.Core.MsTest.Module
-{
-    [TestClass]
-    public class SerializationTest
-    {
-        class ModelA
-        {
-            public int? id;
-            public string name;
-            public DateTime time;
-        }
-
-
-        [TestMethod]
-        public void TestMethod()
-        {
-            string testString = "testString中文12—=¥$《》<> \n\r\t😀" + DateTime.Now;
-
-            var modelA = new ModelA { id = 1, name = testString, time = DateTime.Now };
-
-            #region (x.1)bytes <--> String      
-            Assert.AreEqual(testString.StringToBytes().BytesToString(), testString);
-            #endregion
-
-
-            #region (x.2)object <--> String
-            Assert.AreEqual(Json.Deserialize<ModelA>(Json.Serialize(modelA))?.name, testString);
-            Assert.AreEqual(modelA.Serialize().Deserialize<ModelA>()?.name, testString);
-            #endregion
-
-
-            #region (x.3)object <--> bytes
-            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
-            {
-                Date = DateTime.Parse("2019-01-01 01:01:01"),
-                obj = new { Date2 = DateTime.Parse("2019-02-02 01:01:01") }
-            };
-
-            string str = obj.Serialize();
-            var DateFormatString = Serialization_Newtonsoft.Instance.serializeSetting.DateFormatString;
-            try
-            {
-                Serialization_Newtonsoft.Instance.serializeSetting.DateFormatString = "yyyy-MM-dd";
-
-                string str2 = obj.Serialize();
-                var jtObj = str2.Deserialize<JObject>();
-
-                Assert.AreEqual(jtObj.StringGetByPath("Date"), "2019-01-01");
-                Assert.AreEqual(jtObj.StringGetByPath("obj", "Date2"), "2019-02-02");
-            }
-            finally
-            {
-                Serialization_Newtonsoft.Instance.serializeSetting.DateFormatString = DateFormatString;
-            }
-            #endregion
-
-
-
-        }
-
-
-
-        [TestMethod]
-        public void TestMethod_DateTime()
-        {
-            var time = DateTime.Now;
-            var str = Json.Serialize(time);
-            Assert.AreEqual(time.ToString("yyyy-MM-dd HH:mm:ss"), str);
-        }
-
-
-    }
-}

+ 50 - 0
dotnet/Library/Vit/Vit.Core/Test/Vit.Core.MsTest/Module/Serialization_Extensions_Test.cs

@@ -0,0 +1,50 @@
+using System;
+
+using Microsoft.VisualStudio.TestTools.UnitTesting;
+
+using Vit.Extensions.Json_Extensions;
+using Vit.Extensions.Newtonsoft_Extensions;
+using Vit.Extensions.Object_Serialize_Extensions;
+
+namespace Vit.Core.MsTest.Module
+{
+    [TestClass]
+    public class Serialization_Extensions_Test
+    {
+        class ModelA
+        {
+            public int? id;
+            public string name;
+            public DateTime time;
+        }
+
+
+        static readonly string testString = "testString中文12—=¥$《》<> \n\r\t😀" + DateTime.Now;
+
+        [TestMethod]
+        public void TestMethod()
+        {
+
+            var modelA = new ModelA { id = 1, name = testString, time = DateTime.Now };
+
+            // #1 object <--> String 
+            Assert.AreEqual(testString, modelA.Serialize().Deserialize<ModelA>()?.name);
+
+
+            // #2 object <--> bytes
+            Assert.AreEqual(modelA.SerializeToBytes().DeserializeFromBytes<ModelA>()?.name, testString);
+
+
+            // #3 ConvertBySerialize
+            var obj_ori = new { id = 1, name = testString, time = DateTime.Now };
+            Assert.AreEqual(obj_ori.ConvertBySerialize<ModelA>()?.name, testString);
+
+
+        }
+
+
+
+
+
+    }
+}

+ 135 - 0
dotnet/Library/Vit/Vit.Core/Test/Vit.Core.MsTest/Module/Serialization_Newtonsoft_Test.cs

@@ -0,0 +1,135 @@
+using System;
+
+using Microsoft.VisualStudio.TestTools.UnitTesting;
+
+using Vit.Core.Module.Serialization;
+
+namespace Vit.Core.MsTest.Module
+{
+    [TestClass]
+    public class Serialization_Newtonsoft_Test
+    {
+        class ModelA
+        {
+            public int? id;
+            public string name;
+            public DateTime time;
+        }
+        static readonly string testString = "testString中文12—=¥$《》<> \n\r\t😀" + DateTime.Now;
+        static Serialization_Newtonsoft Instance => Serialization_Newtonsoft.Instance;
+
+        [TestMethod]
+        public void TestMethod_String()
+        {
+
+
+            // #1 object
+            {
+                var modelA = new ModelA { id = 1, name = testString };
+                var objA = new { id = 1, name = testString };
+
+
+                Assert.AreEqual(testString, Instance.Deserialize<ModelA>(Instance.Serialize(modelA))?.name);
+                Assert.AreEqual(testString, Instance.Deserialize<ModelA>(Instance.Serialize(objA))?.name);
+            }
+
+            // #2 ValueType
+            {
+                // string
+                TestBySerialize(testString);
+                TestBySerialize((string)null);
+
+                // int
+                TestBySerialize(10);
+                TestBySerialize((int?)10);
+                TestBySerialize((int?)null);
+
+                // long
+                TestBySerialize((long)123456789);
+                TestBySerialize((long?)123456789);
+                TestBySerialize((long?)null);
+
+                // double
+                TestBySerialize(10.4567);
+                TestBySerialize((double?)10.4567);
+                TestBySerialize((double?)null);
+
+                // bool
+                TestBySerialize(true, "true");
+                TestBySerialize((bool?)false);
+                TestBySerialize((bool?)null);
+
+                // DateTime
+                var date = DateTime.Parse("2019-01-01 01:01:01");
+                TestBySerialize(date, "\"2019-01-01 01:01:01\"");
+                TestBySerialize((DateTime?)date);
+                TestBySerialize((DateTime?)null);
+            }
+        }
+
+        public void TestBySerialize<T>(T value)
+        {
+            var str = Instance.Serialize(value);
+            var actual = Instance.Deserialize<T>(str);
+            Assert.AreEqual(value, actual);
+        }
+        public void TestBySerialize<T>(T value, string expected)
+        {
+            var str = Instance.Serialize(value);
+            Assert.AreEqual(expected, str);
+            var actual = Instance.Deserialize<T>(str);
+            Assert.AreEqual(value, actual);
+        }
+
+
+
+        [TestMethod]
+        public void TestMethod_DateTimeFormat()
+        {
+            var date = DateTime.Parse("2019-01-01 01:01:01");
+            var objA = new { time = date };
+
+
+            var dateFormatString = Instance.serializeSetting.DateFormatString;
+            try
+            {
+                var format = "yyyy-MM-dd";
+                Instance.serializeSetting.DateFormatString = format;
+
+                string str = Instance.Serialize(objA);
+                var obj2 = Instance.Deserialize<ModelA>(str);
+
+                Assert.AreEqual(DateTime.Parse("2019-01-01"), obj2.time);
+            }
+            finally
+            {
+                Instance.serializeSetting.DateFormatString = dateFormatString;
+            }
+        }
+
+
+
+
+        [TestMethod]
+        public void TestMethod_Bytes()
+        {
+            var modelA = new ModelA { id = 1, name = testString, time = DateTime.Now };
+            var objA = new { id = 1, name = testString, time = DateTime.Now };
+
+            Assert.AreEqual(Instance.DeserializeFromBytes<ModelA>(Instance.SerializeToBytes(modelA))?.name, testString);
+            Assert.AreEqual(Instance.DeserializeFromBytes<ModelA>(Instance.SerializeToBytes(objA))?.name, testString);
+        }
+
+
+        [TestMethod]
+        public void TestMethod_BytesToString()
+        {
+            //  bytes <--> String
+            Assert.AreEqual(Instance.BytesToString(Instance.StringToBytes(testString)), testString);
+        }
+
+
+
+
+    }
+}

+ 10 - 11
dotnet/Library/Vit/Vit.Core/Vit.Core/Extensions/Newtonsoft/JTokenExtensions.cs

@@ -84,12 +84,12 @@ namespace Vit.Extensions.Newtonsoft_Extensions
         /// 
         /// </summary>
         /// <param name="data"></param>
-        /// <param name="path">value在data中的路径,可不指定。例如:new []{"taskList"}</param>
+        /// <param name="path">value path in data, could be null. (example: new []{"taskList"} ) </param>
         /// <returns></returns>
         [MethodImpl(MethodImplOptions.AggressiveInlining)]
         public static string StringGetByPath(this JToken data, params object[] path)
         {
-            return data?.JTokenGetByPath(path).ConvertToString();
+            return data?.JTokenGetByPath(path)?.ConvertToString();
         }
 
         #endregion
@@ -190,8 +190,7 @@ namespace Vit.Extensions.Newtonsoft_Extensions
         /// 返回具体的值。(若t为null则返回null)
         /// 例如 若为 double 则返回其double值
         /// (JArray、bool、byte[]、DateTime、double、long、JObject、String)
-        /// 注:JTokenType.Comment 类型会返回 其 t.ToString()
-        /// 其他返回null(JTokenType.None、JTokenType.Null、JTokenType.Undefined 等)。
+        /// 其他返回null(JTokenType.None、JTokenType.Null、JTokenType.Undefined、JTokenType.Comment 等)。
         /// </summary>
         /// <param name="t"></param>
         /// <returns></returns>
@@ -209,10 +208,10 @@ namespace Vit.Extensions.Newtonsoft_Extensions
                 case JTokenType.Integer: return t.Value<long>();
                 case JTokenType.Object: return t.Value<JObject>();
                 case JTokenType.String: return t.Value<string>();
-                case JTokenType.Comment: return t.ToString();
-                case JTokenType.None: return null;
-                case JTokenType.Null: return null;
-                case JTokenType.Undefined: return null;
+                //case JTokenType.Comment: return t.ToString();
+                //case JTokenType.None: return null;
+                //case JTokenType.Null: return null;
+                //case JTokenType.Undefined: return null;
                 default: return null;
             }
         }
@@ -239,7 +238,7 @@ namespace Vit.Extensions.Newtonsoft_Extensions
             }
             return token.ToString();
         }
-        #endregion                                    
+        #endregion
 
 
 
@@ -248,12 +247,12 @@ namespace Vit.Extensions.Newtonsoft_Extensions
         [MethodImpl(MethodImplOptions.AggressiveInlining)]
         public static bool TypeMatch(this JToken token, JTokenType type)
         {
-            return null != token && type == token.Type;
+            return token?.Type == type;
         }
         [MethodImpl(MethodImplOptions.AggressiveInlining)]
         public static bool TypeMatch(this JToken token, JToken token2)
         {
-            return null != token && token2 != null && token.Type == token2.Type;
+            return token != null && token?.Type == token2?.Type;
         }
         #endregion
 

+ 1 - 3
dotnet/Library/Vit/Vit.Core/Vit.Core/Module/Log/Logger.cs

@@ -84,9 +84,7 @@ namespace Vit.Core.Module.Log
 
                     log.AddCollector(collector);
                 }
-                catch (Exception ex)
-                {
-                }
+                catch { }
             });
 
 

+ 6 - 7
dotnet/Library/Vit/Vit.Core/Vit.Core/Module/Serialization/Extensions/Object_Serialize_Extensions.cs

@@ -8,13 +8,12 @@ namespace Vit.Extensions.Object_Serialize_Extensions
     public static partial class Object_Serialize_Extensions
     {
 
-        #region (x.1)object <--> String
+        #region #1 object <--> String
 
         #region Serialize
 
         /// <summary>
-        /// 使用Newtonsoft序列化。
-        /// value 可为 struct(int bool string 等) 或者 class(模型 Array JObject等)
+        /// value type could be:   byte[] / string / Object / Array / struct or ValueType(int? / bool)
         /// </summary>
         /// <param name="value"></param>
         /// <returns></returns>
@@ -29,7 +28,7 @@ namespace Vit.Extensions.Object_Serialize_Extensions
         #region Deserialize
 
         /// <summary>
-        /// 使用Newtonsoft反序列化。T也可为值类型(例如 int?、bool) 
+        /// Deserialize to type, type could be:   byte[] / string / Object / Array / struct or ValueType(int? / bool)
         /// </summary>
         /// <param name="value"></param>
         /// <param name="type"></param>
@@ -41,7 +40,7 @@ namespace Vit.Extensions.Object_Serialize_Extensions
         }
 
         /// <summary>
-        /// 使用Newtonsoft反序列化。T也可为值类型(例如 int?、bool) 
+        /// Deserialize to T, T could be:   byte[] / string / Object / Array / struct or ValueType(int? / bool)
         /// </summary>
         /// <typeparam name="T"></typeparam>
         /// <param name="value"></param>
@@ -57,7 +56,7 @@ namespace Vit.Extensions.Object_Serialize_Extensions
         #endregion
 
 
-        #region (x.2)object <--> bytes
+        #region #2 object <--> bytes
 
         #region SerializeToBytes
         /// <summary>
@@ -101,7 +100,7 @@ namespace Vit.Extensions.Object_Serialize_Extensions
         #endregion
 
 
-        #region (x.3)object <--> ArraySegmentByte
+        #region #3 object <--> ArraySegmentByte
 
 
         /// <summary>

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

@@ -5,35 +5,26 @@ namespace Vit.Core.Module.Serialization
     public interface ISerialization
     {
 
-        #region (x.1)object <--> String
-
-        /// <summary>
-        /// T也可为值类型(例如 int?、bool) 
-        /// </summary>
-        /// <typeparam name="T"></typeparam>
-        /// <param name="value"></param>
-        /// <returns></returns>
-        string Serialize<T>(T value);
-
+        #region #1 object <--> String
         /// <summary>
-        /// T也可为值类型(例如 int?、bool) 
+        /// value and type could be:   byte[] / string / Object / Array / struct or ValueType(int? / bool)
         /// </summary>
         /// <param name="value"></param>
         /// <param name="type"></param>
         /// <returns></returns>
-        string Serialize(object value, Type type);
+        string Serialize(object value, Type type = null);
 
 
 
         /// <summary>
-        /// 使用Newtonsoft反序列化。T也可为值类型(例如 int?、bool) 
+        /// Deserialize to T, T could be:   byte[] / string / Object / Array / struct or ValueType(int? / bool)
         /// </summary>
         /// <param name="value"></param>
         /// <returns></returns>
         T Deserialize<T>(string value);
 
         /// <summary>
-        /// 使用Newtonsoft反序列化。T也可为值类型(例如 int?、bool) 
+        /// Deserialize to type, T could be:   byte[] / string / Object / Array / struct or ValueType(int? / bool)
         /// </summary>
         /// <param name="value"></param>
         /// <param name="type"></param>
@@ -44,26 +35,18 @@ namespace Vit.Core.Module.Serialization
 
 
 
-        #region (x.2)object <--> bytes
-
-        /// <summary>
-        /// T 可以为   byte[]、string、 object 、struct
-        /// </summary>
-        /// <typeparam name="T"></typeparam>
-        /// <param name="obj"></param>
-        /// <returns></returns>
-        byte[] SerializeToBytes<T>(T obj);
+        #region #2 object <--> bytes
 
         /// <summary>
-        /// type 可以为   byte[]、string、 object 、struct
+        /// value and type could be:   byte[] / string / Object / Array / struct or ValueType(int? / bool)
         /// </summary>
         /// <param name="value"></param>
         /// <param name="type"></param>
         /// <returns></returns>
-        byte[] SerializeToBytes(object value, Type type);
+        byte[] SerializeToBytes(object value, Type type = null);
 
         /// <summary>
-        /// type 可以为   byte[]、string、 object 、struct
+        /// T could be:   byte[] / string / Object / Array / struct or ValueType(int? / bool)
         /// </summary>
         /// <typeparam name="T"></typeparam>
         /// <param name="bytes"></param>
@@ -71,7 +54,7 @@ namespace Vit.Core.Module.Serialization
         T DeserializeFromBytes<T>(byte[] bytes);
 
         /// <summary>
-        /// type 可以为   byte[]、string、 object 、struct
+        /// type could be:   byte[] / string / Object / Array / struct or ValueType(int? / bool)
         /// </summary>
         /// <param name="bytes"></param>
         /// <param name="type"></param>
@@ -82,21 +65,18 @@ namespace Vit.Core.Module.Serialization
 
 
 
-        #region (x.3)object <--> Span
-
-        //T DeserializeFromSpan<T>(ReadOnlyMemory<byte> bytes);
-
-        //object DeserializeFromSpan(ReadOnlyMemory<byte> bytes, Type type);
-
+        #region #3 object <--> ArraySegmentByte
+        T DeserializeFromArraySegmentByte<T>(ArraySegment<byte> bytes);
+        object DeserializeFromArraySegmentByte(ArraySegment<byte> bytes, Type type);
         #endregion
 
 
 
-        #region (x.4)object <--> ArraySegmentByte
+        #region #4 object <--> Span
 
-        T DeserializeFromArraySegmentByte<T>(ArraySegment<byte> bytes);
+        //T DeserializeFromSpan<T>(ReadOnlyMemory<byte> bytes);
 
-        object DeserializeFromArraySegmentByte(ArraySegment<byte> bytes, Type type);
+        //object DeserializeFromSpan(ReadOnlyMemory<byte> bytes, Type type);
 
         #endregion
 

+ 13 - 52
dotnet/Library/Vit/Vit.Core/Vit.Core/Module/Serialization/Json.cs

@@ -13,28 +13,15 @@ namespace Vit.Core.Module.Serialization
 
 
 
-        #region (x.1)object <--> String
-
-        /// <summary>
-        /// T could be ValueType(example: int? , bool)
-        /// </summary>
-        /// <typeparam name="T"></typeparam>
-        /// <param name="value"></param>
-        /// <returns></returns>
-        [MethodImpl(MethodImplOptions.AggressiveInlining)]
-        public static string Serialize<T>(T value)
-        {
-            return Instance.Serialize<T>(value);
-        }
-
+        #region #1 object <--> String
         /// <summary>
-        /// T could be ValueType(example: int? , bool)
+        /// value and type could be:   byte[] / string / Object / Array / struct or ValueType(int? / bool)
         /// </summary>
         /// <param name="value"></param>
         /// <param name="type"></param>
         /// <returns></returns>
         [MethodImpl(MethodImplOptions.AggressiveInlining)]
-        public static string Serialize(object value, Type type)
+        public static string Serialize(object value, Type type = null)
         {
             return Instance.Serialize(value, type);
         }
@@ -42,7 +29,7 @@ namespace Vit.Core.Module.Serialization
 
 
         /// <summary>
-        /// 使用Newtonsoft反序列化。T could be ValueType(example: int? , bool)
+        /// Deserialize to T, T could be:   byte[] / string / Object / Array / struct or ValueType(int? / bool)
         /// </summary>
         /// <param name="value"></param>
         /// <returns></returns>
@@ -53,7 +40,7 @@ namespace Vit.Core.Module.Serialization
         }
 
         /// <summary>
-        /// 使用Newtonsoft反序列化。T could be ValueType(example: int? , bool)
+        /// Deserialize to type, type could be:   byte[] / string / Object / Array / struct or ValueType(int? / bool)
         /// </summary>
         /// <param name="value"></param>
         /// <param name="type"></param>
@@ -67,34 +54,22 @@ namespace Vit.Core.Module.Serialization
         #endregion
 
 
-        #region (x.2)object <--> bytes
-
-        /// <summary>
-        /// T 可以为   byte[]、string、 object 、struct
-        /// </summary>
-        /// <typeparam name="T"></typeparam>
-        /// <param name="obj"></param>
-        /// <returns></returns>
-        [MethodImpl(MethodImplOptions.AggressiveInlining)]
-        public static byte[] SerializeToBytes<T>(T obj)
-        {
-            return Instance.SerializeToBytes<T>(obj);
-        }
+        #region #2 object <--> bytes
 
         /// <summary>
-        /// type 可以为   byte[]、string、 object 、struct
+        /// value and type could be:   byte[] / string / Object / Array / struct or ValueType(int? / bool)
         /// </summary>
         /// <param name="value"></param>
         /// <param name="type"></param>
         /// <returns></returns>
         [MethodImpl(MethodImplOptions.AggressiveInlining)]
-        public static byte[] SerializeToBytes(object value, Type type)
+        public static byte[] SerializeToBytes(object value, Type type = null)
         {
             return Instance.SerializeToBytes(value, type);
         }
 
         /// <summary>
-        /// type 可以为   byte[]、string、 object 、struct
+        /// T could be:   byte[] / string / Object / Array / struct or ValueType(int? / bool)
         /// </summary>
         /// <typeparam name="T"></typeparam>
         /// <param name="bytes"></param>
@@ -106,7 +81,7 @@ namespace Vit.Core.Module.Serialization
         }
 
         /// <summary>
-        /// type 可以为   byte[]、string、 object 、struct
+        /// type could be:   byte[] / string / Object / Array / struct or ValueType(int? / bool)
         /// </summary>
         /// <param name="bytes"></param>
         /// <param name="type"></param>
@@ -118,7 +93,7 @@ namespace Vit.Core.Module.Serialization
         }
 
         /// <summary>
-        /// type 可以为   byte[]、string、 object 、struct
+        /// type could be:   byte[] / string / Object / Array / struct or ValueType(int? / bool)
         /// </summary>
         /// <typeparam name="T"></typeparam>
         /// <param name="bytes"></param>
@@ -130,7 +105,7 @@ namespace Vit.Core.Module.Serialization
         }
 
         /// <summary>
-        /// type 可以为   byte[]、string、 object 、struct
+        /// type could be:   byte[] / string / Object / Array / struct or ValueType(int? / bool)
         /// </summary>
         /// <param name="bytes"></param>
         /// <param name="type"></param>
@@ -145,23 +120,9 @@ namespace Vit.Core.Module.Serialization
 
 
 
-        #region (x.3)object <--> Span
-
-        //T DeserializeFromSpan<T>(ReadOnlyMemory<byte> bytes);
-
-        //object DeserializeFromSpan(ReadOnlyMemory<byte> bytes, Type type);
-
-        #endregion
-
 
+        #region #3 object <--> ArraySegmentByte
 
-        #region (x.4)object <--> ArraySegmentByte
-
-        /// <summary>
-        /// 
-        /// </summary>
-        /// <param name="value"></param>
-        /// <returns></returns>
         [MethodImpl(MethodImplOptions.AggressiveInlining)]
         public static ArraySegment<byte> SerializeToArraySegmentByte(object value)
         {

+ 65 - 99
dotnet/Library/Vit/Vit.Core/Vit.Core/Module/Serialization/Serialization_Newtonsoft.cs

@@ -23,16 +23,12 @@ namespace Vit.Core.Module.Serialization
 
 
 
-        #region 基础对象
-
-
-
-        #region 成员对象 
+        #region Basic Fields
 
 
+        #region Member
         public Encoding encoding { get; set; } = defaultEncoding;
-        public string charset { get => encoding.GetCharset(); }
-
+        public string charset => encoding?.GetCharset();
         #endregion
 
 
@@ -55,7 +51,7 @@ namespace Vit.Core.Module.Serialization
         #endregion
 
 
-        #region (x.1)bytes <--> String
+        #region bytes <--> String
 
         [MethodImpl(MethodImplOptions.AggressiveInlining)]
         public string BytesToString(byte[] data, Encoding encoding = null)
@@ -79,7 +75,7 @@ namespace Vit.Core.Module.Serialization
         /// 
         /// </summary>
         /// <param name="value"></param>
-        /// <param name="type">必须为 where T : struct</param>
+        /// <param name="type">must be struct</param>
         /// <returns></returns>
         [MethodImpl(MethodImplOptions.AggressiveInlining)]
         public object DeserializeStruct(string value, Type type)
@@ -87,25 +83,15 @@ namespace Vit.Core.Module.Serialization
             try
             {
                 if (type.IsStringType())
+                {
                     return value;
+                }
                 return value.Convert(type);
             }
             catch { }
             return type.DefaultValue();
         }
 
-        ///// <summary>
-        ///// 
-        ///// </summary>
-        ///// <typeparam name="T">必须为 where T : struct</typeparam>
-        ///// <param name="value"></param>
-        ///// <returns></returns>
-        //public object DeserializeStruct<T>(string value)
-        //{
-        //    return DeserializeStruct(value, typeof(T));
-        //}
-
-
 
         #endregion
 
@@ -121,16 +107,14 @@ namespace Vit.Core.Module.Serialization
 
         public Serialization_Newtonsoft()
         {
-
-            //忽略空值
+            // ignore properties with null value when serializing
             serializeSetting.NullValueHandling = NullValueHandling.Ignore;
 
-            //不缩进
+            // no pretty style
             serializeSetting.Formatting = Formatting.None;
 
-            //日期格式化
-            var DateTimeFormat = Appsettings.json.GetByPath<string>("Vit.Serialization.DateTimeFormat")
-                ?? "yyyy-MM-dd HH:mm:ss";
+            // DateTimeFormat
+            var DateTimeFormat = Appsettings.json.GetByPath<string>("Vit.Serialization.DateTimeFormat") ?? "yyyy-MM-dd HH:mm:ss";
 
             serializeSetting.DateFormatHandling = global::Newtonsoft.Json.DateFormatHandling.IsoDateFormat;
             serializeSetting.DateFormatString = DateTimeFormat;
@@ -140,53 +124,41 @@ namespace Vit.Core.Module.Serialization
 
 
 
-        #region (x.1)object <--> String
+        #region #1 object <--> String
 
         #region Serialize
 
         /// <summary>
-        /// T也可为值类型(例如 int?、bool) 
+        /// value and type could be:   byte[] / string / Object / Array / struct or ValueType(int? / bool)
         /// </summary>
-        /// <typeparam name="T"></typeparam>
         /// <param name="value"></param>
+        /// <param name="type"></param>
         /// <returns></returns>
         [MethodImpl(MethodImplOptions.AggressiveInlining)]
-        public string Serialize<T>(T value)
+        public string Serialize(object value, Type type = null)
         {
-            if (null == value) return null;
+            //if (null == value) return null;
 
-            return Serialize(value, value.GetType());
-        }
+            //if (value is Newtonsoft.Json.Linq.JToken token)
+            //{
+            //    if (token.Type == JTokenType.String) return token.Value<string>();
 
-        /// <summary>
-        /// T也可为值类型(例如 int?、bool) 
-        /// </summary>
-        /// <param name="value"></param>
-        /// <param name="type"></param>
-        /// <returns></returns>
-        [MethodImpl(MethodImplOptions.AggressiveInlining)]
-        public string Serialize(object value, Type type)
-        {
-            if (null == value) return null;
+            //    return token.ToString(serializeSetting.Formatting);
+            //}
 
-            if (value is Newtonsoft.Json.Linq.JToken token)
-            {
-                if (token.TypeMatch(JTokenType.String)) return token.ToString();
+            ////if (value is DateTime time)
+            ////{
+            ////    return time.ToString(serializeSetting.DateFormatString);
+            ////}
 
-                return token.ToString(serializeSetting.Formatting);
-            }
+            //if (type == null) type = value.GetType();
 
-            if (value is DateTime time)
-            {
-                return time.ToString(serializeSetting.DateFormatString);
-            }
+            //if (type.IsValueType && type.GetUnderlyingTypeIfNullable() != typeof(DateTime))
+            //{
+            //    return value.ToString();
+            //}
 
-            if (type.TypeIsValueTypeOrStringType())
-            {
-                //return value.Convert<string>();
-                return value.ToString();
-            }
-            return JsonConvert.SerializeObject(value, serializeSetting);
+            return JsonConvert.SerializeObject(value, type, serializeSetting);
         }
 
         #endregion
@@ -194,33 +166,34 @@ namespace Vit.Core.Module.Serialization
         #region Deserialize
 
         /// <summary>
-        /// 使用Newtonsoft反序列化。T也可为值类型(例如 int?、bool) 
+        /// T could be:   byte[] / string / Object / Array / struct or ValueType(int? / bool)
         /// </summary>
         /// <param name="value"></param>
         /// <returns></returns>
         [MethodImpl(MethodImplOptions.AggressiveInlining)]
         public T Deserialize<T>(string value)
         {
-            //return (T)Deserialize(value,typeof(T));
-            if (null == value) return default;
+            ////return (T)Deserialize(value,typeof(T));
+            //if (null == value) return default;
 
-            Type type = typeof(T);
+            //Type type = typeof(T);
 
-            if (type.GetUnderlyingTypeIfNullable().IsEnum)
-                return value.StringToEnum<T>();
+            //if (type.GetUnderlyingTypeIfNullable().IsEnum)
+            //    return value.StringToEnum<T>();
 
-            if (type.TypeIsValueTypeOrStringType())
-                return (T)DeserializeStruct(value, type);
+            ////if (type.TypeIsValueTypeOrStringType())
+            //if (type.IsValueType && type.GetUnderlyingTypeIfNullable() != typeof(DateTime))
+            //    return (T)DeserializeStruct(value, type);
 
 
-            //if (string.IsNullOrWhiteSpace(value)) return type.DefaultValue();
+            ////if (string.IsNullOrWhiteSpace(value)) return type.DefaultValue();
 
-            return JsonConvert.DeserializeObject<T>(value);
+            return JsonConvert.DeserializeObject<T>(value, serializeSetting);
         }
 
 
         /// <summary>
-        /// 使用Newtonsoft反序列化。T也可为值类型(例如 int?、bool) 
+        /// value and type could be:   byte[] / string / Object / Array / struct or ValueType(int? / bool)
         /// </summary>
         /// <param name="value"></param>
         /// <param name="type"></param>
@@ -228,17 +201,18 @@ namespace Vit.Core.Module.Serialization
         [MethodImpl(MethodImplOptions.AggressiveInlining)]
         public object Deserialize(string value, Type type)
         {
-            if (null == value || null == type) return null;
+            //if (null == value || null == type) return null;
 
-            if (type.GetUnderlyingTypeIfNullable().IsEnum)
-                return value.StringToEnum(type);
+            //if (type.GetUnderlyingTypeIfNullable().IsEnum)
+            //    return value.StringToEnum(type);
 
-            if (type.TypeIsValueTypeOrStringType())
-                return DeserializeStruct(value, type);
+            ////if (type.TypeIsValueTypeOrStringType())
+            //if (type.IsValueType && type.GetUnderlyingTypeIfNullable() != typeof(DateTime))
+            //    return DeserializeStruct(value, type);
 
-            //if (string.IsNullOrWhiteSpace(value)) return type.DefaultValue();
+            ////if (string.IsNullOrWhiteSpace(value)) return type.DefaultValue();
 
-            return JsonConvert.DeserializeObject(value, type);
+            return JsonConvert.DeserializeObject(value, type, serializeSetting);
         }
 
         #endregion
@@ -251,30 +225,20 @@ namespace Vit.Core.Module.Serialization
 
 
 
-        #region (x.2)object <--> bytes
+        #region #2 object <--> bytes
 
         #region SerializeToBytes
-        /// <summary>
-        /// T 可以为   byte[]、string、 object 、struct
-        /// </summary>
-        /// <typeparam name="T"></typeparam>
-        /// <param name="obj"></param>
-        /// <returns></returns>
-        [MethodImpl(MethodImplOptions.AggressiveInlining)]
-        public byte[] SerializeToBytes<T>(T obj)
-        {
-            return SerializeToBytes<T>(obj, null);
-        }
+
 
         /// <summary>
-        /// T 可以为   byte[]、string、 object 、struct
+        /// value and type could be:   byte[] / string / Object / Array / struct or ValueType(int? / bool)
         /// </summary>
-        /// <typeparam name="T"></typeparam>
         /// <param name="obj"></param>
+        /// <param name="type"></param>
         /// <param name="encoding"></param>
         /// <returns></returns>
         [MethodImpl(MethodImplOptions.AggressiveInlining)]
-        public byte[] SerializeToBytes<T>(T obj, Encoding encoding)
+        public byte[] SerializeToBytesWithEncoding(object obj, Type type = null, Encoding encoding = null)
         {
             if (null == obj) return null;
 
@@ -286,25 +250,27 @@ namespace Vit.Core.Module.Serialization
                 case ArraySegment<byte> asbs:
                     return asbs.ArraySegmentByteToBytes();
                 case string str:
-                    strValue = str; break;
-                default: strValue = Serialize(obj); break;
+                    strValue = str;
+                    break;
+                default:
+                    strValue = Serialize(obj, type);
+                    break;
             }
-
             return StringToBytes(strValue, encoding);
         }
 
 
 
         /// <summary>
-        /// type 可以为   byte[]、string、 object 、struct
+        /// value and type could be:   byte[] / string / Object / Array / struct or ValueType(int? / bool)
         /// </summary>
         /// <param name="value"></param>
         /// <param name="type"></param>
         /// <returns></returns>
         [MethodImpl(MethodImplOptions.AggressiveInlining)]
-        public byte[] SerializeToBytes(object value, Type type)
+        public byte[] SerializeToBytes(object value, Type type = null)
         {
-            return SerializeToBytes(value);
+            return SerializeToBytesWithEncoding(value, type);
         }
         #endregion
 
@@ -384,7 +350,7 @@ namespace Vit.Core.Module.Serialization
 
 
 
-        #region (x.4)object <--> ArraySegmentByte
+        #region #3 object <--> ArraySegmentByte
 
         #region SerializeToArraySegmentByte
         /// <summary>

+ 1 - 3
dotnet/Library/Vit/Vit.Core/Vit.Core/Util/Shell/OsShell.cs

@@ -55,9 +55,7 @@ namespace Vit.Core.Util.Shell
                             process = null;
                         }
                     }
-                    catch (System.Exception ex)
-                    {
-                    }
+                    catch { }
                 }
             };
             AppDomain.CurrentDomain.ProcessExit += stopProcess;

+ 1 - 3
dotnet/Library/Vit/Vit.Core/Vit.Core/Util/Shell/ShellStream.cs

@@ -46,9 +46,7 @@ namespace Vit.Core.Util.Shell
                                 process = null;
                             }
                         }
-                        catch (System.Exception ex)
-                        {
-                        }
+                        catch { }
                     }
                 };
             #endregion

+ 0 - 2
dotnet/Library/Vit/Vit.Core/Vit.Core/Util/XmlComment/XmlCommentHelp.cs

@@ -87,8 +87,6 @@ namespace Vit.Core.Util.XmlComment
 
         public string Type_GetSummary(Type type)
         {
-            Xml.XmlHelp s;
-          
             var memberName = "T:" + type.FullName;
 
             if (!members.TryGetValue(memberName, out var list)) return null;

+ 6 - 1
dotnet/Sers.sln

@@ -155,7 +155,12 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Sers.ApiTrace.Collector.Zip
 EndProject
 Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Vit.Core.Util.Threading.MsTest", "Library\Vit\Vit.Core\Test\Vit.Core.Util.Threading.MsTest\Vit.Core.Util.Threading.MsTest.csproj", "{6A154DEE-D6EB-4DD5-B1EC-CF23ED89D743}"
 EndProject
-Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Sers.Core.Module.Api.ApiEvent.BeforeCallApi.JsonWebToken", "Library\Sers\Sers.Core\Sers.Core.Module.Api.ApiEvent.BeforeCallApi.JsonWebToken\Sers.Core.Module.Api.ApiEvent.BeforeCallApi.JsonWebToken.csproj", "{39A9BA34-1369-40C6-BABF-FEC57787165B}"
+Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Sers.Core.Module.Api.ApiEvent.BeforeCallApi.JsonWebToken", "Library\Sers\Sers.Core\Sers.Core.Module.Api.ApiEvent.BeforeCallApi.JsonWebToken\Sers.Core.Module.Api.ApiEvent.BeforeCallApi.JsonWebToken.csproj", "{39A9BA34-1369-40C6-BABF-FEC57787165B}"
+EndProject
+Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{D5E1F6A4-53C3-4C31-ABD8-8A5D13CE4E72}"
+	ProjectSection(SolutionItems) = preProject
+		.editorconfig = .editorconfig
+	EndProjectSection
 EndProject
 Global
 	GlobalSection(SolutionConfigurationPlatforms) = preSolution