Beispiel #1
0
 public override void EncodeObject(object value, bool arrayEncoding, ByteBuffer buffer)
 {
     if (arrayEncoding)
     {
         AmqpBitConverter.WriteULong(buffer, (ulong)value);
     }
     else
     {
         ULongEncoding.Encode((ulong)value, buffer);
     }
 }
Beispiel #2
0
 protected override bool WriteFormatCode(ByteBuffer buffer)
 {
     AmqpBitConverter.WriteUByte(buffer, 0);
     if (!this.descriptorCode.HasValue)
     {
         SymbolEncoding.Encode(this.descriptorName, buffer);
     }
     else
     {
         ULongEncoding.Encode(this.descriptorCode, buffer);
     }
     AmqpBitConverter.WriteUByte(buffer, this.Code);
     return(true);
 }
Beispiel #3
0
            protected override bool WriteFormatCode(ByteBuffer buffer)
            {
                AmqpBitConverter.WriteUByte(buffer, (byte)FormatCode.Described);
                if (this.descriptorCode != null)
                {
                    ULongEncoding.Encode(this.descriptorCode, buffer);
                }
                else
                {
                    SymbolEncoding.Encode(this.descriptorName, buffer);
                }

                AmqpBitConverter.WriteUByte(buffer, this.Code);
                return(true);
            }
Beispiel #4
0
            protected override void OnWriteObject(ByteBuffer buffer, object graph)
            {
                if (graph == null)
                {
                    AmqpEncoding.EncodeNull(buffer);
                    return;
                }

                AmqpBitConverter.WriteUByte(buffer, (byte)FormatCode.Described);
                if (this.descriptorName.Value != null)
                {
                    SymbolEncoding.Encode(this.descriptorName, buffer);
                }
                else
                {
                    ULongEncoding.Encode(this.descriptorCode, buffer);
                }

                if (this.members.Length == 0)
                {
                    AmqpBitConverter.WriteUByte(buffer, (byte)FormatCode.List0);
                    return;
                }

                AmqpBitConverter.WriteUByte(buffer, (byte)FormatCode.List32);
                int endCopy = buffer.End;               // remember the current position

                AmqpBitConverter.WriteUInt(buffer, 0);  // reserve space for list size
                AmqpBitConverter.WriteUInt(buffer, (uint)this.members.Length);

                for (int i = 0; i < this.members.Length; ++i)
                {
                    object memberValue = this.members[i].Accessor.ReadObject(graph);
                    this.members[i].Type.OnWriteObject(buffer, memberValue);
                }

                // write the correct size
                AmqpBitConverter.WriteUInt(buffer.Buffer, endCopy, (uint)(buffer.End - endCopy - FixedWidth.UInt));
            }
Beispiel #5
0
 /// <summary>
 /// Encodes a 64-bit unsigned integer and appends the bytes to the buffer.
 /// </summary>
 /// <param name="data">The 64-bit unsigned integer.</param>
 /// <param name="buffer">The destination buffer.</param>
 public static void EncodeULong(ulong?data, ByteBuffer buffer)
 {
     ULongEncoding.Encode(data, buffer);
 }
Beispiel #6
0
 public void Encode(ByteBuffer buffer)
 {
     AmqpBitConverter.WriteUByte(buffer, FormatCode.Described);
     ULongEncoding.Encode(this.DescriptorCode, buffer);
     this.EncodeValue(buffer);
 }
Beispiel #7
0
 public void Encode(ByteBuffer buffer)
 {
     AmqpBitConverter.WriteUByte(buffer, 0);
     ULongEncoding.Encode(new ulong?(this.DescriptorCode), buffer);
     this.EncodeValue(buffer);
 }
Beispiel #8
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]);
        }