/// <summary>
 /// Serialize to binary with default resolver.
 /// </summary>
 public override void Serialize <T>(ref MessagePackWriter writer, T value, IFormatterResolver resolver = null)
 {
     using (var scratch = new Nerdbank.Streams.Sequence <byte>())
     {
         var scratchWriter = writer.Clone(scratch);
         base.Serialize(ref scratchWriter, value, resolver);
         scratchWriter.Flush();
         ToLZ4BinaryCore(scratch.AsReadOnlySequence, ref writer);
     }
 }
示例#2
0
 /// <summary>
 /// From Json String to LZ4MessagePack binary
 /// </summary>
 public override void ConvertFromJson(TextReader reader, ref MessagePackWriter writer)
 {
     using (var scratch = new Nerdbank.Streams.Sequence <byte>())
     {
         var scratchWriter = writer.Clone(scratch);
         base.ConvertFromJson(reader, ref scratchWriter);
         scratchWriter.Flush();
         ToLZ4BinaryCore(scratch.AsReadOnlySequence, ref writer);
     }
 }
 public override T Deserialize <T>(ref MessagePackReader reader, IFormatterResolver resolver = null)
 {
     using (var msgPackUncompressed = new Nerdbank.Streams.Sequence <byte>())
     {
         if (TryDecompress(ref reader, msgPackUncompressed))
         {
             var uncompressedReader = reader.Clone(msgPackUncompressed.AsReadOnlySequence);
             return(base.Deserialize <T>(ref uncompressedReader, resolver));
         }
         else
         {
             return(base.Deserialize <T>(ref reader, resolver));
         }
     }
 }
示例#4
0
 /// <summary>
 /// Dump message-pack binary to JSON string.
 /// </summary>
 public override void ConvertToJson(ref MessagePackReader reader, TextWriter jsonWriter)
 {
     using (var scratch = new Nerdbank.Streams.Sequence <byte>())
     {
         if (TryDecompress(ref reader, scratch))
         {
             var scratchReader = new MessagePackReader(scratch.AsReadOnlySequence);
             base.ConvertToJson(ref scratchReader, jsonWriter);
         }
         else
         {
             base.ConvertToJson(ref reader, jsonWriter);
         }
     }
 }
        public void Serialize(ref MessagePackWriter writer, object value, MessagePackSerializerOptions options)
        {
            if (value == null)
            {
                writer.WriteNil();
                return;
            }

            Type type = value.GetType();

            byte[] typeName;
            if (!this.typeNameCache.TryGetValue(type, out typeName))
            {
                if (BlacklistCheck.Contains(type.FullName))
                {
                    throw new InvalidOperationException("Type is in blacklist:" + type.FullName);
                }

                TypeInfo ti = type.GetTypeInfo();
                if (ti.IsAnonymous() || UseBuiltinTypes.Contains(type))
                {
                    typeName = null;
                }
                else
                {
                    typeName = StringEncoding.UTF8.GetBytes(this.BuildTypeName(type));
                }

                this.typeNameCache.TryAdd(type, typeName);
            }

            if (typeName == null)
            {
                Resolvers.TypelessFormatterFallbackResolver.Instance.GetFormatter <object>().Serialize(ref writer, value, options);
                return;
            }

            // don't use GetOrAdd for avoid closure capture.
            KeyValuePair <object, SerializeMethod> formatterAndDelegate;

            if (!this.serializers.TryGetValue(type, out formatterAndDelegate))
            {
                // double check locking...
                lock (this.serializers)
                {
                    if (!this.serializers.TryGetValue(type, out formatterAndDelegate))
                    {
                        TypeInfo ti = type.GetTypeInfo();

                        IFormatterResolver resolver = options.Resolver;
                        var formatter = resolver.GetFormatterDynamic(type);
                        if (formatter == null)
                        {
                            throw new FormatterNotRegisteredException(type.FullName + " is not registered in this resolver. resolver:" + resolver.GetType().Name);
                        }

                        Type formatterType         = typeof(IMessagePackFormatter <>).MakeGenericType(type);
                        ParameterExpression param0 = Expression.Parameter(typeof(object), "formatter");
                        ParameterExpression param1 = Expression.Parameter(typeof(MessagePackWriter).MakeByRefType(), "writer");
                        ParameterExpression param2 = Expression.Parameter(typeof(object), "value");
                        ParameterExpression param3 = Expression.Parameter(typeof(MessagePackSerializerOptions), "options");

                        MethodInfo serializeMethodInfo = formatterType.GetRuntimeMethod("Serialize", new[] { typeof(MessagePackWriter).MakeByRefType(), type, typeof(MessagePackSerializerOptions) });

                        MethodCallExpression body = Expression.Call(
                            Expression.Convert(param0, formatterType),
                            serializeMethodInfo,
                            param1,
                            ti.IsValueType ? Expression.Unbox(param2, type) : Expression.Convert(param2, type),
                            param3);

                        SerializeMethod lambda = Expression.Lambda <SerializeMethod>(body, param0, param1, param2, param3).Compile();

                        formatterAndDelegate = new KeyValuePair <object, SerializeMethod>(formatter, lambda);
                        this.serializers.TryAdd(type, formatterAndDelegate);
                    }
                }
            }

            // mark will be written at the end, when size is known
            using (var scratch = new Nerdbank.Streams.Sequence <byte>())
            {
                MessagePackWriter scratchWriter = writer.Clone(scratch);
                scratchWriter.WriteString(typeName);
                formatterAndDelegate.Value(formatterAndDelegate.Key, ref scratchWriter, value, options);
                scratchWriter.Flush();

                // mark as extension with code 100
                writer.WriteExtensionFormat(new ExtensionResult((sbyte)TypelessFormatter.ExtensionTypeCode, scratch.AsReadOnlySequence));
            }
        }
示例#6
0
        private static void Benchmark <T>(T target)
        {
            const int Iteration = 10000;

            Console.WriteLine("Running {0} iterations...", Iteration);

            var jsonSerializer = new JsonSerializer();

            MsgPack.Serialization.SerializationContext msgpack = MsgPack.Serialization.SerializationContext.Default;
            msgpack.GetSerializer <T>().PackSingleObject(target);
            MessagePackSerializer.Serialize(target);
            MessagePackSerializer.Serialize(target, MessagePackSerializerOptions.LZ4Default);
            ZeroFormatter.ZeroFormatterSerializer.Serialize(target);
            ProtoBuf.Serializer.Serialize(new MemoryStream(), target);
            jsonSerializer.Serialize(new JsonTextWriter(new StringWriter()), target);

            Console.WriteLine(typeof(T).Name + " serialization test");
            Console.WriteLine();

            Console.WriteLine("Serialize::");
            Deserializing = false;

            byte[] data  = null;
            var    data0 = new Nerdbank.Streams.Sequence <byte>();

            byte[] data1        = null;
            byte[] data2        = null;
            byte[] data3        = null;
            byte[] dataJson     = null;
            byte[] dataGzipJson = null;

            using (new Measure("MessagePack for C#"))
            {
                for (int i = 0; i < Iteration; i++)
                {
                    data0.Reset();
                    MessagePackSerializer.Serialize(data0, target);
                }
            }

            using (new Measure("MessagePack for C# (LZ4)"))
            {
                for (int i = 0; i < Iteration; i++)
                {
                    data3 = MessagePackSerializer.Serialize(target, MessagePackSerializerOptions.LZ4Default);
                }
            }

            using (new Measure("MsgPack-Cli"))
            {
                for (int i = 0; i < Iteration; i++)
                {
                    data = msgpack.GetSerializer <T>().PackSingleObject(target);
                }
            }

            using (new Measure("protobuf-net"))
            {
                for (int i = 0; i < Iteration; i++)
                {
                    using (var ms = new MemoryStream())
                    {
                        ProtoBuf.Serializer.Serialize(ms, target);
                    }
                }
            }

            using (new Measure("ZeroFormatter"))
            {
                for (int i = 0; i < Iteration; i++)
                {
                    data1 = ZeroFormatter.ZeroFormatterSerializer.Serialize(target);
                }
            }

            using (new Measure("Json.NET"))
            {
                for (int i = 0; i < Iteration; i++)
                {
                    using (var ms = new MemoryStream())
                        using (var sw = new StreamWriter(ms, Encoding.UTF8, 1024, true))
                            using (var jw = new JsonTextWriter(sw))
                            {
                                jsonSerializer.Serialize(jw, target);
                            }
                }
            }

            using (new Measure("Json.NET(+GZip)"))
            {
                for (int i = 0; i < Iteration; i++)
                {
                    using (var ms = new MemoryStream())
                        using (var gzip = new GZipStream(ms, CompressionLevel.Fastest))
                            using (var sw = new StreamWriter(gzip, Encoding.UTF8, 1024, true))
                                using (var jw = new JsonTextWriter(sw))
                                {
                                    jsonSerializer.Serialize(jw, target);
                                }
                }
            }

            using (var ms = new MemoryStream())
            {
                ProtoBuf.Serializer.Serialize(ms, target);
                data2 = ms.ToArray();
            }

            using (var ms = new MemoryStream())
            {
                using (var sw = new StreamWriter(ms, Encoding.UTF8, 1024, true))
                    using (var jw = new JsonTextWriter(sw))
                    {
                        jsonSerializer.Serialize(jw, target);
                    }

                dataJson = ms.ToArray();
            }

            using (var ms = new MemoryStream())
            {
                using (var gzip = new GZipStream(ms, CompressionLevel.Fastest))
                    using (var sw = new StreamWriter(gzip, Encoding.UTF8, 1024, true))
                        using (var jw = new JsonTextWriter(sw))
                        {
                            jsonSerializer.Serialize(jw, target);
                        }

                dataGzipJson = ms.ToArray();
            }

            msgpack.GetSerializer <T>().UnpackSingleObject(data);
            MessagePackSerializer.Deserialize <T>(data0);
            ////ZeroFormatterSerializer.Deserialize<T>(data1);
            ProtoBuf.Serializer.Deserialize <T>(new MemoryStream(data2));
            MessagePackSerializer.Deserialize <T>(data3, MessagePackSerializerOptions.LZ4Default);
            jsonSerializer.Deserialize <T>(new JsonTextReader(new StreamReader(new MemoryStream(dataJson))));

            Console.WriteLine();
            Console.WriteLine("Deserialize::");
            Deserializing = true;

            using (new Measure("MessagePack for C#"))
            {
                for (int i = 0; i < Iteration; i++)
                {
                    MessagePackSerializer.Deserialize <T>(data0);
                }
            }

            using (new Measure("MessagePack for C# (LZ4)"))
            {
                for (int i = 0; i < Iteration; i++)
                {
                    MessagePackSerializer.Deserialize <T>(data3, MessagePackSerializerOptions.LZ4Default);
                }
            }

            using (new Measure("MsgPack-Cli"))
            {
                for (int i = 0; i < Iteration; i++)
                {
                    msgpack.GetSerializer <T>().UnpackSingleObject(data);
                }
            }

            using (new Measure("protobuf-net"))
            {
                for (int i = 0; i < Iteration; i++)
                {
                    using (var ms = new MemoryStream(data2))
                    {
                        ProtoBuf.Serializer.Deserialize <T>(ms);
                    }
                }
            }

            using (new Measure("ZeroFormatter"))
            {
                for (int i = 0; i < Iteration; i++)
                {
                    ZeroFormatterSerializer.Deserialize <T>(data1);
                }
            }

            using (new Measure("Json.NET"))
            {
                for (int i = 0; i < Iteration; i++)
                {
                    using (var ms = new MemoryStream(dataJson))
                        using (var sr = new StreamReader(ms, Encoding.UTF8))
                            using (var jr = new JsonTextReader(sr))
                            {
                                jsonSerializer.Deserialize <T>(jr);
                            }
                }
            }

            using (new Measure("Json.NET(+GZip)"))
            {
                for (int i = 0; i < Iteration; i++)
                {
                    using (var ms = new MemoryStream(dataGzipJson))
                        using (var gzip = new GZipStream(ms, CompressionMode.Decompress))
                            using (var sr = new StreamReader(gzip, Encoding.UTF8))
                                using (var jr = new JsonTextReader(sr))
                                {
                                    jsonSerializer.Deserialize <T>(jr);
                                }
                }
            }

            Console.WriteLine();
            Console.WriteLine("FileSize::");
            var label = string.Empty;

            label = "MessagePack for C#";
            Console.WriteLine($"{label,-25} {data0.Length,14} byte");
            label = "MessagePack for C# (LZ4)";
            Console.WriteLine($"{label,-25} {data3.Length,14} byte");
            label = "MsgPack-Cli";
            Console.WriteLine($"{label,-25} {data.Length,14} byte");
            label = "protobuf-net";
            Console.WriteLine($"{label,-25} {data2.Length,14} byte");
            label = "ZeroFormatter";
            Console.WriteLine($"{label,-25} {data1.Length,14} byte");
            label = "Json.NET";
            Console.WriteLine($"{label,-25} {dataJson.Length,14} byte");
            label = "Json.NET(+GZip)";
            Console.WriteLine($"{label,-25} {dataGzipJson.Length,14} byte");

            Console.WriteLine();
            Console.WriteLine();
        }