/// <summary> /// Converts parameter object from Amqp request to a particular type to be used as input parameter to RPC method being invoked /// </summary> /// <param name="deserializationType">Type into which object parameters must be converted</param> /// <param name="parameters">Input received at AmqpRequest.body.params</param> /// <returns>Deserialized object which will be used as an input to Rpc Method</returns> public dynamic PeeloutAmqpWrapper(Type deserializationType, object parameters) { try { if (deserializationType == null) { return(null); } //Create Amqp serializer instance AmqpSerializer _serializer = new AmqpSerializer(); //Create dynamic buffer ByteBuffer _paramsBuffer = new ByteBuffer(1024, true); //Write object to buffer _serializer.WriteObject(_paramsBuffer, parameters); //Get ReadObject methodinfo using reflection var _readObjectMethodInfo = typeof(AmqpSerializer).GetMethods(BindingFlags.Public | BindingFlags.Instance) .Where(x => x.Name.Equals("ReadObject") && x.IsGenericMethod && x.GetGenericArguments().Length.Equals(1)) .FirstOrDefault(); if (_readObjectMethodInfo == null) { throw new MissingMethodException("ReadObject from AmqpSerializer"); } //Mark methodinfo as generic var _readObjectGenericMethodInfo = _readObjectMethodInfo.MakeGenericMethod(deserializationType); //Invoke ReadObject to deserialize object return(_readObjectGenericMethodInfo.Invoke(_serializer, new[] { _paramsBuffer })); } catch (Exception ex) { Log.Error(ex, "Exception while deserializing request parameters", parameters); } return(null); }
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); }
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]); }