public void SerializeDeserializeType() { var serializer = new TypeSerializer(typeof(Person)); MemoryStream stream = new MemoryStream(); OutputChannel output = new OutputChannel(new BinaryWriter(stream)); output.Write(serializer); stream.Seek(0, SeekOrigin.Begin); InputChannel channel = new InputChannel(new BinaryReader(stream)); var result = channel.Read(true); Assert.IsNotNull(result); Assert.IsInstanceOfType(result, typeof(TypeSerializer)); var newserializer = (TypeSerializer)result; var props = newserializer.Properties; Assert.IsNotNull(props); Assert.AreEqual(3, props.Count); Assert.AreEqual("Id", props[0].Name); Assert.AreEqual(Types.Integer, props[0].Type); Assert.IsNull(props[0].TypeName); Assert.AreEqual("FirstName", props[1].Name); Assert.AreEqual(Types.String, props[1].Type); Assert.IsNull(props[1].TypeName); Assert.AreEqual("LastName", props[2].Name); Assert.AreEqual(Types.String, props[2].Type); Assert.IsNull(props[2].TypeName); }
public void CreateSerializerByFullNameAndProperties() { IList<PropertyType> properties = new List<PropertyType>(); properties.Add(new PropertyType() { Name = "Id", Type = Types.Integer }); properties.Add(new PropertyType() { Name = "FirstName", Type = Types.String }); var serializer = new TypeSerializer("ErlSharp.Tests.Person", properties); Assert.AreEqual("ErlSharp.Tests.Person", serializer.TypeName); var props = serializer.Properties; Assert.IsNotNull(props); Assert.AreEqual(2, props.Count); Assert.AreEqual("Id", props[0].Name); Assert.AreEqual(Types.Integer, props[0].Type); Assert.IsNull(props[0].TypeName); Assert.AreEqual("FirstName", props[1].Name); Assert.AreEqual(Types.String, props[1].Type); Assert.IsNull(props[1].TypeName); }
public object Read(bool retserializer = false) { byte type = this.reader.ReadByte(); switch (type) { case (byte)Types.Null: return null; case (byte)Types.Integer: return this.reader.ReadInt32(); case (byte)Types.Double: return this.reader.ReadDouble(); case (byte)Types.String: return this.reader.ReadString(); case (byte)Types.Byte: return this.reader.ReadByte(); case (byte)Types.Character: return this.reader.ReadChar(); case (byte)Types.Single: return this.reader.ReadSingle(); case (byte)Types.Short: return this.reader.ReadInt16(); case (byte)Types.Long: return this.reader.ReadInt64(); case (byte)Types.Decimal: return this.reader.ReadDecimal(); case (byte)Types.Type: var name = this.reader.ReadString(); var nprops = this.reader.ReadInt16(); IList<PropertyType> properties = new List<PropertyType>(); for (short k = 0; k < nprops; k++) { PropertyType property = new PropertyType(); property.Name = this.reader.ReadString(); property.Type = (Types)this.reader.ReadByte(); if (property.Type == Types.Object) property.TypeName = this.reader.ReadString(); properties.Add(property); } var serializer = new TypeSerializer(name, properties); this.serializers.Add(serializer); if (retserializer) return serializer; else return this.Read(); case (byte)Types.Object: var nserializer = this.reader.ReadInt16(); return this.serializers[nserializer].DeserializerObject(this); } throw new InvalidDataException(); }
public void GetPersonProperties() { var serializer = new TypeSerializer(typeof(Person)); var props = serializer.Properties; Assert.IsNotNull(props); Assert.AreEqual(3, props.Count); Assert.AreEqual("Id", props[0].Name); Assert.AreEqual(Types.Integer, props[0].Type); Assert.IsNull(props[0].TypeName); Assert.AreEqual("FirstName", props[1].Name); Assert.AreEqual(Types.String, props[1].Type); Assert.IsNull(props[1].TypeName); Assert.AreEqual("LastName", props[2].Name); Assert.AreEqual(Types.String, props[2].Type); Assert.IsNull(props[2].TypeName); }
public void Write(object obj) { if (obj == null) { this.writer.Write((byte)Types.Null); return; } if (obj is int) { this.writer.Write((byte)Types.Integer); this.writer.Write((int)obj); return; } if (obj is short) { this.writer.Write((byte)Types.Short); this.writer.Write((short)obj); return; } if (obj is long) { this.writer.Write((byte)Types.Long); this.writer.Write((long)obj); return; } if (obj is char) { this.writer.Write((byte)Types.Character); this.writer.Write((char)obj); return; } if (obj is byte) { this.writer.Write((byte)Types.Byte); this.writer.Write((byte)obj); return; } if (obj is double) { this.writer.Write((byte)Types.Double); this.writer.Write((double)obj); return; } if (obj is float) { this.writer.Write((byte)Types.Single); this.writer.Write((float)obj); return; } if (obj is string) { this.writer.Write((byte)Types.String); this.writer.Write((string)obj); return; } if (obj is decimal) { this.writer.Write((byte)Types.Decimal); this.writer.Write((decimal)obj); return; } if (obj is TypeSerializer) { var ts = (TypeSerializer)obj; var props = ts.Properties; this.writer.Write((byte)Types.Type); this.writer.Write(ts.TypeName); this.writer.Write((short)props.Count); foreach (var prop in props) { this.writer.Write(prop.Name); this.writer.Write((byte)prop.Type); if (prop.Type == Types.Object) this.writer.Write(prop.TypeName); } return; } var type = obj.GetType(); string typename = type.FullName; TypeSerializer serializer; if (!this.typepositions.ContainsKey(typename)) { serializer = new TypeSerializer(type); this.typepositions[typename] = this.serializers.Count; this.serializers.Add(serializer); this.Write(serializer); } else serializer = this.serializers[this.typepositions[typename]]; this.writer.Write((byte)Types.Object); this.writer.Write((short)this.typepositions[typename]); serializer.SerializeObject(obj, this); }
public void GetPersonTypeFullName() { var serializer = new TypeSerializer(typeof(Person)); Assert.AreEqual("ErlSharp.Tests.Person", serializer.TypeName); }
public void SerializePersonObject() { var serializer = new TypeSerializer(typeof(Person)); var person = new Person() { Id = 1, FirstName = "John", LastName = "Smith" }; MemoryStream stream = new MemoryStream(); OutputChannel output = new OutputChannel(new BinaryWriter(stream)); serializer.SerializeObject(person, output); stream.Seek(0, SeekOrigin.Begin); InputChannel channel = new InputChannel(new BinaryReader(stream)); Assert.AreEqual(1, channel.Read()); Assert.AreEqual("John", channel.Read()); Assert.AreEqual("Smith", channel.Read()); }