Exemplo n.º 1
0
        /// <summary>
        /// Deserializes an object from a binary source.
        /// </summary>
        /// <param name="Reader">Binary deserializer.</param>
        /// <param name="DataType">Optional datatype. If not provided, will be read from the binary source.</param>
        /// <param name="Embedded">If the object is embedded into another.</param>
        /// <returns>Deserialized object.</returns>
        public object Deserialize(BinaryDeserializer Reader, uint?DataType, bool Embedded)
        {
            if (!DataType.HasValue)
            {
                DataType = Reader.ReadBits(6);
            }

            switch (DataType.Value)
            {
            case ObjectSerializer.TYPE_ENUM:
            case ObjectSerializer.TYPE_STRING:
            case ObjectSerializer.TYPE_CI_STRING: return(Reader.ReadString());

            case ObjectSerializer.TYPE_CHAR: return(new string(Reader.ReadChar(), 1));

            case ObjectSerializer.TYPE_BOOLEAN: return(Reader.ReadBoolean().ToString());

            case ObjectSerializer.TYPE_BYTE: return(Reader.ReadByte().ToString());

            case ObjectSerializer.TYPE_INT16: return(Reader.ReadInt16().ToString());

            case ObjectSerializer.TYPE_INT32: return(Reader.ReadInt32().ToString());

            case ObjectSerializer.TYPE_INT64: return(Reader.ReadInt64().ToString());

            case ObjectSerializer.TYPE_SBYTE: return(Reader.ReadSByte().ToString());

            case ObjectSerializer.TYPE_UINT16: return(Reader.ReadUInt16().ToString());

            case ObjectSerializer.TYPE_UINT32: return(Reader.ReadUInt32().ToString());

            case ObjectSerializer.TYPE_UINT64: return(Reader.ReadUInt64().ToString());

            case ObjectSerializer.TYPE_DECIMAL: return(Reader.ReadDecimal().ToString());

            case ObjectSerializer.TYPE_DOUBLE: return(Reader.ReadDouble().ToString());

            case ObjectSerializer.TYPE_SINGLE: return(Reader.ReadSingle().ToString());

            case ObjectSerializer.TYPE_GUID: return(Reader.ReadGuid().ToString());

            case ObjectSerializer.TYPE_DATETIME: return(Reader.ReadDateTime().ToString());

            case ObjectSerializer.TYPE_DATETIMEOFFSET: return(Reader.ReadDateTimeOffset().ToString());

            case ObjectSerializer.TYPE_TIMESPAN: return(Reader.ReadTimeSpan().ToString());

            case ObjectSerializer.TYPE_NULL: return(null);

            default: throw new Exception("Expected a string value.");
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// Deserializes an object from a binary source.
        /// </summary>
        /// <param name="Reader">Binary deserializer.</param>
        /// <param name="DataType">Optional datatype. If not provided, will be read from the binary source.</param>
        /// <param name="Embedded">If the object is embedded into another.</param>
        /// <returns>Deserialized object.</returns>
        public override object Deserialize(BinaryDeserializer Reader, uint?DataType, bool Embedded)
        {
            if (!DataType.HasValue)
            {
                DataType = Reader.ReadBits(6);
            }

            switch (DataType.Value)
            {
            case ObjectSerializer.TYPE_CHAR: return((char?)Reader.ReadChar());

            case ObjectSerializer.TYPE_BYTE: return((char?)Reader.ReadByte());

            case ObjectSerializer.TYPE_INT16: return((char?)Reader.ReadInt16());

            case ObjectSerializer.TYPE_INT32: return((char?)Reader.ReadInt32());

            case ObjectSerializer.TYPE_INT64: return((char?)Reader.ReadInt64());

            case ObjectSerializer.TYPE_SBYTE: return((char?)Reader.ReadSByte());

            case ObjectSerializer.TYPE_UINT16: return((char?)Reader.ReadUInt16());

            case ObjectSerializer.TYPE_UINT32: return((char?)Reader.ReadUInt32());

            case ObjectSerializer.TYPE_UINT64: return((char?)Reader.ReadUInt64());

            case ObjectSerializer.TYPE_DECIMAL: return((char?)Reader.ReadDecimal());

            case ObjectSerializer.TYPE_DOUBLE: return((char?)Reader.ReadDouble());

            case ObjectSerializer.TYPE_SINGLE: return((char?)Reader.ReadSingle());

            case ObjectSerializer.TYPE_MIN: return(char.MinValue);

            case ObjectSerializer.TYPE_MAX: return(char.MaxValue);

            case ObjectSerializer.TYPE_NULL: return(null);

            case ObjectSerializer.TYPE_STRING:
            case ObjectSerializer.TYPE_CI_STRING:
                string s = Reader.ReadString();
                return(string.IsNullOrEmpty(s) ? (char?)0 : s[0]);

            default: throw new Exception("Expected a nullable char value.");
            }
        }
Exemplo n.º 3
0
        /// <summary>
        /// Deserializes an object from a binary source.
        /// </summary>
        /// <param name="Reader">Binary deserializer.</param>
        /// <param name="DataType">Optional datatype. If not provided, will be read from the binary source.</param>
        /// <param name="Embedded">If the object is embedded into another.</param>
        /// <returns>Deserialized object.</returns>
        public override object Deserialize(BinaryDeserializer Reader, uint?DataType, bool Embedded)
        {
            if (!DataType.HasValue)
            {
                DataType = Reader.ReadBits(6);
            }

            switch (DataType.Value)
            {
            case ObjectSerializer.TYPE_BOOLEAN: return(Reader.ReadBoolean() ? (sbyte?)1 : (sbyte?)0);

            case ObjectSerializer.TYPE_BYTE: return((sbyte?)Reader.ReadByte());

            case ObjectSerializer.TYPE_INT16: return((sbyte?)Reader.ReadInt16());

            case ObjectSerializer.TYPE_INT32: return((sbyte?)Reader.ReadInt32());

            case ObjectSerializer.TYPE_INT64: return((sbyte?)Reader.ReadInt64());

            case ObjectSerializer.TYPE_SBYTE: return((sbyte?)Reader.ReadSByte());

            case ObjectSerializer.TYPE_UINT16: return((sbyte?)Reader.ReadUInt16());

            case ObjectSerializer.TYPE_UINT32: return((sbyte?)Reader.ReadUInt32());

            case ObjectSerializer.TYPE_UINT64: return((sbyte?)Reader.ReadUInt64());

            case ObjectSerializer.TYPE_DECIMAL: return((sbyte?)Reader.ReadDecimal());

            case ObjectSerializer.TYPE_DOUBLE: return((sbyte?)Reader.ReadDouble());

            case ObjectSerializer.TYPE_SINGLE: return((sbyte?)Reader.ReadSingle());

            case ObjectSerializer.TYPE_STRING:
            case ObjectSerializer.TYPE_CI_STRING: return((sbyte?)sbyte.Parse(Reader.ReadString()));

            case ObjectSerializer.TYPE_MIN: return(sbyte.MinValue);

            case ObjectSerializer.TYPE_MAX: return(sbyte.MaxValue);

            case ObjectSerializer.TYPE_NULL: return(null);

            default: throw new Exception("Expected a nullable sbyte value.");
            }
        }
Exemplo n.º 4
0
        /// <summary>
        /// Deserializes an object from a binary source.
        /// </summary>
        /// <param name="Reader">Binary deserializer.</param>
        /// <param name="DataType">Optional datatype. If not provided, will be read from the binary source.</param>
        /// <param name="Embedded">If the object is embedded into another.</param>
        /// <returns>Deserialized object.</returns>
        public override object Deserialize(BinaryDeserializer Reader, uint?DataType, bool Embedded)
        {
            if (!DataType.HasValue)
            {
                DataType = Reader.ReadBits(6);
            }

            switch (DataType.Value)
            {
            case ObjectSerializer.TYPE_BOOLEAN: return(Enum.ToObject(this.enumType, Reader.ReadBoolean() ? 1 : 0));

            case ObjectSerializer.TYPE_BYTE: return(Enum.ToObject(this.enumType, Reader.ReadByte()));

            case ObjectSerializer.TYPE_INT16: return(Enum.ToObject(this.enumType, Reader.ReadInt16()));

            case ObjectSerializer.TYPE_INT32: return(Enum.ToObject(this.enumType, Reader.ReadInt32()));

            case ObjectSerializer.TYPE_INT64: return(Enum.ToObject(this.enumType, Reader.ReadInt64()));

            case ObjectSerializer.TYPE_SBYTE: return(Enum.ToObject(this.enumType, Reader.ReadSByte()));

            case ObjectSerializer.TYPE_UINT16: return(Enum.ToObject(this.enumType, Reader.ReadUInt16()));

            case ObjectSerializer.TYPE_UINT32: return(Enum.ToObject(this.enumType, Reader.ReadUInt32()));

            case ObjectSerializer.TYPE_UINT64: return(Enum.ToObject(this.enumType, Reader.ReadUInt64()));

            case ObjectSerializer.TYPE_DECIMAL: return(Enum.ToObject(this.enumType, Reader.ReadDecimal()));

            case ObjectSerializer.TYPE_DOUBLE: return(Enum.ToObject(this.enumType, Reader.ReadDouble()));

            case ObjectSerializer.TYPE_SINGLE: return(Enum.ToObject(this.enumType, Reader.ReadSingle()));

            case ObjectSerializer.TYPE_STRING:
            case ObjectSerializer.TYPE_CI_STRING: return(Enum.Parse(this.enumType, Reader.ReadString()));

            case ObjectSerializer.TYPE_ENUM: return(Reader.ReadEnum(this.enumType));

            case ObjectSerializer.TYPE_NULL: return(null);

            default: throw new Exception("Expected an enum value.");
            }
        }
Exemplo n.º 5
0
        /// <summary>
        /// Deserializes an object from a binary source.
        /// </summary>
        /// <param name="Reader">Binary deserializer.</param>
        /// <param name="DataType">Optional datatype. If not provided, will be read from the binary source.</param>
        /// <param name="Embedded">If the object is embedded into another.</param>
        /// <returns>Deserialized object.</returns>
        public override object Deserialize(BinaryDeserializer Reader, uint?DataType, bool Embedded)
        {
            if (!DataType.HasValue)
            {
                DataType = Reader.ReadBits(6);
            }

            switch (DataType.Value)
            {
            case ObjectSerializer.TYPE_BOOLEAN: return(Reader.ReadBoolean());

            case ObjectSerializer.TYPE_BYTE: return(Reader.ReadByte() != 0);

            case ObjectSerializer.TYPE_INT16: return(Reader.ReadInt16() != 0);

            case ObjectSerializer.TYPE_INT32: return(Reader.ReadInt32() != 0);

            case ObjectSerializer.TYPE_INT64: return(Reader.ReadInt64() != 0);

            case ObjectSerializer.TYPE_SBYTE: return(Reader.ReadSByte() != 0);

            case ObjectSerializer.TYPE_UINT16: return(Reader.ReadUInt16() != 0);

            case ObjectSerializer.TYPE_UINT32: return(Reader.ReadUInt32() != 0);

            case ObjectSerializer.TYPE_UINT64: return(Reader.ReadUInt64() != 0);

            case ObjectSerializer.TYPE_DECIMAL: return(Reader.ReadDecimal() != 0);

            case ObjectSerializer.TYPE_DOUBLE: return(Reader.ReadDouble() != 0);

            case ObjectSerializer.TYPE_SINGLE: return(Reader.ReadSingle() != 0);

            case ObjectSerializer.TYPE_MIN: return(false);

            case ObjectSerializer.TYPE_MAX: return(true);

            case ObjectSerializer.TYPE_NULL: return(null);

            default: throw new Exception("Expected a boolean value.");
            }
        }
        public void DBFiles_BinSerialization_Test_01_Serialization()
        {
            BinarySerializer Serializer = new BinarySerializer(string.Empty, Encoding.UTF8, true);

            Serializer.Write(true);
            Serializer.Write(false);
            Serializer.Write((byte)25);
            Serializer.Write((short)1234);
            Serializer.Write((short)-1234);
            Serializer.Write((int)12345678);
            Serializer.Write((int)-12345678);
            Serializer.Write((long)1234567890123456789);
            Serializer.Write((long)-1234567890123456789);
            Serializer.Write((sbyte)-45);
            Serializer.Write((ushort)1234);
            Serializer.Write((uint)12345678);
            Serializer.Write((ulong)1234567890123456789);
            Serializer.Write((decimal)1234567890.123456789);
            Serializer.Write(1234567890.123456789);
            Serializer.Write(1234567890.123456789f);
            Serializer.Write(new DateTime(2016, 10, 06, 20, 32, 0));
            Serializer.Write(new TimeSpan(1, 2, 3, 4, 5));
            Serializer.Write('☀');
            Serializer.Write(new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 });
            Serializer.Write("Today, there will be a lot of ☀.");
            Serializer.WriteVariableLengthUInt64(0);
            Serializer.WriteVariableLengthUInt64(100);
            Serializer.WriteVariableLengthUInt64(10000);
            Serializer.WriteVariableLengthUInt64(1000000);
            Serializer.WriteVariableLengthUInt64(100000000);
            Serializer.WriteVariableLengthUInt64(10000000000);
            Serializer.WriteVariableLengthUInt64(1000000000000);
            Serializer.WriteVariableLengthUInt64(100000000000000);
            Serializer.WriteVariableLengthUInt64(10000000000000000);
            Serializer.WriteBit(false);
            Serializer.WriteBit(true);
            Serializer.WriteBits(10, 4);
            Serializer.WriteBits(100, 7);
            Serializer.WriteBits(1000, 10);
            Serializer.WriteBits(10000, 14);
            Serializer.Write(NormalEnum.Option2);
            Serializer.Write(FlagsEnum.Option2 | FlagsEnum.Option4);

            byte[]             Data         = Serializer.GetSerialization();
            BinaryDeserializer Deserializer = new BinaryDeserializer(string.Empty, Encoding.UTF8, Data, true);

            AssertEx.Same(true, Deserializer.ReadBoolean());
            AssertEx.Same(false, Deserializer.ReadBoolean());
            AssertEx.Same((byte)25, Deserializer.ReadByte());
            AssertEx.Same((short)1234, Deserializer.ReadInt16());
            AssertEx.Same((short)-1234, Deserializer.ReadInt16());
            AssertEx.Same((int)12345678, Deserializer.ReadInt32());
            AssertEx.Same((int)-12345678, Deserializer.ReadInt32());
            AssertEx.Same((long)1234567890123456789, Deserializer.ReadInt64());
            AssertEx.Same((long)-1234567890123456789, Deserializer.ReadInt64());
            AssertEx.Same((sbyte)-45, Deserializer.ReadSByte());
            AssertEx.Same((ushort)1234, Deserializer.ReadUInt16());
            AssertEx.Same((uint)12345678, Deserializer.ReadUInt32());
            AssertEx.Same((ulong)1234567890123456789, Deserializer.ReadUInt64());
            AssertEx.Same((decimal)1234567890.123456789, Deserializer.ReadDecimal());
            AssertEx.Same(1234567890.123456789, Deserializer.ReadDouble());
            AssertEx.Same(1234567890.123456789f, Deserializer.ReadSingle());
            AssertEx.Same(new DateTime(2016, 10, 06, 20, 32, 0), Deserializer.ReadDateTime());
            AssertEx.Same(new TimeSpan(1, 2, 3, 4, 5), Deserializer.ReadTimeSpan());
            AssertEx.Same('☀', Deserializer.ReadChar());
            AssertEx.Same(new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }, Deserializer.ReadByteArray());
            AssertEx.Same("Today, there will be a lot of ☀.", Deserializer.ReadString());
            AssertEx.Same(0, Deserializer.ReadVariableLengthUInt64());
            AssertEx.Same(100, Deserializer.ReadVariableLengthUInt64());
            AssertEx.Same(10000, Deserializer.ReadVariableLengthUInt64());
            AssertEx.Same(1000000, Deserializer.ReadVariableLengthUInt64());
            AssertEx.Same(100000000, Deserializer.ReadVariableLengthUInt64());
            AssertEx.Same(10000000000, Deserializer.ReadVariableLengthUInt64());
            AssertEx.Same(1000000000000, Deserializer.ReadVariableLengthUInt64());
            AssertEx.Same(100000000000000, Deserializer.ReadVariableLengthUInt64());
            AssertEx.Same(10000000000000000, Deserializer.ReadVariableLengthUInt64());
            AssertEx.Same(false, Deserializer.ReadBit());
            AssertEx.Same(true, Deserializer.ReadBit());
            AssertEx.Same(10, Deserializer.ReadBits(4));
            AssertEx.Same(100, Deserializer.ReadBits(7));
            AssertEx.Same(1000, Deserializer.ReadBits(10));
            AssertEx.Same(10000, Deserializer.ReadBits(14));
            AssertEx.Same(NormalEnum.Option2, Deserializer.ReadEnum(typeof(NormalEnum)));
            AssertEx.Same(FlagsEnum.Option2 | FlagsEnum.Option4, Deserializer.ReadEnum(typeof(FlagsEnum)));
        }