RendleLabs / SerializerTests

.NET Serializer testing framework
GNU General Public License v3.0
0 stars 0 forks source link

MessagePack #1

Open ThatRendle opened 7 years ago

ThatRendle commented 7 years ago

MessagePack.cs

using MessagePack;
using System;
using System.IO;

namespace SerializerTests
{
    public class MessagePack<T> : TestBase<T, IFormatterResolver> where T : class
    {
        public MessagePack(Func<int, T> testData)
        {
            base.CreateNTestData = testData;
            FormatterFactory = () => MessagePackSerializer.DefaultResolver;
        }
        protected override T Deserialize(Stream stream)
        {
            return MessagePackSerializer.Deserialize<T>(stream);
        }

        protected override void Serialize(T obj, Stream stream)
        {
            MessagePackSerializer.Serialize(stream, obj);
        }
    }
}

BookShelf.cs

using MessagePack;
using ProtoBuf;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.Text;
using System.Threading.Tasks;

namespace SerializerTests.TypesToSerialize
{
    [Serializable, DataContract, ProtoContract, MessagePackObject]
    public class BookShelf
    {
        [DataMember, ProtoMember(1), Key(0)]
        public List<Book> Books
        {
            get;
            set;
        }

        [DataMember, ProtoMember(2), Key(1)]
        private string Secret;

        public BookShelf(string secret)
        {
            Secret = secret;
        }

        public BookShelf() // Parameterless ctor is needed for every protocol buffer class during deserialization
        { }
    }

    [Serializable,DataContract,ProtoContract, MessagePackObject]
    public class Book
    {
        [DataMember, ProtoMember(1), Key(0)]
        public string Title;

        [DataMember, ProtoMember(2), Key(1)]
        public int Id;
    }

}

BookShelf1.cs

using MessagePack;
using ProtoBuf;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.Text;
using System.Threading.Tasks;

namespace SerializerTests.TypesToSerialize
{
    [Serializable, DataContract, ProtoContract, MessagePackObject]
    public class BookShelf1
    {
        [DataMember, ProtoMember(1), Key(0)]
        public List<Book1> Books
        {
            get;
            set;
        }

        [DataMember, ProtoMember(2), Key(1)]
        private string Secret;

        public BookShelf1(string secret)
        {
            Secret = secret;
        }

        public BookShelf1() // Parameterless ctor is needed for every protocol buffer class during deserialization
        { }
    }

    [Serializable, DataContract, ProtoContract, MessagePackObject]
    public class Book1
    {
        [DataMember, ProtoMember(1), Key(0)]
        public string Title;

        [DataMember, ProtoMember(2), Key(1)]
        public int Id;
    }
}

BookShelf2.cs

using MessagePack;
using ProtoBuf;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.Text;
using System.Threading.Tasks;

namespace SerializerTests.TypesToSerialize
{
    [Serializable, DataContract, ProtoContract, MessagePackObject]
    public class BookShelf2
    {
        [DataMember, ProtoMember(1), Key(0)]
        public List<Book2> Books
        {
            get;
            set;
        }

        [DataMember, ProtoMember(2), Key(1)]
        private string Secret;

        public BookShelf2(string secret)
        {
            Secret = secret;
        }

        public BookShelf2() // Parameterless ctor is needed for every protocol buffer class during deserialization
        { }
    }

    [Serializable, DataContract, ProtoContract, MessagePackObject]
    public class Book2
    {
        [DataMember, ProtoMember(1), Key(0)]
        public string Title;

        [DataMember, ProtoMember(2), Key(1)]
        public int Id;
    }
}

LargeBookShelf.cs

using MessagePack;
using ProtoBuf;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.Text;
using System.Threading.Tasks;

namespace SerializerTests.TypesToSerialize
{
    [Serializable, DataContract, ProtoContract, MessagePackObject]
    public class LargeBookShelf
    {
        [DataMember, ProtoMember(1), Key(0)]
        public List<LargeBook> Books
        {
            get;
            set;
        }

        [DataMember, ProtoMember(2), Key(1)]
        private string Secret;

        [DataMember, ProtoMember(3), Key(2)]
        private int Id;

        [DataMember, ProtoMember(4), Key(3)]
        private int Id4;

        [DataMember, ProtoMember(5), Key(4)]
        private double Id5;

        [DataMember, ProtoMember(6), Key(5)]
        private List<float> Id6;

        [DataMember, ProtoMember(7), Key(6)]
        private List<float> Id7;

        [DataMember, ProtoMember(8), Key(7)]
        private List<float> Id8;

        [DataMember, ProtoMember(9), Key(8)]
        private List<float> Id9;

        [DataMember, ProtoMember(10), Key(9)]
        private Dictionary<double, string> Id10;

        [DataMember, ProtoMember(11), Key(10)]
        private Dictionary<double, string> Id11;

        [DataMember, ProtoMember(12), Key(11)]
        private Dictionary<double, string> Id12;

        [DataMember, ProtoMember(13), Key(12)]
        private Dictionary<double, string> Id13;

        [DataMember, ProtoMember(14), Key(13)]
        private Dictionary<double, string> Id14 { get; set; }

        [DataMember, ProtoMember(15), Key(14)]
        private Dictionary<double, string> Id15 { get; set; }

        [DataMember, ProtoMember(16), Key(15)]
        private Dictionary<double, string> Id16 { get; set; }

        [DataMember, ProtoMember(17), Key(16)]
        private Dictionary<double, string> Id17 { get; set; }

        [DataMember, ProtoMember(18), Key(17)]
        private Dictionary<double, string> Id18 { get; set; }

        [DataMember, ProtoMember(19), Key(18)]
        private Dictionary<double, string> Id19 { get; set; }

        [DataMember, ProtoMember(20), Key(19)]
        private Dictionary<double, string> Id20 { get; set; }

        public LargeBookShelf(string secret)
        {
            Secret = secret;
        }

        public LargeBookShelf() // Parameterless ctor is needed for every protocol buffer class during deserialization
        { }
    }

    [Serializable, DataContract, ProtoContract]
    public class LargeBook
    {
        [DataMember, ProtoMember(1), Key(0)]
        public string Title;

        [DataMember, ProtoMember(2), Key(1)]
        public int Id;

        [DataMember, ProtoMember(3), Key(2)]
        private int Id3;

        [DataMember, ProtoMember(4), Key(3)]
        private int Id4;

        [DataMember, ProtoMember(5), Key(4)]
        private double Id5;

        [DataMember, ProtoMember(6), Key(5)]
        private List<float> Id6;

        [DataMember, ProtoMember(7), Key(6)]
        private List<float> Id7;

        [DataMember, ProtoMember(8), Key(7)]
        private List<float> Id8;

        [DataMember, ProtoMember(9), Key(8)]
        private List<float> Id9;

        [DataMember, ProtoMember(10), Key(9)]
        private Dictionary<double, string> Id10;

        [DataMember, ProtoMember(11), Key(10)]
        private Dictionary<double, string> Id11;

        [DataMember, ProtoMember(12), Key(11)]
        private Dictionary<double, string> Id12;

        [DataMember, ProtoMember(13), Key(12)]
        private Dictionary<double, string> Id13;

        [DataMember, ProtoMember(14), Key(13)]
        private Dictionary<double, string> Id14 { get; set; }

        [DataMember, ProtoMember(15), Key(14)]
        private Dictionary<double, string> Id15 { get; set; }

        [DataMember, ProtoMember(16), Key(15)]
        private Dictionary<double, string> Id16 { get; set; }

        [DataMember, ProtoMember(17), Key(16)]
        private Dictionary<double, string> Id17 { get; set; }

        [DataMember, ProtoMember(18), Key(17)]
        private Dictionary<double, string> Id18 { get; set; }

        [DataMember, ProtoMember(19), Key(18)]
        private Dictionary<double, string> Id19 { get; set; }

        [DataMember, ProtoMember(20), Key(19)]
        private Dictionary<double, string> Id20 { get; set; }
    }
}
ThatRendle commented 7 years ago

Program constructor

        public Program(string[] args)
        {
            Args = new Queue<string>(args);
            SerializersToTest = new List<ISerializeDeserializeTester>
            {
                new SlimSerializer<BookShelf>(Data),
                new FastJson<BookShelf>(Data),
                new Jil<BookShelf>(Data),
                new DataContractIndented<BookShelf>(Data),
                new DataContractBinaryXml<BookShelf>(Data),
                new DataContract<BookShelf>(Data),
                new XmlSerializer<BookShelf>(Data),
                new JsonNet<BookShelf>(Data),
                new Protobuf_net<BookShelf>(Data),
                new BinaryFormatter<BookShelf>(Data),
                new MessagePack<BookShelf>(Data),
            };

            StartupSerializersToTest = new List<ISerializeDeserializeTester>
            {
                new SlimSerializer<BookShelf>(Data),
                new SlimSerializer<BookShelf1>(Data1),
                new SlimSerializer<BookShelf2>(Data2),
                new SlimSerializer<LargeBookShelf>(DataLarge),

                new BinaryFormatter<BookShelf>(Data),
                new BinaryFormatter<BookShelf1>(Data1),
                new BinaryFormatter<BookShelf2>(Data2),
                new BinaryFormatter<LargeBookShelf>(DataLarge),

                new FastJson<BookShelf>(Data),
                new FastJson<BookShelf1>(Data1),
                new FastJson<BookShelf2>(Data2),
                new FastJson<LargeBookShelf>(DataLarge),

                new Jil<BookShelf>(Data),
                new Jil<BookShelf1>(Data1),
                new Jil<BookShelf2>(Data2),
                new Jil<LargeBookShelf>(DataLarge),

                new DataContract<BookShelf>(Data),
                new DataContract<BookShelf1>(Data1),
                new DataContract<BookShelf2>(Data2),
                new DataContract<LargeBookShelf>(DataLarge),

                new XmlSerializer<BookShelf>(Data),
                new XmlSerializer<BookShelf1>(Data1),
                new XmlSerializer<BookShelf2>(Data2),
                new XmlSerializer<LargeBookShelf>(DataLarge),

                new JsonNet<BookShelf>(Data),
                new JsonNet<BookShelf1>(Data1),
                new JsonNet<BookShelf2>(Data2),
                new JsonNet<LargeBookShelf>(DataLarge),

                new Protobuf_net<BookShelf>(Data),
                new Protobuf_net<BookShelf1>(Data1),
                new Protobuf_net<BookShelf2>(Data2),
                new Protobuf_net<LargeBookShelf>(DataLarge),

                new MessagePack<BookShelf>(Data),
                new MessagePack<BookShelf1>(Data1),
                new MessagePack<BookShelf2>(Data2),
                new MessagePack<LargeBookShelf>(DataLarge),
            };

        }