Beispiel #1
0
        static T GetValue <T>(this AmqpValue value, AmqpSerializer serializer)
        {
            ByteBuffer buffer = value.ValueBuffer;

            if (buffer == null)
            {
                return((T)value.Value);
            }

            buffer.Seek(0);
            if (typeof(T) == typeof(ByteBuffer))
            {
                int offset  = GetBinaryOffset(buffer);
                int len     = buffer.Length - offset;
                var payload = new ByteBuffer(buffer.Buffer, buffer.Offset + offset, len, len);
                return((T)(object)payload);
            }
            else if (typeof(T) == typeof(byte[]))
            {
                int    offset  = GetBinaryOffset(buffer);
                int    len     = buffer.Length - offset;
                byte[] payload = new byte[len];
                Buffer.BlockCopy(buffer.Buffer, buffer.Offset + offset, payload, 0, payload.Length);
                return((T)(object)payload);
            }
            else
            {
                return(serializer.ReadObject <T>(buffer));
            }
        }
Beispiel #2
0
        public void AmqpSerializerCustomResolverTest()
        {
            var serializer = new AmqpSerializer(new PocoContractResolver()
            {
                PrefixList = new[] { "Serialization.Poco" }
            });

            Circle circle = new Circle()
            {
                Id = Guid.NewGuid(), Radius = 3.5
            };
            ByteBuffer buffer = new ByteBuffer(1024, true);

            serializer.WriteObject(buffer, circle);

            Shape shape = serializer.ReadObject <Shape>(buffer);

            Assert.AreEqual(typeof(Circle), shape.GetType());
            Assert.AreEqual(circle.Id, ((Circle)shape).Id);
            Assert.AreEqual(circle.Radius, ((Circle)shape).Radius);
        }
Beispiel #3
0
        public void AmqpSerializerCircularProvidesTest()
        {
            Provides1 p1 = new Provides1()
            {
                Value = 2, Id = Guid.NewGuid()
            };
            Provides2 p2 = p1.Next = new Provides2()
            {
                Value = 8, Name = "p2"
            };

            ByteBuffer b = new ByteBuffer(512, true);

            AmqpSerializer.Serialize(b, p1);

            var serializer = new AmqpSerializer();
            var t          = serializer.ReadObject <ProvidesBase>(b);
            var p          = t as Provides1;

            Assert.IsTrue(p != null);
            Assert.AreEqual(7, p2.Value);
            Assert.AreEqual(p2.Value, p.Next.Value);
            Assert.AreEqual(p1.Value + 1, p.Value);
        }
        public void AmqpSerializerListEncodingTest()
        {
            Action <Person, Person> personValidator = (p1, p2) =>
            {
                Assert.IsTrue(p2 != null);
                Assert.AreEqual(21, p2.Age, "Age should be increased by OnDeserialized");
                Assert.AreEqual(p1.GetType().Name, p2.GetType().Name);
                Assert.AreEqual(p1.DateOfBirth.Value, p2.DateOfBirth.Value);
                Assert.AreEqual(p1.Properties.Count, p2.Properties.Count);
                foreach (var k in p1.Properties.Keys)
                {
                    Assert.AreEqual(p1.Properties[k], p2.Properties[k]);
                }
            };

            Action <List <int>, List <int> > gradesValidator = (l1, l2) =>
            {
                if (l1 == null || l2 == null)
                {
                    Assert.IsTrue(l1 == null && l2 == null);
                    return;
                }

                Assert.AreEqual(l1.Count, l2.Count);
                for (int i = 0; i < l1.Count; ++i)
                {
                    Assert.AreEqual(l1[i], l2[i]);
                }
            };

            // Create an object to be serialized
            Person p = new Student("Tom")
            {
                Address = new StreetAddress()
                {
                    FullAddress = new string('B', 1024)
                },
                Grades = new List <int>()
                {
                    1, 2, 3, 4, 5
                }
            };

            p.Age         = 20;
            p.DateOfBirth = new DateTime(1980, 5, 12, 10, 2, 45, DateTimeKind.Utc);
            p.Properties.Add("height", 6.1);
            p.Properties.Add("male", true);
            p.Properties.Add("nick-name", "big foot");

            byte[]     workBuffer = new byte[4096];
            ByteBuffer buffer     = new ByteBuffer(workBuffer, 0, 0, workBuffer.Length);

            AmqpSerializer.Serialize(buffer, p);
            Assert.AreEqual(2, p.Version);

            // Deserialize and verify
            Person p3 = AmqpSerializer.Deserialize <Person>(buffer);

            Assert.AreEqual(2, p.Version);
            personValidator(p, p3);
            Assert.AreEqual(((Student)p).Address.FullAddress, ((Student)p3).Address.FullAddress);
            gradesValidator(((Student)p).Grades, ((Student)p3).Grades);

            // Inter-op: it should be an AMQP described list as other clients see it
            buffer.Seek(0);
            DescribedValue dl1 = AmqpSerializer.Deserialize <DescribedValue>(buffer);

            Assert.AreEqual(dl1.Descriptor, 0x0000123400000001UL);
            List lv = dl1.Value as List;

            Assert.IsTrue(lv != null);
            Assert.AreEqual(p.Name, lv[0]);
            Assert.AreEqual(p.Age, lv[1]);
            Assert.AreEqual(p.DateOfBirth.Value, lv[2]);
            Assert.IsTrue(lv[3] is DescribedValue, "Address is decribed type");
            Assert.AreEqual(((DescribedValue)lv[3]).Descriptor, 0x0000123400000003UL);
            Assert.AreEqual(((List)((DescribedValue)lv[3]).Value)[0], ((Student)p).Address.FullAddress);
            Assert.IsTrue(lv[4] is Map, "Properties should be map");
            Assert.AreEqual(((Map)lv[4])["height"], p.Properties["height"]);
            Assert.AreEqual(((Map)lv[4])["male"], p.Properties["male"]);
            Assert.AreEqual(((Map)lv[4])["nick-name"], p.Properties["nick-name"]);
            Assert.IsTrue(lv[5] is List);

            // Non-default serializer
            AmqpSerializer serializer = new AmqpSerializer();
            ByteBuffer     bf1        = new ByteBuffer(1024, true);

            serializer.WriteObject(bf1, p);

            Person p4 = serializer.ReadObject <Person>(bf1);

            personValidator(p, p4);

            // Extensible: more items in the payload should not break
            DescribedValue dl2 = new DescribedValue(
                new Symbol("test.amqp:teacher"),
                new List()
            {
                "Jerry", 40, null, 50000, lv[4], null, null, "unknown-string", true, new Symbol("unknown-symbol")
            });
            ByteBuffer bf2 = new ByteBuffer(1024, true);

            serializer.WriteObject(bf2, dl2);
            serializer.WriteObject(bf2, 100ul);

            Person p5 = serializer.ReadObject <Person>(bf2);

            Assert.IsTrue(p5 is Teacher);
            Assert.IsTrue(p5.DateOfBirth == null);                       // nullable should work
            Assert.AreEqual(100ul, serializer.ReadObject <object>(bf2)); // unknowns should be skipped
            Assert.AreEqual(0, bf2.Length);

            // teacher
            Teacher teacher = new Teacher("Han");

            teacher.Age     = 30;
            teacher.Sallary = 60000;
            teacher.Classes = new Dictionary <int, string>()
            {
                { 101, "CS" }, { 102, "Math" }, { 205, "Project" }
            };

            ByteBuffer bf3 = new ByteBuffer(1024, true);

            serializer.WriteObject(bf3, teacher);

            Person p6 = serializer.ReadObject <Person>(bf3);

            Assert.IsTrue(p6 is Teacher);
            Assert.AreEqual(teacher.Age + 1, p6.Age);
            Assert.AreEqual(teacher.Sallary * 2, ((Teacher)p6).Sallary);
            Assert.AreEqual(teacher.Id, ((Teacher)p6).Id);
            Assert.AreEqual(teacher.Classes[101], ((Teacher)p6).Classes[101]);
            Assert.AreEqual(teacher.Classes[102], ((Teacher)p6).Classes[102]);
            Assert.AreEqual(teacher.Classes[205], ((Teacher)p6).Classes[205]);
        }