lith há 4 anos atrás
pai
commit
e7c768e96a

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

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

+ 139 - 0
dotnet/Library/Sers/Sers.Core/Sers.Core/Module/Rpc/Serialization/MessagePackFormatter_Newtonsoft_Object.cs

@@ -0,0 +1,139 @@
+using System;
+using System.Runtime.CompilerServices;
+using MessagePack.Formatters;
+using MessagePack;
+using Newtonsoft.Json.Linq;
+using Vit.Core.Module.Serialization;
+using Vit.Extensions;
+
+using static Vit.Core.Module.Serialization.Serialization_MessagePack;
+ 
+
+namespace Sers.Core.Module.Rpc.Serialize
+{
+    public class MessagePackFormatter_Newtonsoft_Object : IMessagePackFormatter<object>
+    {
+
+        public static readonly MessagePackFormatter_Newtonsoft_Object Instance = new MessagePackFormatter_Newtonsoft_Object();
+
+        [MethodImpl(MethodImplOptions.AggressiveInlining)]
+        public void Serialize(
+          ref MessagePackWriter writer, object value_, MessagePackSerializerOptions options)
+        {
+            if (value_ == null)
+            {
+                writer.WriteNil();
+                return;
+            }
+
+
+            Newtonsoft.Json.Linq.JObject value = value_ as JObject;
+            if (value.IsNull())
+            {
+                writer.WriteNil();
+                return;
+            }
+
+            writer.WriteMapHeader(value.Count);
+
+            foreach (var kv in value)
+            {
+                //key
+                writer.Write(kv.Key);
+
+
+                //value
+                if (kv.Value.IsNull())
+                {
+                    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 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;
+                }
+            }
+        }
+
+
+
+        [MethodImpl(MethodImplOptions.AggressiveInlining)]
+        public object 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++)
+                {
+                    string key = reader.ReadString();
+
+                    switch (reader.NextMessagePackType)
+                    {
+                        case MessagePackType.Map:
+                            result[key] = Deserialize(ref reader, options) as JObject;
+
+                            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;
+                    }
+
+                }
+            }
+            finally
+            {
+                reader.Depth--;
+            }
+
+            return result;
+        }
+    }
+}

+ 45 - 0
dotnet/Library/Sers/Sers.Core/Sers.Core/Module/Rpc/Serialization/MessagePackFormatter_RpcContextData.cs

@@ -0,0 +1,45 @@
+using System;
+using System.Runtime.CompilerServices;
+using MessagePack.Formatters;
+using MessagePack;
+using Newtonsoft.Json.Linq;
+using Vit.Core.Module.Serialization;
+using Vit.Extensions;
+ 
+
+namespace Sers.Core.Module.Rpc.Serialize
+{
+    public class MessagePackFormatter_RpcContextData : IMessagePackFormatter<RpcContextData>
+    {
+
+        public static readonly MessagePackFormatter_Newtonsoft_Object Instance = new MessagePackFormatter_Newtonsoft_Object();
+
+        [MethodImpl(MethodImplOptions.AggressiveInlining)]
+        public void Serialize(
+          ref MessagePackWriter writer, RpcContextData value_, MessagePackSerializerOptions options)
+        {
+            if (value_ == null)
+            {
+                writer.WriteNil();
+                return;
+            }
+
+ 
+        }
+
+
+
+        [MethodImpl(MethodImplOptions.AggressiveInlining)]
+        public RpcContextData Deserialize(
+          ref MessagePackReader reader, MessagePackSerializerOptions options)
+        {
+            if (reader.TryReadNil())
+            {
+                return default;
+            }
+
+   
+            return default;
+        }
+    }
+}

+ 23 - 16
dotnet/Library/Vit/Vit.Core/Vit.Core/Module/Serialization/Serialization_MessagePack.cs

@@ -17,30 +17,37 @@ namespace Vit.Core.Module.Serialization
 
 
 
-        public readonly MessagePackSerializerOptions options;
+        public  MessagePackSerializerOptions options;
         public Serialization_MessagePack()
         {
- 
-            var resolver = MessagePack.Resolvers.CompositeResolver.Create(
-                new IMessagePackFormatter[]
-                {  
+            options = MessagePack.Resolvers.ContractlessStandardResolver.Options;
 
+            //CompatibleWithNewtonsoft();
+        }
+
+        /// <summary>
+        /// 适配Newtonsoft 如 JObject JArray类型
+        /// </summary>
+        public void CompatibleWithNewtonsoft()
+        {
+
+            var resolver = MessagePack.Resolvers.CompositeResolver.Create(
+              new IMessagePackFormatter[]
+              {
                     MessagePackFormatter_JObject.Instance,
                     MessagePackFormatter_JArray.Instance,
 
                     //NilFormatter.Instance,
                     //new IgnoreFormatter<MethodBase>(),
-                
-                 },
-                new IFormatterResolver[]
-                {                 
-                     //ContractlessStandardResolver.Options.Resolver,
-                     ContractlessStandardResolver.Instance
-                });
-
-            options = MessagePack.Resolvers.ContractlessStandardResolver.Options.WithResolver(resolver);
-
-            //options = MessagePack.Resolvers.ContractlessStandardResolver.Options;
+              },
+              new IFormatterResolver[]
+              {                 
+                    // ContractlessStandardResolver.Instance,
+                    // ContractlessStandardResolver.Options.Resolver,
+                    options.Resolver
+              });
+
+            options = options.WithResolver(resolver);
         }