示例#1
0
 public override object DecodeObject(ByteBuffer buffer, FormatCode formatCode)
 {
     if (formatCode == FormatCode.Described)
     {
         return(DescribedEncoding.Decode(buffer, formatCode));
     }
     else
     {
         return(AmqpEncoding.DecodeObject(buffer, formatCode));
     }
 }
示例#2
0
        public static DescribedType Decode(ByteBuffer buffer)
        {
            FormatCode formatCode;

            if ((formatCode = AmqpEncoding.ReadFormatCode(buffer)) == FormatCode.Null)
            {
                return(null);
            }

            return(DescribedEncoding.Decode(buffer, formatCode));
        }
示例#3
0
            public override object ReadObject(ByteBuffer buffer)
            {
                AmqpDescribedType describedType = DescribedEncoding.Decode(buffer);

                if (describedType == null)
                {
                    return(null);
                }
                else
                {
                    if (!this.symbol.Equals(describedType.Descriptor))
                    {
                        throw new SerializationException(describedType.Descriptor.ToString());
                    }

                    return(this.setter((TAs)describedType.Value));
                }
            }
示例#4
0
        public void AmqpSerializerListEncodingTest()
        {
            Action <Person, Person> personValidator = (p1, p2) =>
            {
                Assert.IsNotNull(p2);
                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 Address()
                {
                    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");

            var stream = new MemoryStream(new byte[4096], 0, 4096, true, true);

            AmqpContractSerializer.WriteObject(stream, p);
            stream.Flush();

            // Deserialize and verify
            stream.Seek(0, SeekOrigin.Begin);
            Person p3 = AmqpContractSerializer.ReadObject <Person>(stream);

            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
            stream.Seek(0, SeekOrigin.Begin);
            DescribedType dl1 = DescribedEncoding.Decode(new ByteBuffer(stream.GetBuffer(), 0, (int)stream.Length));

            Assert.AreEqual(dl1.Descriptor, 1ul);
            List <object> lv = dl1.Value as List <object>;

            Assert.IsNotNull(lv);
            Assert.AreEqual(p.Name, lv[0]);
            Assert.AreEqual(p.Age, lv[1]);
            Assert.AreEqual(p.DateOfBirth.Value, lv[2]);
            Assert.IsTrue(lv[3] is DescribedType, "Address is decribed type");
            Assert.AreEqual(((DescribedType)lv[3]).Descriptor, 3ul);
            Assert.AreEqual(((List <object>)((DescribedType)lv[3]).Value)[0], ((Student)p).Address.FullAddress);
            Assert.IsTrue(lv[4] is AmqpMap, "Properties should be map");
            Assert.AreEqual(((AmqpMap)lv[4])[new MapKey("height")], p.Properties["height"]);
            Assert.AreEqual(((AmqpMap)lv[4])[new MapKey("male")], p.Properties["male"]);
            Assert.AreEqual(((AmqpMap)lv[4])[new MapKey("nick-name")], p.Properties["nick-name"]);
            Assert.IsTrue(lv[5] is List <object>);

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

            serializer.WriteObjectInternal(bf1, p);

            Person p4 = serializer.ReadObjectInternal <Person, Person>(bf1);

            personValidator(p, p4);

            // Extensible: more items in the payload should not break
            DescribedType dl2 = new DescribedType(
                new AmqpSymbol("teacher"),
                new List <object>()
            {
                "Jerry", 40, null, 50000, lv[4], null, null, "unknown-string", true, new AmqpSymbol("unknown-symbol")
            });
            ByteBuffer bf2 = new ByteBuffer(1024, true);

            DescribedEncoding.Encode(dl2, bf2);
            ULongEncoding.Encode(100, bf2);

            Person p5 = serializer.ReadObjectInternal <Person, Person>(bf2);

            Assert.IsTrue(p5 is Teacher);
            Assert.AreEqual(100ul, AmqpCodec.DecodeULong(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.WriteObjectInternal(bf3, teacher);

            Person p6 = serializer.ReadObjectInternal <Person, 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]);
        }