Example #1
0
        public TypeSerializer(Type type)
        {
            this.type = type;
            this.properties = new List<PropertyType>();

            foreach (var pi in type.GetProperties())
            {
                var prop = new PropertyType();
                prop.Name = pi.Name;
                var tp = pi.PropertyType;

                if (tp == typeof(string))
                    prop.Type = Types.String;
                else if (tp == typeof(int))
                    prop.Type = Types.Integer;
                else if (tp == typeof(double))
                    prop.Type = Types.Double;
                else
                {
                    prop.Type = Types.Object;
                    prop.TypeName = tp.FullName;
                }

                this.properties.Add(prop);
            }
        }
Example #2
0
        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();
        }