lith 4 jaren geleden
bovenliggende
commit
f6e54c6d4e

+ 2 - 2
dotnet/Library/Sers/Sers.Core/Sers.Core/Module/Rpc/RpcContextData.cs

@@ -124,8 +124,8 @@ namespace Sers.Core.Module.Rpc
 
         public object error;
 
-
-        public object user;
+        [MessagePack.MessagePackFormatter(typeof(Serialization_MessagePack.MessagePackFormatter_JObject))]
+        public object user ;
 
        
 

+ 205 - 202
dotnet/Library/Vit/Vit.Core/Vit.Core/Module/Serialization/Serialization_MessagePack.cs

@@ -10,7 +10,7 @@ namespace Vit.Core.Module.Serialization
     /// <summary>
     /// 参考 https://github.com/neuecc/MessagePack-CSharp
     /// </summary>
-    public class Serialization_MessagePack : ISerialization
+    public partial class Serialization_MessagePack : ISerialization
     {
 
         public static readonly Serialization_MessagePack Instance = new Serialization_MessagePack();
@@ -39,6 +39,8 @@ namespace Vit.Core.Module.Serialization
                 });
 
             options = MessagePack.Resolvers.ContractlessStandardResolver.Options.WithResolver(resolver);
+
+            //options = MessagePack.Resolvers.ContractlessStandardResolver.Options;
         }
 
 
@@ -179,289 +181,290 @@ namespace Vit.Core.Module.Serialization
 {
     using Newtonsoft.Json.Linq;
 
+    public partial class Serialization_MessagePack
+    {
 
 
 
+        #region MessagePackFormatter JObject JArray 
 
-    #region MessagePackFormatter JObject JArray 
-
-
-    class MessagePackFormatter_JObject : IMessagePackFormatter<Newtonsoft.Json.Linq.JObject>
-    {
-
-        public static readonly MessagePackFormatter_JObject Instance = new MessagePackFormatter_JObject();
 
-        [MethodImpl(MethodImplOptions.AggressiveInlining)]
-        public void Serialize(
-          ref MessagePackWriter writer, Newtonsoft.Json.Linq.JObject value, MessagePackSerializerOptions options)
+        public class MessagePackFormatter_JObject : IMessagePackFormatter<Newtonsoft.Json.Linq.JObject>
         {
-            if (value.IsNull())
-            {
-                writer.WriteNil();
-                return;
-            }
 
-            writer.WriteMapHeader(value.Count);
+            public static readonly MessagePackFormatter_JObject Instance = new MessagePackFormatter_JObject();
 
-            foreach (var kv in value)
+            [MethodImpl(MethodImplOptions.AggressiveInlining)]
+            public void Serialize(
+              ref MessagePackWriter writer, Newtonsoft.Json.Linq.JObject value, MessagePackSerializerOptions options)
             {
-                //key
-                writer.Write(kv.Key);
-
-
-                //value
-                if (kv.Value.IsNull())
+                if (value.IsNull())
                 {
                     writer.WriteNil();
-                    continue;
+                    return;
                 }
 
-                switch (kv.Value)
-                {
-                    case JObject jo:
-                        //if (objectFormatter == null) objectFormatter = options.Resolver.GetFormatterWithVerify<JObject>();
-                        //objectFormatter?.Serialize(ref writer, jo, options); 
-                        Serialize(ref writer, jo, options);
-                        break;
-                    case JArray ja:
-                        //if (arrayFormatter == null) arrayFormatter = options.Resolver.GetFormatterWithVerify<JArray>();
-                        //arrayFormatter?.Serialize(ref writer, ja, options);
-                        MessagePackFormatter_JArray.Instance.Serialize(ref writer, ja, options);
-                        break;
-
-                    case JValue jv:
-                        switch (jv.Type)
-                        {
-                            case JTokenType.String: writer.Write(jv.Value<string>()); break;
-                            case JTokenType.Integer: writer.Write(jv.Value<long>()); break;
-                            case JTokenType.Float: writer.Write(jv.Value<double>()); break;
-                            case JTokenType.Boolean: writer.Write(jv.Value<bool>()); break;
-                            case JTokenType.Date: writer.Write(jv.Value<DateTime>().ToString("yyyy-MM-dd HH:mm:ss")); break;
-                            default: writer.Write(Serialization_Newtonsoft.Instance.SerializeToString(jv)); break;
-                        }
-                        break;
-                    default:
-                        string str = Serialization_Newtonsoft.Instance.SerializeToString(kv.Value);
-                        writer.Write(str);
-                        break;
-                }
-            }
-        }
-
+                writer.WriteMapHeader(value.Count);
 
-
-        [MethodImpl(MethodImplOptions.AggressiveInlining)]
-        public Newtonsoft.Json.Linq.JObject Deserialize(
-          ref MessagePackReader reader, MessagePackSerializerOptions options)
-        {
-            if (reader.TryReadNil())
-            {
-                return default;
-            }
-
-            var result = new JObject();
-            int count = reader.ReadMapHeader();
-            if (count == 0) return result;
-
-            options.Security.DepthStep(ref reader);
-            try
-            {
-                for (int i = 0; i < count; i++)
+                foreach (var kv in value)
                 {
-                    string key = reader.ReadString();
+                    //key
+                    writer.Write(kv.Key);
+
 
-                    switch (reader.NextMessagePackType)
+                    //value
+                    if (kv.Value.IsNull())
                     {
-                        case MessagePackType.Map:                
-                            result[key] = Deserialize(ref reader, options);
+                        writer.WriteNil();
+                        continue;
+                    }
 
+                    switch (kv.Value)
+                    {
+                        case JObject jo:
+                            //if (objectFormatter == null) objectFormatter = options.Resolver.GetFormatterWithVerify<JObject>();
+                            //objectFormatter?.Serialize(ref writer, jo, options); 
+                            Serialize(ref writer, jo, options);
                             break;
-                        case MessagePackType.Array:                  
-                            result[key] = MessagePackFormatter_JArray.Instance.Deserialize(ref reader, options);
+                        case JArray ja:
+                            //if (arrayFormatter == null) arrayFormatter = options.Resolver.GetFormatterWithVerify<JArray>();
+                            //arrayFormatter?.Serialize(ref writer, ja, options);
+                            MessagePackFormatter_JArray.Instance.Serialize(ref writer, ja, options);
                             break;
-                        case MessagePackType.Integer: result[key] = reader.ReadInt64(); break;
-                        case MessagePackType.Boolean: result[key] = reader.ReadBoolean(); break;
-                        case MessagePackType.Float: result[key] = reader.ReadDouble(); break;
-                        case MessagePackType.String: result[key] = reader.ReadString(); break;
-                        case MessagePackType.Nil:
-                            result[key] = null;
-                            reader.Skip();
+
+                        case JValue jv:
+                            switch (jv.Type)
+                            {
+                                case JTokenType.String: writer.Write(jv.Value<string>()); break;
+                                case JTokenType.Integer: writer.Write(jv.Value<long>()); break;
+                                case JTokenType.Float: writer.Write(jv.Value<double>()); break;
+                                case JTokenType.Boolean: writer.Write(jv.Value<bool>()); break;
+                                case JTokenType.Date: writer.Write(jv.Value<DateTime>().ToString("yyyy-MM-dd HH:mm:ss")); break;
+                                default: writer.Write(Serialization_Newtonsoft.Instance.SerializeToString(jv)); break;
+                            }
                             break;
                         default:
-                            result[key] = null;
-                            reader.Skip();
+                            string str = Serialization_Newtonsoft.Instance.SerializeToString(kv.Value);
+                            writer.Write(str);
                             break;
                     }
-
                 }
             }
-            finally
-            {
-                reader.Depth--;
-            }
-
-            return result;
-        }
-    }
-
-    class MessagePackFormatter_JArray : IMessagePackFormatter<JArray>
-    {
-        public static readonly MessagePackFormatter_JArray Instance = new MessagePackFormatter_JArray();
-
-
-        [MethodImpl(MethodImplOptions.AggressiveInlining)]
-        public void Serialize(
-          ref MessagePackWriter writer, Newtonsoft.Json.Linq.JArray value, MessagePackSerializerOptions options)
-        {
-            if (value.IsNull())
-            {
-                writer.WriteNil();
-                return;
-            }
 
 
-            writer.WriteArrayHeader(value.Count);
 
-            foreach (var token in value)
+            [MethodImpl(MethodImplOptions.AggressiveInlining)]
+            public Newtonsoft.Json.Linq.JObject Deserialize(
+              ref MessagePackReader reader, MessagePackSerializerOptions options)
             {
-                if (token.IsNull())
+                if (reader.TryReadNil())
                 {
-                    writer.WriteNil();
-                    continue;
+                    return default;
                 }
-                switch (token)
+
+                var result = new JObject();
+                int count = reader.ReadMapHeader();
+                if (count == 0) return result;
+
+                options.Security.DepthStep(ref reader);
+                try
                 {
-                    case JObject jo:                      
-                        MessagePackFormatter_JObject.Instance.Serialize(ref writer, jo, options);
-                        break;
-                    case JArray ja:                     
-                        Serialize(ref writer, ja, options);
-                        break;
-                    case JValue jv:
-                        switch (jv.Type)
+                    for (int i = 0; i < count; i++)
+                    {
+                        string key = reader.ReadString();
+
+                        switch (reader.NextMessagePackType)
                         {
-                            case JTokenType.String: writer.Write(jv.Value<string>()); break;
-                            case JTokenType.Integer: writer.Write(jv.Value<long>()); break;
-                            case JTokenType.Float: writer.Write(jv.Value<double>()); break;
-                            case JTokenType.Boolean: writer.Write(jv.Value<bool>()); break;
-                            case JTokenType.Date: writer.Write(jv.Value<DateTime>().ToString("yyyy-MM-dd HH:mm:ss")); break;
-                            default: writer.Write(Serialization_Newtonsoft.Instance.SerializeToString(jv)); break;
+                            case MessagePackType.Map:
+                                result[key] = Deserialize(ref reader, options);
+
+                                break;
+                            case MessagePackType.Array:
+                                result[key] = MessagePackFormatter_JArray.Instance.Deserialize(ref reader, options);
+                                break;
+                            case MessagePackType.Integer: result[key] = reader.ReadInt64(); break;
+                            case MessagePackType.Boolean: result[key] = reader.ReadBoolean(); break;
+                            case MessagePackType.Float: result[key] = reader.ReadDouble(); break;
+                            case MessagePackType.String: result[key] = reader.ReadString(); break;
+                            case MessagePackType.Nil:
+                                result[key] = null;
+                                reader.Skip();
+                                break;
+                            default:
+                                result[key] = null;
+                                reader.Skip();
+                                break;
                         }
-                        break;
-                    default:
-                        string str = Serialization_Newtonsoft.Instance.SerializeToString(token);
-                        writer.Write(str);
-                        break;
+
+                    }
+                }
+                finally
+                {
+                    reader.Depth--;
                 }
 
+                return result;
             }
         }
 
-
-        [MethodImpl(MethodImplOptions.AggressiveInlining)]
-        public Newtonsoft.Json.Linq.JArray Deserialize(
-          ref MessagePackReader reader, MessagePackSerializerOptions options)
+        public class MessagePackFormatter_JArray : IMessagePackFormatter<JArray>
         {
-            if (reader.TryReadNil())
+            public static readonly MessagePackFormatter_JArray Instance = new MessagePackFormatter_JArray();
+
+
+            [MethodImpl(MethodImplOptions.AggressiveInlining)]
+            public void Serialize(
+              ref MessagePackWriter writer, Newtonsoft.Json.Linq.JArray value, MessagePackSerializerOptions options)
             {
-                return default;
-            }
+                if (value.IsNull())
+                {
+                    writer.WriteNil();
+                    return;
+                }
 
-            var result = new JArray();
-            int count = reader.ReadArrayHeader();
-            if (count == 0) return result;
 
-            options.Security.DepthStep(ref reader);
-            try
-            {       
+                writer.WriteArrayHeader(value.Count);
 
-                for (int i = 0; i < count; i++)
+                foreach (var token in value)
                 {
-
-                    switch (reader.NextMessagePackType)
+                    if (token.IsNull())
                     {
-                        case MessagePackType.Map:                          
-                            result.Add(MessagePackFormatter_JObject.Instance.Deserialize(ref reader, options));
+                        writer.WriteNil();
+                        continue;
+                    }
+                    switch (token)
+                    {
+                        case JObject jo:
+                            MessagePackFormatter_JObject.Instance.Serialize(ref writer, jo, options);
                             break;
-                        case MessagePackType.Array:                           
-                            result.Add(Deserialize(ref reader, options));
+                        case JArray ja:
+                            Serialize(ref writer, ja, options);
                             break;
-                        case MessagePackType.Integer: result.Add(reader.ReadInt64()); break;
-                        case MessagePackType.Boolean: result.Add(reader.ReadBoolean()); break;
-                        case MessagePackType.Float: result.Add(reader.ReadDouble()); break;
-                        case MessagePackType.String: result.Add(reader.ReadString()); break;
-                        case MessagePackType.Nil:
-                            result.Add(null);
-                            reader.Skip();
+                        case JValue jv:
+                            switch (jv.Type)
+                            {
+                                case JTokenType.String: writer.Write(jv.Value<string>()); break;
+                                case JTokenType.Integer: writer.Write(jv.Value<long>()); break;
+                                case JTokenType.Float: writer.Write(jv.Value<double>()); break;
+                                case JTokenType.Boolean: writer.Write(jv.Value<bool>()); break;
+                                case JTokenType.Date: writer.Write(jv.Value<DateTime>().ToString("yyyy-MM-dd HH:mm:ss")); break;
+                                default: writer.Write(Serialization_Newtonsoft.Instance.SerializeToString(jv)); break;
+                            }
                             break;
                         default:
-                            result.Add(null);
-                            reader.Skip();
+                            string str = Serialization_Newtonsoft.Instance.SerializeToString(token);
+                            writer.Write(str);
                             break;
                     }
 
                 }
             }
-            finally
+
+
+            [MethodImpl(MethodImplOptions.AggressiveInlining)]
+            public Newtonsoft.Json.Linq.JArray Deserialize(
+              ref MessagePackReader reader, MessagePackSerializerOptions options)
             {
-                reader.Depth--;
-            }
+                if (reader.TryReadNil())
+                {
+                    return default;
+                }
 
+                var result = new JArray();
+                int count = reader.ReadArrayHeader();
+                if (count == 0) return result;
 
-            return result;
-        }
-    }
+                options.Security.DepthStep(ref reader);
+                try
+                {
+
+                    for (int i = 0; i < count; i++)
+                    {
+
+                        switch (reader.NextMessagePackType)
+                        {
+                            case MessagePackType.Map:
+                                result.Add(MessagePackFormatter_JObject.Instance.Deserialize(ref reader, options));
+                                break;
+                            case MessagePackType.Array:
+                                result.Add(Deserialize(ref reader, options));
+                                break;
+                            case MessagePackType.Integer: result.Add(reader.ReadInt64()); break;
+                            case MessagePackType.Boolean: result.Add(reader.ReadBoolean()); break;
+                            case MessagePackType.Float: result.Add(reader.ReadDouble()); break;
+                            case MessagePackType.String: result.Add(reader.ReadString()); break;
+                            case MessagePackType.Nil:
+                                result.Add(null);
+                                reader.Skip();
+                                break;
+                            default:
+                                result.Add(null);
+                                reader.Skip();
+                                break;
+                        }
 
-    #endregion
+                    }
+                }
+                finally
+                {
+                    reader.Depth--;
+                }
 
 
+                return result;
+            }
+        }
 
+        #endregion
+        
 
 
 
 
 
 
-    #region MessagePackFormatter_NewtonsoftAsString
-    class MessagePackFormatter_NewtonsoftAsString<NewtonsoftType> : IMessagePackFormatter<NewtonsoftType>
-    //where NewtonsoftType: Newtonsoft.Json.Linq.JToken
-    {
-        [MethodImpl(MethodImplOptions.AggressiveInlining)]
-        public void Serialize(
-          ref MessagePackWriter writer, NewtonsoftType value, MessagePackSerializerOptions options)
+
+        #region MessagePackFormatter_NewtonsoftAsString
+        public class MessagePackFormatter_NewtonsoftAsString<NewtonsoftType> : IMessagePackFormatter<NewtonsoftType>
+        //where NewtonsoftType: Newtonsoft.Json.Linq.JToken
         {
-            if (value == null)
+            [MethodImpl(MethodImplOptions.AggressiveInlining)]
+            public void Serialize(
+              ref MessagePackWriter writer, NewtonsoftType value, MessagePackSerializerOptions options)
             {
-                writer.WriteNil();
-                return;
-            }
+                if (value == null)
+                {
+                    writer.WriteNil();
+                    return;
+                }
 
-            var str = Serialization_Newtonsoft.Instance.SerializeToString(value);
-            writer.Write(str);
-        }
+                var str = Serialization_Newtonsoft.Instance.SerializeToString(value);
+                writer.Write(str);
+            }
 
 
-        [MethodImpl(MethodImplOptions.AggressiveInlining)]
-        public NewtonsoftType Deserialize(
-          ref MessagePackReader reader, MessagePackSerializerOptions options)
-        {
-            if (reader.TryReadNil())
+            [MethodImpl(MethodImplOptions.AggressiveInlining)]
+            public NewtonsoftType Deserialize(
+              ref MessagePackReader reader, MessagePackSerializerOptions options)
             {
-                return default;
-            }
+                if (reader.TryReadNil())
+                {
+                    return default;
+                }
 
-            options.Security.DepthStep(ref reader);
+                options.Security.DepthStep(ref reader);
 
-            var data = reader.ReadString();
+                var data = reader.ReadString();
 
-            reader.Depth--;
+                reader.Depth--;
 
-            return Newtonsoft.Json.JsonConvert.DeserializeObject<NewtonsoftType>(data);
+                return Newtonsoft.Json.JsonConvert.DeserializeObject<NewtonsoftType>(data);
+            }
         }
-    }
 
 
-    #endregion
+        #endregion
+    }
 }
 
 #endregion