public void AmqpSerializerMapEncodingTest() { NamedList <string> list = new NamedList <string>() { Name = "test-list", List = new string[] { "v1", "v2" } }; AmqpContractSerializer serializer = new AmqpContractSerializer(); ByteBuffer b = new ByteBuffer(1024, true); serializer.WriteObjectInternal(b, list); var result = serializer.ReadObjectInternal <NamedList <string>, NamedList <string> >(b); Assert.Equal(list.Name, result.Name); EnsureEqual((IList)list.List, (IList)result.List); }
public void AmqpSerializerListEncodingTest() { Action <Person, Person> personValidator = (p1, p2) => { Assert.NotNull(p2); Assert.True(21 == p2.Age, "Age should be increased by OnDeserialized"); Assert.Equal(p1.GetType().Name, p2.GetType().Name); Assert.Equal(p1.DateOfBirth.Value, p2.DateOfBirth.Value); Assert.Equal(p1.Properties.Count, p2.Properties.Count); foreach (var k in p1.Properties.Keys) { Assert.Equal(p1.Properties[k], p2.Properties[k]); } }; Action <List <int>, List <int> > gradesValidator = (l1, l2) => { if (l1 == null || l2 == null) { Assert.True(l1 == null && l2 == null); return; } Assert.Equal(l1.Count, l2.Count); for (int i = 0; i < l1.Count; ++i) { Assert.Equal(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.Equal(((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 = (DescribedType)AmqpEncoding.DecodeObject(new ByteBuffer(stream.ToArray(), 0, (int)stream.Length)); Assert.Equal(1ul, dl1.Descriptor); List <object> lv = dl1.Value as List <object>; Assert.NotNull(lv); Assert.Equal(p.Name, lv[0]); Assert.Equal(p.Age, lv[1]); Assert.Equal(p.DateOfBirth.Value, lv[2]); Assert.True(lv[3] is DescribedType, "Address is decribed type"); Assert.Equal(3ul, ((DescribedType)lv[3]).Descriptor); Assert.Equal(((List <object>)((DescribedType)lv[3]).Value)[0], ((Student)p).Address.FullAddress); Assert.True(lv[4] is AmqpMap, "Properties should be map"); Assert.Equal(((AmqpMap)lv[4])[new MapKey("height")], p.Properties["height"]); Assert.Equal(((AmqpMap)lv[4])[new MapKey("male")], p.Properties["male"]); Assert.Equal(((AmqpMap)lv[4])[new MapKey("nick-name")], p.Properties["nick-name"]); Assert.True(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); AmqpEncoding.EncodeObject(dl2, bf2); AmqpCodec.EncodeULong(100ul, bf2); Person p5 = serializer.ReadObjectInternal <Person, Person>(bf2); Assert.True(p5 is Teacher); Assert.Equal(100ul, AmqpCodec.DecodeULong(bf2)); // unknowns should be skipped Assert.Equal(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.True(p6 is Teacher); Assert.Equal(teacher.Age + 1, p6.Age); Assert.Equal(teacher.Sallary * 2, ((Teacher)p6).Sallary); Assert.Equal(teacher.Id, ((Teacher)p6).Id); Assert.Equal(teacher.Classes[101], ((Teacher)p6).Classes[101]); Assert.Equal(teacher.Classes[102], ((Teacher)p6).Classes[102]); Assert.Equal(teacher.Classes[205], ((Teacher)p6).Classes[205]); }
public CompositeMap( AmqpContractSerializer serializer, Type type, SerializableType baseType, string descriptorName, ulong? descriptorCode, SerialiableMember[] members, Dictionary<Type, SerializableType> knownTypes, MethodAccessor onDesrialized) : base(serializer, type, baseType, descriptorName, descriptorCode, members, knownTypes, onDesrialized) { this.EncodingType = EncodingType.Map; }
public Serializable(AmqpContractSerializer serializer, Type type) : base(serializer, type) { this.AmqpType = AmqpType.Serializable; }
protected Composite( AmqpContractSerializer serializer, Type type, SerializableType baseType, string descriptorName, ulong? descriptorCode, SerialiableMember[] members, Dictionary<Type, SerializableType> knownTypes, MethodAccessor onDesrialized) : base(serializer, type) { this.AmqpType = AmqpType.Composite; this.baseType = (Composite)baseType; this.descriptorName = descriptorName; this.descriptorCode = descriptorCode; this.members = members; this.onDeserialized = onDesrialized; this.knownTypes = GetKnownTypes(knownTypes); }
public Map(AmqpContractSerializer serializer, Type type, MemberAccessor keyAccessor, MemberAccessor valueAccessor, MethodAccessor addAccessor) : base(serializer, type) { this.AmqpType = AmqpType.Primitive; this.keyType = this.serializer.GetType(keyAccessor.Type); this.valueType = this.serializer.GetType(valueAccessor.Type); this.keyAccessor = keyAccessor; this.valueAccessor = valueAccessor; this.addMethodAccessor = addAccessor; }
public List(AmqpContractSerializer serializer, Type type, Type itemType, MethodAccessor addAccessor) : base(serializer, type) { this.AmqpType = AmqpType.Primitive; this.itemType = serializer.GetType(itemType); this.addMethodAccessor = addAccessor; }
protected SerializableType(AmqpContractSerializer serializer, Type type) { this.serializer = serializer; this.type = type; this.hasDefaultCtor = type.GetConstructor(Type.EmptyTypes) != null; }
protected Collection(AmqpContractSerializer serializer, Type type) : base(serializer, type) { }