示例#1
0
        private void TestRoundTrip(int intVal)
        {
            byte[] payload = intVal.ToMsgPack();
            Assert.IsNotNull(payload);
            Assert.AreNotEqual(0, payload.Length);
            int restoredInt = MsgPackSerializer.Deserialize <int>(payload);

            Assert.AreEqual(intVal, restoredInt);
        }
示例#2
0
        public void DescendantConverterCanUseAscendant(ContextFixtureBase fixture)
        {
            IImageInfo expected = CreateTestObject();

            var actual = MsgPackSerializer.Deserialize <IMegaImageInfo>(MsgPackSerializer.Serialize(expected, fixture.OldContext), fixture.NewContext);

            actual.ShouldBeAssignableTo <IMegaImageInfo>();
            AssertEqual(actual, expected);
        }
示例#3
0
        public void True()
        {
            var data = new[] { (byte)DataTypes.True };

            MsgPackSerializer.Deserialize <bool?>(data).ShouldBe(true);

            var token = Helpers.CheckTokenDeserialization(data);

            ((bool?)token).ShouldBe(true);
        }
示例#4
0
        public void ReadNullAsNullableInt()
        {
            var data = new[] { (byte)DataTypes.Null };

            MsgPackSerializer.Deserialize <int?>(data).ShouldBe(null);

            var token = Helpers.CheckTokenDeserialization(data);

            ((int?)token).ShouldBe(null);
        }
示例#5
0
        public void TestDouble(double value, byte[] bytes)
        {
            var d = MsgPackSerializer.Deserialize <double>(bytes);

            d.ShouldBe(value);

            var token = Helpers.CheckTokenDeserialization(bytes);

            ((double)token).ShouldBe(value);
        }
示例#6
0
        private static void TestMsgPackSharp()
        {
            for (int i = 0; i < numMessagesToTest; i++)
            {
                byte[] buffer = MsgPackSerializer.SerializeObject(testMsg);
#pragma warning disable 0219
                var deserialized = MsgPackSerializer.Deserialize <AnimalMessage>(buffer);
#pragma warning restore 0219
            }
        }
        private void MsgPack_Common(JObject jObj, byte[] expected)
        {
            var serialized = _msgpack.Serialize(jObj);

            Console.WriteLine(BitConverter.ToString(serialized));
            Assert.AreEqual(serialized.GetType(), typeof(byte[]));
            Assert.AreEqual(serialized, expected);
            var deserialized = _msgpack.Deserialize(serialized);

            Assert.IsTrue(JObject.DeepEquals(jObj, deserialized));
        }
示例#8
0
        public List <STest> MessagePackDeserializerFromStream()
        {
            List <STest> data = null;

            for (var i = 0; i < N; i++)
            {
                _msgPackSerializerStream.Position = 0;
                data = _msgPackSerializer.Deserialize <List <STest> >(_msgPackSerializerStream);
            }
            return(data);
        }
示例#9
0
        public void TestAsMaps()
        {
            MsgPackSerializer.DefaultContext.SerializationMethod = SerializationMethod.Map;
            AnimalMessage msg = AnimalMessage.CreateTestMessage();

            byte[] payload = msg.ToMsgPack();
            Assert.IsNotNull(payload);
            Assert.AreNotEqual(0, payload.Length);
            AnimalMessage restored = MsgPackSerializer.Deserialize <AnimalMessage>(payload);

            VerifyAnimalMessage(msg, restored);
        }
示例#10
0
 private void RoundTrip(AnimalMessage input, int numTrips, bool verify)
 {
     for (int i = 0; i < numTrips; i++)
     {
         byte[] buffer = input.ToMsgPack();
         if (verify)
         {
             VerifyAnimalMessage(input, MsgPackSerializer.Deserialize <AnimalMessage>(buffer));
         }
         Interlocked.Increment(ref numVerified);
     }
 }
示例#11
0
        public void Hirarchical_Integration_test()
        {
            MyComplexObject testObj = new MyComplexObject()
            {
                Name         = "Root",
                Value        = 1,
                runtimeThing = 30,// should not be preserved
                ArrayOfInts  = new int[] { 1, 2, 3, 4 },
                ChildObject  = new MyComplexObject()
                {
                    Name               = "branch",
                    Value              = 3,
                    ArrayOfInts        = new int[] { 999, 200 },
                    ArrayOfChildObject = new MyComplexObject[] {
                        new MyComplexObject()
                        {
                            Name = "1st ArrayItem"
                        },
                        new MyComplexObject()
                        {
                            Name = "2nd ArrayItem"
                        },
                        new MyComplexObject()
                        {
                            Name = "3rd ArrayItem, with more than 31 chars (extra byte needed)"
                        },
                        null,
                        new MyComplexObject()
                        {
                            Name = "5th ArrayItem, with a string that takes more than 255 bytes so we need two bytes to contain the length of this nice and lengthy example, and we are not there yet... well it is a little chatty example I must admit, but we'll get there eventually, maybe, Yes!"
                        },
                    }
                },
                Date = new DateTime(2021, 3, 5, 11, 32, 20),
                NullableTypeWithValue = 3
            };

            byte[] buffer = MsgPackSerializer.Serialize(testObj);

            MyComplexObject ret = MsgPackSerializer.Deserialize <MyComplexObject>(buffer);

            Assert.AreEqual(testObj.Name, ret.Name);
            Assert.AreEqual(testObj.Value, ret.Value);
            Assert.AreNotEqual(testObj.runtimeThing, ret.runtimeThing);

            Assert.AreEqual(testObj.ChildObject.Name, ret.ChildObject.Name);
            Assert.AreEqual(testObj.ChildObject.ArrayOfChildObject[2].Name, ret.ChildObject.ArrayOfChildObject[2].Name);
            Assert.AreEqual(testObj.ChildObject.ArrayOfChildObject[3], ret.ChildObject.ArrayOfChildObject[3]);
            Assert.AreEqual(testObj.ChildObject.ArrayOfChildObject[4].Name, ret.ChildObject.ArrayOfChildObject[4].Name);
            Assert.AreEqual(testObj.NullableType, ret.NullableType);
            Assert.AreEqual(testObj.NullableTypeWithValue, ret.NullableTypeWithValue);
            Assert.AreEqual(testObj.Date, ret.Date);
        }
示例#12
0
        public void TestNulls()
        {
            MsgPackSerializer.DefaultContext.SerializationMethod = SerializationMethod.Array;
            var msg = AnimalMessage.CreateTestMessage();

            msg.AnimalColor = null;
            byte[] payload = msg.ToMsgPack();
            Assert.IsNotNull(payload);
            Assert.AreNotEqual(0, payload.Length);
            var restored = MsgPackSerializer.Deserialize <AnimalMessage>(payload);

            Assert.IsNull(restored.AnimalColor);
        }
示例#13
0
        public void MsgPack_Serialize_Deserialize_Test()
        {
            using (MsgPackSerializer serializer = new MsgPackSerializer())
            {
                Person person = new Person()
                {
                    Id = 2, Name = "Jerry", Address = new Address()
                };

                byte[] buffer = serializer.Serialize(person);

                Person _person = serializer.Deserialize <Person>(buffer);

                Assert.Equal(person.Id, _person.Id);
                Assert.Equal(person.Name, _person.Name);

                MessagePack <Person> pack = new MessagePack <Person>(person);

                buffer  = serializer.Serialize(pack);
                _person = serializer.Deserialize <MessagePack <Person> >(buffer)?.Message;

                Assert.Equal(person.Id, _person.Id);
                Assert.Equal(person.Name, _person.Name);

                Packet packet = new Packet(typeof(Person).FullName, buffer, PacketType.Join, Guid.NewGuid().ToString());

                buffer = serializer.Serialize(packet);
                Packet _packet = serializer.Deserialize <Packet>(buffer);

                Assert.Equal(packet.PackType, _packet.PackType);
                Assert.Equal(packet.PacketType, _packet.PacketType);
                Assert.Equal(packet.FromId, _packet.FromId);

                _person = serializer.Deserialize <MessagePack <Person> >(_packet.Payload)?.Message;

                Assert.Equal(person.Id, _person.Id);
                Assert.Equal(person.Name, _person.Name);
            }
        }
        private int?GetPacketSize()
        {
            if (_readingOffset - _parsingOffset < Constants.PacketSizeBufferSize)
            {
                return(null);
            }

            var headerSizeBuffer = new byte[Constants.PacketSizeBufferSize];

            Array.Copy(_buffer, _parsingOffset, headerSizeBuffer, 0, Constants.PacketSizeBufferSize);
            var packetSize = (int)MsgPackSerializer.Deserialize <ulong>(headerSizeBuffer, _clientOptions.MsgPackContext);

            return(packetSize);
        }
示例#15
0
        public void TestMixedTypeDictionaryRoundTrip()
        {
            MsgPackSerializer.DefaultContext.SerializationMethod = SerializationMethod.Array;
            var obj = new Dictionary <string, object> {
                { "Boolean", false },
                { "String", "string" },
                { "Float", 1.0f },
                { "Null", null }
            };

            var msg = MsgPackSerializer.SerializeObject(obj);

            var deserializedDictionary = MsgPackSerializer.Deserialize <Dictionary <string, object> > (msg);

            Assert.That(deserializedDictionary.ContainsValue(false));
        }
示例#16
0
        public void RoundTripNestedDictionary()
        {
            var parent = new Dictionary <string, object>();
            var child  = new Dictionary <string, object>();

            child["Foo"]    = "Bar";
            parent["Child"] = child;
            byte[] buffer   = parent.ToMsgPack();
            var    restored = MsgPackSerializer.Deserialize <Dictionary <string, object> >(buffer);

            Assert.IsNotNull(restored);
            Assert.AreEqual(1, restored.Count);
            Assert.IsTrue(restored.ContainsKey("Child"));
            Assert.IsTrue(restored["Child"] is Dictionary <string, object>);
            Assert.AreEqual("Bar", ((Dictionary <string, object>)restored["Child"])["Foo"]);
        }
示例#17
0
        public void InterfaceInheritance(ContextFixtureBase fixture)
        {
            IMegaImageInfo expected = new MegaImageInfo
            {
                Credits  = Guid.NewGuid().ToString("N"),
                Height   = 123,
                Link     = Guid.NewGuid().ToString("N"),
                SomeDate = DateTime.UtcNow,
                Width    = 345
            };

            var actual = MsgPackSerializer.Deserialize <IMegaImageInfo>(MsgPackSerializer.Serialize(expected, fixture.OldContext), fixture.NewContext);

            actual.ShouldBeAssignableTo <IMegaImageInfo>();
            AssertEqual(actual, expected);
        }
示例#18
0
        public void TestNestedObject()
        {
            MsgPackSerializer.DefaultContext.SerializationMethod = SerializationMethod.Array;
            var obj = new SerializationTestObject();

            byte[] msg   = MsgPackSerializer.SerializeObject(obj);
            var    desiz = MsgPackSerializer.Deserialize <SerializationTestObject>(msg);

            Assert.That(desiz != null, "No Nesting: null desiz");
            Assert.That(desiz.Equals(obj), "No Nesting: not equal");

            obj.AddChild();
            msg   = MsgPackSerializer.SerializeObject(obj);
            desiz = MsgPackSerializer.Deserialize <SerializationTestObject>(msg);

            Assert.That(desiz != null, "Nesting: null desiz");
            Assert.That(desiz.Equals(obj), "Nesting: not equal");
        }
示例#19
0
        public void TestRoundTripPrimitives()
        {
            MsgPackSerializer.DefaultContext.SerializationMethod = SerializationMethod.Array;
            TestRoundTrip(0);
            TestRoundTrip(127);

            var stuff = new Dictionary <string, string>();

            stuff["Foo"] = "Value1";
            stuff["Bar"] = "Value2";
            byte[] payload = stuff.ToMsgPack();
            Assert.IsNotNull(payload);
            Assert.AreNotEqual(0, payload.Length);

            var restoredStuff = MsgPackSerializer.Deserialize <Dictionary <string, string> >(payload);

            Assert.AreEqual(stuff.Count, restoredStuff.Count);
        }
示例#20
0
        public void small_test()
        {
            MyClass message = new MyClass()
            {
                Name     = "TestMessage",
                Quantity = 35,
                Anything = new List <object>(new object[] { "First", 2, false, null, 5.5d, "last" })
            };

            // Serialize
            byte[] buffer = MsgPackSerializer.Serialize(message);
            // Deserialize
            MyClass creceiveMessage = MsgPackSerializer.Deserialize <MyClass>(buffer);

            Assert.AreEqual(message.Name, creceiveMessage.Name);
            Assert.AreEqual(message.Quantity, creceiveMessage.Quantity);
            Assert.AreEqual(message.Anything, creceiveMessage.Anything);
        }
示例#21
0
        public void TestCompat()
        {
            MsgPackSerializer.DefaultContext.SerializationMethod = SerializationMethod.Array;
            AnimalMessage msg = AnimalMessage.CreateTestMessage();

            byte[] payload          = msg.ToMsgPack();
            string msgFilename      = Path.Combine(Environment.CurrentDirectory, "animal.msg");
            string verifierFilename = Path.Combine(Environment.CurrentDirectory, "msgpack-sharp-verifier.exe");

            File.WriteAllBytes(msgFilename, payload);
            string args = verifierFilename + " " + msgFilename;

            Process.Start("mono", args);
            Assert.IsTrue(File.Exists(msgFilename + ".out"), "The verifier program that uses other people's msgpack libs failed to successfully handle our message while running [mono " + args + "]");
            payload = File.ReadAllBytes(msgFilename + ".out");
            AnimalMessage restored = MsgPackSerializer.Deserialize <AnimalMessage>(payload);

            VerifyAnimalMessage(msg, restored);
        }
示例#22
0
        public void TestTimeSpan()
        {
            var tests = new List <KeyValuePair <TimeSpan, byte[]> >
            {
                new KeyValuePair <TimeSpan, byte[]>(TimeSpan.MinValue, new byte[] { 211, 128, 0, 0, 0, 0, 0, 0, 0 }),
                new KeyValuePair <TimeSpan, byte[]>(TimeSpan.MaxValue, new byte[] { 207, 127, 255, 255, 255, 255, 255, 255, 255 }),
                new KeyValuePair <TimeSpan, byte[]>(new TimeSpan(1, 2, 3, 4, 5), new byte[] { 207, 0, 0, 0, 218, 91, 159, 127, 80 }),
                new KeyValuePair <TimeSpan, byte[]>(TimeSpan.FromTicks(-100), new byte[] { 208, 156 })
            };

            foreach (var test in tests)
            {
                var value = MsgPackSerializer.Deserialize <TimeSpan>(test.Value);
                value.ShouldBe(test.Key);

                var token = Helpers.CheckTokenDeserialization(test.Value);
                ((TimeSpan)token).ShouldBe(test.Key);
            }
        }
示例#23
0
        public void SmokeSerialization()
        {
            var oldContext = new MsgPackContext();

            oldContext.RegisterConverter(new InnerClassConverter());

            var newContext = new MsgPackContext();

            newContext.DiscoverConverters <InnerClass>();

            var s        = Guid.NewGuid().ToString("B");
            var expected = new InnerClass {
                B = s
            };

            MsgPackSerializer.Serialize(expected, newContext).ShouldBe(MsgPackSerializer.Serialize(expected, oldContext));
            MsgPackSerializer.Deserialize <InnerClass>(MsgPackSerializer.Serialize(expected, oldContext), newContext).B.ShouldBe(s);
            MsgPackSerializer.Deserialize <InnerClass>(MsgPackSerializer.Serialize(expected, newContext), oldContext).B.ShouldBe(s);
        }
示例#24
0
        public void TestDeserializationOfMixedTypeDictionary()
        {
            MsgPackSerializer.DefaultContext.SerializationMethod = SerializationMethod.Array;
            var obj = new Dictionary <string, object> {
                { "Boolean", false },
                { "String", "string" },
                { "Float", 1.0f },
                { "Null", null }
            };

            var msg = MsgPackSerializer.SerializeObject(obj);

            var deserializedDictionary = MsgPackSerializer.Deserialize <Dictionary <string, object> >(msg);

            object value = null;

            deserializedDictionary.TryGetValue("Boolean", out value);
            Assert.That(value.Equals(false));
        }
示例#25
0
        public void TestManualConfig()
        {
            var tank = new Tank()
            {
                Name     = "M1",
                MaxSpeed = 65.0f,
                Cargo    = AnimalMessage.CreateTestMessage()
            };

            MsgPackSerializer.DefaultContext.RegisterSerializer <Tank>("MaxSpeed", "Name", "Cargo");
            byte[] payload = tank.ToMsgPack();
            Assert.IsNotNull(payload);
            Assert.AreNotEqual(0, payload.Length);
            var restoredTank = MsgPackSerializer.Deserialize <Tank>(payload);

            Assert.IsNotNull(restoredTank);
            Assert.AreEqual(tank.Name, restoredTank.Name);
            Assert.AreEqual(tank.MaxSpeed, restoredTank.MaxSpeed);
            VerifyAnimalMessage(tank.Cargo, restoredTank.Cargo);
        }
示例#26
0
        public void TestArrayPack()
        {
            var expected = new object[]
            {
                0,
                50505,
                float.NaN,
                float.MaxValue,
                new[] { true, false, true },
                null,
                new Dictionary <object, object> {
                    { "Ball", "Soccer" }
                }
            };

            var data = new byte[]
            {
                151,
                0,
                205, 197, 73,
                202, 255, 192, 0, 0,
                202, 127, 127, 255, 255,
                147,
                195,
                194,
                195,
                192,
                129,
                164, 66, 97, 108, 108, 166, 83, 111, 99, 99, 101, 114
            };

            var settings = new MsgPackContext();

            settings.RegisterConverter(new TestReflectionConverter());

            MsgPackSerializer.Deserialize <object[]>(data, settings).ShouldBe(expected);

            Helpers.CheckTokenDeserialization(data);
        }
示例#27
0
        public void TestDateTime()
        {
            var tests = new List <KeyValuePair <byte[], System.DateTime> >()
            {
                new KeyValuePair <byte[], DateTime>(new byte[] { 211, 247, 96, 128, 10, 8, 74, 128, 0, }, new DateTime(1, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc)),
                new KeyValuePair <byte[], DateTime>(new byte[] { 211, 35, 42, 168, 127, 252, 129, 152, 240, }, new DateTime(9999, 12, 31, 23, 59, 59, 999, DateTimeKind.Utc)),
                new KeyValuePair <byte[], DateTime>(new byte[] { 211, 0, 51, 110, 236, 17, 171, 0, 0, }, new DateTime(2015, 11, 17, 0, 0, 0, 0, DateTimeKind.Utc)),
                new KeyValuePair <byte[], DateTime>(new byte[] { 211, 247, 96, 154, 26, 189, 97, 197, 0, }, new DateTime(1, 2, 3, 4, 5, 6, DateTimeKind.Utc)),
                new KeyValuePair <byte[], DateTime>(new byte[] { 207, 247, 96, 128, 10, 8, 74, 128, 0, }, new DateTime(1, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc)),
                new KeyValuePair <byte[], DateTime>(new byte[] { 207, 35, 42, 168, 127, 252, 129, 152, 240, }, new DateTime(9999, 12, 31, 23, 59, 59, 999, DateTimeKind.Utc)),
                new KeyValuePair <byte[], DateTime>(new byte[] { 207, 0, 51, 110, 236, 17, 171, 0, 0, }, new DateTime(2015, 11, 17, 0, 0, 0, 0, DateTimeKind.Utc)),
                new KeyValuePair <byte[], DateTime>(new byte[] { 207, 247, 96, 154, 26, 189, 97, 197, 0, }, new DateTime(1, 2, 3, 4, 5, 6, DateTimeKind.Utc)),
            };

            foreach (var test in tests)
            {
                MsgPackSerializer.Deserialize <System.DateTime>(test.Key).ShouldBe(test.Value);

                var token = Helpers.CheckTokenDeserialization(test.Key);
                ((DateTime)token).ShouldBe(test.Value);
            }
        }
示例#28
0
        private void TestGenericDictionary <Key, Value> (Key testKey, Value testValue)
        {
            Dictionary <Key, Value> intDict = new Dictionary <Key, Value>();

            intDict.Add(testKey, testValue);

            var msg       = MsgPackSerializer.SerializeObject(intDict);
            var desizDict = MsgPackSerializer.Deserialize <Dictionary <Key, Value> >(msg);

            string logHeader = string.Format("Dictionary<{0}, {1}>: ", typeof(Key).ToString(), typeof(Value).ToString());

            Assert.That(desizDict != null, logHeader + "null desiz");
            Assert.That(typeof(Dictionary <Key, Value>) == desizDict.GetType(), logHeader + "different types");

            if (testValue == null)
            {
                Assert.That(desizDict[testKey] == null);
            }
            else
            {
                Assert.That(desizDict.ContainsKey(testKey) && desizDict[testKey].Equals(testValue), logHeader + "key value lost");
            }
        }
示例#29
0
        private void TestLimit(int count)
        {
            var msg = new AnimalMessage();

            msg.SpotColors = new List <AnimalColor>();
            for (int i = 0; i < count; i++)
            {
                msg.SpotColors.Add(new AnimalColor()
                {
                    Red = 1.0f
                });
            }
            byte[] payload = msg.ToMsgPack();

            var restored = MsgPackSerializer.Deserialize <AnimalMessage>(payload);

            Assert.IsNotNull(restored.SpotColors);
            Assert.AreEqual(msg.SpotColors.Count, restored.SpotColors.Count);

            for (int i = 0; i < count; i++)
            {
                Assert.AreEqual(msg.SpotColors[i], restored.SpotColors[i]);
            }
        }
示例#30
0
 public void MPLight_Stream()
 {
     _stream.Seek(0, SeekOrigin.Begin);
     var data = MsgPackSerializer.Deserialize <T[]>(_stream, _mplightContext);
 }