Пример #1
0
        /// <summary>
        /// Reads a single value.
        /// </summary>
        /// <param name="Reader">Binary reader.</param>
        /// <param name="FieldDataType">Field data type.</param>
        /// <returns>Single value.</returns>
        /// <exception cref="ArgumentException">If the <paramref name="FieldDataType"/> was invalid.</exception>
        public static float ReadSingle(BinaryDeserializer Reader, uint FieldDataType)
        {
            switch (FieldDataType)
            {
            case ObjectSerializer.TYPE_BOOLEAN: return(Reader.ReadBoolean() ? 1 : 0);

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

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

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

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

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

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

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

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

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

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

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

            case ObjectSerializer.TYPE_STRING: return(float.Parse(Reader.ReadString()));

            default:
                throw new ArgumentException("Expected a single value, but was a " +
                                            FilesProvider.GetFieldDataTypeName(FieldDataType) + ".", nameof(FieldDataType));
            }
        }
Пример #2
0
        /// <summary>
        /// Reads a typed array.
        /// </summary>
        /// <param name="T">Element type.</param>
        /// <param name="Provider">Database provider object.</param>
        /// <param name="Reader">Binary reader.</param>
        /// <param name="FieldDataType">Field data type.</param>
        /// <returns>String value.</returns>
        /// <exception cref="ArgumentException">If the <paramref name="FieldDataType"/> was invalid.</exception>
        public static Array ReadArray(Type T, FilesProvider Provider, BinaryDeserializer Reader, uint FieldDataType)
        {
            switch (FieldDataType)
            {
            case ObjectSerializer.TYPE_ARRAY:
                IObjectSerializer S          = Provider.GetObjectSerializer(T);
                ulong             NrElements = Reader.ReadVariableLengthUInt64();
                if (NrElements > int.MaxValue)
                {
                    throw new Exception("Array too long.");
                }

                int   i, c = (int)NrElements;;
                Array Result = Array.CreateInstance(T, c);

                uint ElementDataType  = Reader.ReadBits(6);
                uint?ElementDataTypeN = ElementDataType == ObjectSerializer.TYPE_NULL ? (uint?)null : (uint?)ElementDataType;

                for (i = 0; i < c; i++)
                {
                    Result.SetValue(S.Deserialize(Reader, ElementDataTypeN, true), i);
                }

                return(Result);

            case ObjectSerializer.TYPE_NULL:
                return(null);

            default:
                throw new Exception("Array expected.");
            }
        }
Пример #3
0
        /// <summary>
        /// Reads a typed array.
        /// </summary>
        /// <typeparam name="T">Element type.</typeparam>
        /// <param name="Provider">Database provider object.</param>
        /// <param name="Reader">Binary reader.</param>
        /// <param name="FieldDataType">Field data type.</param>
        /// <returns>String value.</returns>
        /// <exception cref="ArgumentException">If the <paramref name="FieldDataType"/> was invalid.</exception>
        public static T[] ReadArray <T>(FilesProvider Provider, BinaryDeserializer Reader, uint FieldDataType)
        {
            switch (FieldDataType)
            {
            case ObjectSerializer.TYPE_ARRAY:
                List <T>          Elements   = new List <T>();
                IObjectSerializer S          = Provider.GetObjectSerializer(typeof(T));
                ulong             NrElements = Reader.ReadVariableLengthUInt64();
                uint ElementDataType         = Reader.ReadBits(6);
                uint?ElementDataTypeN        = ElementDataType == ObjectSerializer.TYPE_NULL ? (uint?)null : (uint?)ElementDataType;

                while (NrElements-- > 0)
                {
                    Elements.Add((T)S.Deserialize(Reader, ElementDataTypeN, true));
                }

                return(Elements.ToArray());

            case ObjectSerializer.TYPE_NULL:
                return(null);

            default:
                throw new Exception("Array expected.");
            }
        }
Пример #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>
        /// <param name="CheckFieldNames">If field names are to be extended.</param>
        /// <returns>Deserialized object.</returns>
        public object Deserialize(BinaryDeserializer Reader, uint?DataType, bool Embedded, bool CheckFieldNames)
        {
            if (!Reader.ReadBit())
            {
                return(null);
            }

            string Key = Reader.ReadString();
            object Value;

            DataType = Reader.ReadBits(6);

            switch (DataType.Value)
            {
            case ObjectSerializer.TYPE_OBJECT:
                int               Pos      = Reader.Position;
                string            TypeName = Reader.ReadString();
                IObjectSerializer Serializer;

                if (string.IsNullOrEmpty(TypeName))
                {
                    Serializer = this.genericSerializer;
                }
                else
                {
                    Type T = Types.GetType(TypeName);
                    if (!(T is null))
                    {
                        Serializer = this.provider.GetObjectSerializer(T);
                    }
Пример #5
0
        private Array ReadGenericArray(BinaryDeserializer Reader)
        {
            ulong NrElements      = Reader.ReadVariableLengthUInt64();
            uint  ElementDataType = Reader.ReadBits(6);

            switch (ElementDataType)
            {
            case ObjectSerializer.TYPE_BOOLEAN: return(this.ReadArray <bool>(Reader, NrElements, ElementDataType));

            case ObjectSerializer.TYPE_BYTE: return(this.ReadArray <byte>(Reader, NrElements, ElementDataType));

            case ObjectSerializer.TYPE_INT16: return(this.ReadArray <short>(Reader, NrElements, ElementDataType));

            case ObjectSerializer.TYPE_INT32: return(this.ReadArray <int>(Reader, NrElements, ElementDataType));

            case ObjectSerializer.TYPE_INT64: return(this.ReadArray <long>(Reader, NrElements, ElementDataType));

            case ObjectSerializer.TYPE_SBYTE: return(this.ReadArray <sbyte>(Reader, NrElements, ElementDataType));

            case ObjectSerializer.TYPE_UINT16: return(this.ReadArray <ushort>(Reader, NrElements, ElementDataType));

            case ObjectSerializer.TYPE_UINT32: return(this.ReadArray <uint>(Reader, NrElements, ElementDataType));

            case ObjectSerializer.TYPE_UINT64: return(this.ReadArray <ulong>(Reader, NrElements, ElementDataType));

            case ObjectSerializer.TYPE_DECIMAL: return(this.ReadArray <decimal>(Reader, NrElements, ElementDataType));

            case ObjectSerializer.TYPE_DOUBLE: return(this.ReadArray <double>(Reader, NrElements, ElementDataType));

            case ObjectSerializer.TYPE_SINGLE: return(this.ReadArray <float>(Reader, NrElements, ElementDataType));

            case ObjectSerializer.TYPE_DATETIME: return(this.ReadArray <DateTime>(Reader, NrElements, ElementDataType));

            case ObjectSerializer.TYPE_DATETIMEOFFSET: return(this.ReadArray <DateTimeOffset>(Reader, NrElements, ElementDataType));

            case ObjectSerializer.TYPE_TIMESPAN: return(this.ReadArray <TimeSpan>(Reader, NrElements, ElementDataType));

            case ObjectSerializer.TYPE_CHAR: return(this.ReadArray <char>(Reader, NrElements, ElementDataType));

            case ObjectSerializer.TYPE_STRING:
            case ObjectSerializer.TYPE_ENUM: return(this.ReadArray <string>(Reader, NrElements, ElementDataType));

            case ObjectSerializer.TYPE_CI_STRING: return(this.ReadArray <CaseInsensitiveString>(Reader, NrElements, ElementDataType));

            case ObjectSerializer.TYPE_BYTEARRAY: return(this.ReadArray <byte[]>(Reader, NrElements, ElementDataType));

            case ObjectSerializer.TYPE_GUID: return(this.ReadArray <Guid>(Reader, NrElements, ElementDataType));

            case ObjectSerializer.TYPE_ARRAY: return(this.ReadArrayOfArrays(Reader, NrElements));

            case ObjectSerializer.TYPE_OBJECT: return(this.ReadArrayOfObjects(Reader, NrElements, ElementDataType));

            case ObjectSerializer.TYPE_NULL: return(this.ReadArrayOfNullableElements(Reader, NrElements));

            default: throw new Exception("Unrecognized data type: " + ElementDataType.ToString());
            }
        }
        private Array[] ReadArrayOfArrays(BinaryDeserializer Reader, ulong NrElements)
        {
            List <Array> Elements = new List <Array>();

            while (NrElements-- > 0)
            {
                Elements.Add(this.ReadGenericArray(Reader));
            }

            return(Elements.ToArray());
        }
Пример #7
0
 /// <summary>
 /// Reads a nullable 64-bit unsigned integer value.
 /// </summary>
 /// <param name="Reader">Binary reader.</param>
 /// <param name="FieldDataType">Field data type.</param>
 /// <returns>Nullable unsigned 64-bit integer value.</returns>
 /// <exception cref="ArgumentException">If the <paramref name="FieldDataType"/> was invalid.</exception>
 public static ulong?ReadNullableUInt64(BinaryDeserializer Reader, uint FieldDataType)
 {
     if (FieldDataType == ObjectSerializer.TYPE_NULL)
     {
         return(null);
     }
     else
     {
         return(ReadUInt64(Reader, FieldDataType));
     }
 }
Пример #8
0
 /// <summary>
 /// Reads a nullable enum stored as a nullable 32-bit integer value.
 /// </summary>
 /// <param name="Reader">Binary reader.</param>
 /// <param name="FieldDataType">Field data type.</param>
 /// <param name="EnumType">Enumeration type.</param>
 /// <returns>Nullable enumeration value.</returns>
 /// <exception cref="ArgumentException">If the <paramref name="FieldDataType"/> was invalid.</exception>
 public static object ReadNullableEnum(BinaryDeserializer Reader, uint FieldDataType, Type EnumType)
 {
     if (FieldDataType == ObjectSerializer.TYPE_NULL)
     {
         return(null);
     }
     else
     {
         return(Enum.ToObject(EnumType, ReadInt32(Reader, FieldDataType)));
     }
 }
        private GenericObject[] ReadArrayOfObjects(BinaryDeserializer Reader, ulong NrElements, uint ElementDataType)
        {
            List <GenericObject> Elements = new List <GenericObject>();

            while (NrElements-- > 0)
            {
                Elements.Add((GenericObject)this.Deserialize(Reader, ElementDataType, true));
            }

            return(Elements.ToArray());
        }
Пример #10
0
 /// <summary>
 /// Reads a nullable single value.
 /// </summary>
 /// <param name="Reader">Binary reader.</param>
 /// <param name="FieldDataType">Field data type.</param>
 /// <returns>Nullable single value.</returns>
 /// <exception cref="ArgumentException">If the <paramref name="FieldDataType"/> was invalid.</exception>
 public static float?ReadNullableSingle(BinaryDeserializer Reader, uint FieldDataType)
 {
     if (FieldDataType == ObjectSerializer.TYPE_NULL)
     {
         return(null);
     }
     else
     {
         return(ReadSingle(Reader, FieldDataType));
     }
 }
Пример #11
0
 /// <summary>
 /// Reads a nullable decimal value.
 /// </summary>
 /// <param name="Reader">Binary reader.</param>
 /// <param name="FieldDataType">Field data type.</param>
 /// <returns>Nullable decimal value.</returns>
 /// <exception cref="ArgumentException">If the <paramref name="FieldDataType"/> was invalid.</exception>
 public static decimal?ReadNullableDecimal(BinaryDeserializer Reader, uint FieldDataType)
 {
     if (FieldDataType == ObjectSerializer.TYPE_NULL)
     {
         return(null);
     }
     else
     {
         return(ReadDecimal(Reader, FieldDataType));
     }
 }
Пример #12
0
 /// <summary>
 /// Reads a nullable GUID value.
 /// </summary>
 /// <param name="Reader">Binary reader.</param>
 /// <param name="FieldDataType">Field data type.</param>
 /// <returns>Nullable Guid value.</returns>
 /// <exception cref="ArgumentException">If the <paramref name="FieldDataType"/> was invalid.</exception>
 public static Guid?ReadNullableGuid(BinaryDeserializer Reader, uint FieldDataType)
 {
     if (FieldDataType == ObjectSerializer.TYPE_NULL)
     {
         return(null);
     }
     else
     {
         return(ReadGuid(Reader, FieldDataType));
     }
 }
Пример #13
0
        private T[] ReadArray <T>(BinaryDeserializer Reader, ulong NrElements, uint ElementDataType)
        {
            List <T>          Elements = new List <T>();
            IObjectSerializer S        = this.provider.GetObjectSerializer(typeof(T));

            while (NrElements-- > 0)
            {
                Elements.Add((T)S.Deserialize(Reader, ElementDataType, true));
            }

            return(Elements.ToArray());
        }
Пример #14
0
        /// <summary>
        /// Reads a GUID value.
        /// </summary>
        /// <param name="Reader">Binary reader.</param>
        /// <param name="FieldDataType">Field data type.</param>
        /// <returns>Guid value.</returns>
        /// <exception cref="ArgumentException">If the <paramref name="FieldDataType"/> was invalid.</exception>
        public static Guid ReadGuid(BinaryDeserializer Reader, uint FieldDataType)
        {
            switch (FieldDataType)
            {
            case ObjectSerializer.TYPE_GUID: return(Reader.ReadGuid());

            case ObjectSerializer.TYPE_STRING: return(Guid.Parse(Reader.ReadString()));

            default:
                throw new ArgumentException("Expected a GUID value, but was a " +
                                            FilesProvider.GetFieldDataTypeName(FieldDataType) + ".", nameof(FieldDataType));
            }
        }
Пример #15
0
        /// <summary>
        /// Reads a time span value.
        /// </summary>
        /// <param name="Reader">Binary reader.</param>
        /// <param name="FieldDataType">Field data type.</param>
        /// <returns>TimeSpan value.</returns>
        /// <exception cref="ArgumentException">If the <paramref name="FieldDataType"/> was invalid.</exception>
        public static TimeSpan ReadTimeSpan(BinaryDeserializer Reader, uint FieldDataType)
        {
            switch (FieldDataType)
            {
            case ObjectSerializer.TYPE_TIMESPAN: return(Reader.ReadTimeSpan());

            case ObjectSerializer.TYPE_STRING: return(TimeSpan.Parse(Reader.ReadString()));

            default:
                throw new ArgumentException("Expected a time span value, but was a " +
                                            FilesProvider.GetFieldDataTypeName(FieldDataType) + ".", nameof(FieldDataType));
            }
        }
Пример #16
0
        /// <summary>
        /// Reads a date &amp; time value.
        /// </summary>
        /// <param name="Reader">Binary reader.</param>
        /// <param name="FieldDataType">Field data type.</param>
        /// <returns>DateTime value.</returns>
        /// <exception cref="ArgumentException">If the <paramref name="FieldDataType"/> was invalid.</exception>
        public static DateTime ReadDateTime(BinaryDeserializer Reader, uint FieldDataType)
        {
            switch (FieldDataType)
            {
            case ObjectSerializer.TYPE_DATETIME: return(Reader.ReadDateTime());

            case ObjectSerializer.TYPE_STRING: return(DateTime.Parse(Reader.ReadString()));

            default:
                throw new ArgumentException("Expected a date & time value, but was a " +
                                            FilesProvider.GetFieldDataTypeName(FieldDataType) + ".", nameof(FieldDataType));
            }
        }
Пример #17
0
        /// <summary>
        /// Reads a string value.
        /// </summary>
        /// <param name="Reader">Binary reader.</param>
        /// <param name="FieldDataType">Field data type.</param>
        /// <returns>String value.</returns>
        /// <exception cref="ArgumentException">If the <paramref name="FieldDataType"/> was invalid.</exception>
        public static string ReadString(BinaryDeserializer Reader, uint FieldDataType)
        {
            switch (FieldDataType)
            {
            case ObjectSerializer.TYPE_STRING: return(Reader.ReadString());

            case ObjectSerializer.TYPE_ENUM: return(Reader.ReadString());

            case ObjectSerializer.TYPE_NULL: return(null);

            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_DATETIME: return(Reader.ReadDateTime().ToString());

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

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

            default:
                throw new ArgumentException("Expected a char value, but was a " +
                                            FilesProvider.GetFieldDataTypeName(FieldDataType) + ".", nameof(FieldDataType));
            }
        }
Пример #18
0
        /// <summary>
        /// Reads a char value.
        /// </summary>
        /// <param name="Reader">Binary reader.</param>
        /// <param name="FieldDataType">Field data type.</param>
        /// <returns>Char value.</returns>
        /// <exception cref="ArgumentException">If the <paramref name="FieldDataType"/> was invalid.</exception>
        public static char ReadChar(BinaryDeserializer Reader, uint FieldDataType)
        {
            switch (FieldDataType)
            {
            case ObjectSerializer.TYPE_CHAR: return(Reader.ReadChar());

            case ObjectSerializer.TYPE_BOOLEAN: return(Reader.ReadBoolean() ? (char)1 : (char)0);

            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_STRING:
                string s = Reader.ReadString();
                if (string.IsNullOrEmpty(s))
                {
                    return((char)0);
                }
                else
                {
                    return(s[0]);
                }

            default:
                throw new ArgumentException("Expected a char value, but was a " +
                                            FilesProvider.GetFieldDataTypeName(FieldDataType) + ".", nameof(FieldDataType));
            }
        }
Пример #19
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>
        /// <param name="CheckFieldNames">If field names are to be extended.</param>
        /// <returns>Deserialized object.</returns>
        public object Deserialize(BinaryDeserializer Reader, uint?DataType, bool Embedded, bool CheckFieldNames)
        {
            if (!Reader.ReadBit())
            {
                return(null);
            }

            string Key = Reader.ReadString();
            object Value;

            DataType = Reader.ReadBits(6);

            switch (DataType.Value)
            {
            case ObjectSerializer.TYPE_OBJECT:
                int               Pos      = Reader.Position;
                string            TypeName = Reader.ReadString();
                IObjectSerializer Serializer;

                if (string.IsNullOrEmpty(TypeName))
                {
                    Serializer = this.genericSerializer;
                }
                else
                {
                    Type T = Types.GetType(TypeName);
                    if (T != null)
                    {
                        Serializer = this.provider.GetObjectSerializer(T);
                    }
                    else
                    {
                        Serializer = this.genericSerializer;
                    }
                }

                Reader.Position = Pos;

                Value = Serializer.Deserialize(Reader, ObjectSerializer.TYPE_OBJECT, false);
                break;

            case ObjectSerializer.TYPE_BOOLEAN:
                Value = Reader.ReadBit();
                break;

            case ObjectSerializer.TYPE_BYTE:
                Value = Reader.ReadByte();
                break;

            case ObjectSerializer.TYPE_INT16:
                Value = Reader.ReadInt16();
                break;

            case ObjectSerializer.TYPE_INT32:
                Value = Reader.ReadInt32();
                break;

            case ObjectSerializer.TYPE_INT64:
                Value = Reader.ReadInt64();
                break;

            case ObjectSerializer.TYPE_SBYTE:
                Value = Reader.ReadSByte();
                break;

            case ObjectSerializer.TYPE_UINT16:
                Value = Reader.ReadUInt16();
                break;

            case ObjectSerializer.TYPE_UINT32:
                Value = Reader.ReadUInt32();
                break;

            case ObjectSerializer.TYPE_UINT64:
                Value = Reader.ReadUInt64();
                break;

            case ObjectSerializer.TYPE_DECIMAL:
                Value = Reader.ReadDecimal();
                break;

            case ObjectSerializer.TYPE_DOUBLE:
                Value = Reader.ReadDouble();
                break;

            case ObjectSerializer.TYPE_SINGLE:
                Value = Reader.ReadSingle();
                break;

            case ObjectSerializer.TYPE_DATETIME:
                Value = Reader.ReadDateTime();
                break;

            case ObjectSerializer.TYPE_TIMESPAN:
                Value = Reader.ReadTimeSpan();
                break;

            case ObjectSerializer.TYPE_CHAR:
                Value = Reader.ReadChar();
                break;

            case ObjectSerializer.TYPE_STRING:
                Value = Reader.ReadString();
                break;

            case ObjectSerializer.TYPE_ENUM:
                Value = Reader.ReadString();
                break;

            case ObjectSerializer.TYPE_BYTEARRAY:
                Value = Reader.ReadByteArray();
                break;

            case ObjectSerializer.TYPE_GUID:
                Value = Reader.ReadGuid();
                break;

            case ObjectSerializer.TYPE_NULL:
                Value = null;
                break;

            default:
                throw new Exception("Object or value expected.");
            }

            return(new KeyValuePair <string, object>(Key, Value));
        }
Пример #20
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)
 {
     return(this.Deserialize(Reader, DataType, Embedded, true));
 }
Пример #21
0
        private object[] ReadArrayOfNullableElements(BinaryDeserializer Reader, ulong NrElements)
        {
            List <object> Elements = new List <object>();
            uint          ElementDataType;

            while (NrElements-- > 0)
            {
                ElementDataType = Reader.ReadBits(6);

                switch (ElementDataType)
                {
                case ObjectSerializer.TYPE_BOOLEAN:
                    Elements.Add(Reader.ReadBoolean());
                    break;

                case ObjectSerializer.TYPE_BYTE:
                    Elements.Add(Reader.ReadByte());
                    break;

                case ObjectSerializer.TYPE_INT16:
                    Elements.Add(Reader.ReadInt16());
                    break;

                case ObjectSerializer.TYPE_INT32:
                    Elements.Add(Reader.ReadInt32());
                    break;

                case ObjectSerializer.TYPE_INT64:
                    Elements.Add(Reader.ReadInt64());
                    break;

                case ObjectSerializer.TYPE_SBYTE:
                    Elements.Add(Reader.ReadSByte());
                    break;

                case ObjectSerializer.TYPE_UINT16:
                    Elements.Add(Reader.ReadUInt16());
                    break;

                case ObjectSerializer.TYPE_UINT32:
                    Elements.Add(Reader.ReadUInt32());
                    break;

                case ObjectSerializer.TYPE_UINT64:
                    Elements.Add(Reader.ReadUInt64());
                    break;

                case ObjectSerializer.TYPE_DECIMAL:
                    Elements.Add(Reader.ReadDecimal());
                    break;

                case ObjectSerializer.TYPE_DOUBLE:
                    Elements.Add(Reader.ReadDouble());
                    break;

                case ObjectSerializer.TYPE_SINGLE:
                    Elements.Add(Reader.ReadSingle());
                    break;

                case ObjectSerializer.TYPE_DATETIME:
                    Elements.Add(Reader.ReadDateTime());
                    break;

                case ObjectSerializer.TYPE_TIMESPAN:
                    Elements.Add(Reader.ReadTimeSpan());
                    break;

                case ObjectSerializer.TYPE_CHAR:
                    Elements.Add(Reader.ReadChar());
                    break;

                case ObjectSerializer.TYPE_STRING:
                case ObjectSerializer.TYPE_ENUM:
                    Elements.Add(Reader.ReadString());
                    break;

                case ObjectSerializer.TYPE_BYTEARRAY:
                    Elements.Add(Reader.ReadByteArray());
                    break;

                case ObjectSerializer.TYPE_GUID:
                    Elements.Add(Reader.ReadGuid());
                    break;

                case ObjectSerializer.TYPE_ARRAY:
                    Elements.Add(this.ReadGenericArray(Reader));
                    break;

                case ObjectSerializer.TYPE_OBJECT:
                    Elements.Add(this.Deserialize(Reader, ElementDataType, true));
                    break;

                case ObjectSerializer.TYPE_NULL:
                    Elements.Add(null);
                    break;

                default:
                    throw new Exception("Unrecognized data type: " + ElementDataType.ToString());
                }
            }

            return(Elements.ToArray());
        }
Пример #22
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>
        /// <param name="CheckFieldNames">If field names are to be extended.</param>
        /// <returns>Deserialized object.</returns>
        public object Deserialize(BinaryDeserializer Reader, uint?DataType, bool Embedded, bool CheckFieldNames)
        {
            uint           FieldDataType;
            ulong          FieldCode;
            ulong          CollectionCode;
            StreamBookmark Bookmark    = Reader.GetBookmark();
            uint?          DataTypeBak = DataType;
            Guid           ObjectId    = Embedded ? Guid.Empty : Reader.ReadGuid();
            ulong          ContentLen  = Embedded ? 0 : Reader.ReadVariableLengthUInt64();
            string         TypeName;
            string         FieldName;
            string         CollectionName;

            if (!DataType.HasValue)
            {
                DataType = Reader.ReadBits(6);
                if (DataType.Value == ObjectSerializer.TYPE_NULL)
                {
                    return(null);
                }
            }

            switch (DataType.Value)
            {
            case ObjectSerializer.TYPE_OBJECT:
                break;

            case ObjectSerializer.TYPE_BOOLEAN:
                return(Reader.ReadBit());

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

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

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

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

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

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

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

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

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

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

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

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

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

            case ObjectSerializer.TYPE_CHAR:
                return(Reader.ReadChar());

            case ObjectSerializer.TYPE_STRING:
                return(Reader.ReadString());

            case ObjectSerializer.TYPE_ENUM:
                return(Reader.ReadString());

            case ObjectSerializer.TYPE_BYTEARRAY:
                return(Reader.ReadByteArray());

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

            case ObjectSerializer.TYPE_NULL:
                return(null);

            default:
                throw new Exception("Object or value expected.");
            }

            FieldCode = Reader.ReadVariableLengthUInt64();

            if (Embedded)
            {
                CollectionCode = Reader.ReadVariableLengthUInt64();
                CollectionName = this.provider.GetFieldName(null, CollectionCode);
            }
            else
            {
                CollectionName = Reader.CollectionName;
            }

            if (FieldCode == 0)
            {
                TypeName = string.Empty;
            }
            else if (CheckFieldNames)
            {
                TypeName = this.provider.GetFieldName(CollectionName, FieldCode);
            }
            else
            {
                TypeName = CollectionName + "." + FieldCode.ToString();
            }

            LinkedList <KeyValuePair <string, object> > Properties = new LinkedList <KeyValuePair <string, object> >();

            while ((FieldCode = Reader.ReadVariableLengthUInt64()) != 0)
            {
                if (CheckFieldNames)
                {
                    FieldName = this.provider.GetFieldName(CollectionName, FieldCode);
                }
                else
                {
                    FieldName = CollectionName + "." + FieldCode.ToString();
                }

                FieldDataType = Reader.ReadBits(6);

                switch (FieldDataType)
                {
                case ObjectSerializer.TYPE_BOOLEAN:
                    Properties.AddLast(new KeyValuePair <string, object>(FieldName, Reader.ReadBoolean()));
                    break;

                case ObjectSerializer.TYPE_BYTE:
                    Properties.AddLast(new KeyValuePair <string, object>(FieldName, Reader.ReadByte()));
                    break;

                case ObjectSerializer.TYPE_INT16:
                    Properties.AddLast(new KeyValuePair <string, object>(FieldName, Reader.ReadInt16()));
                    break;

                case ObjectSerializer.TYPE_INT32:
                    Properties.AddLast(new KeyValuePair <string, object>(FieldName, Reader.ReadInt32()));
                    break;

                case ObjectSerializer.TYPE_INT64:
                    Properties.AddLast(new KeyValuePair <string, object>(FieldName, Reader.ReadInt64()));
                    break;

                case ObjectSerializer.TYPE_SBYTE:
                    Properties.AddLast(new KeyValuePair <string, object>(FieldName, Reader.ReadSByte()));
                    break;

                case ObjectSerializer.TYPE_UINT16:
                    Properties.AddLast(new KeyValuePair <string, object>(FieldName, Reader.ReadUInt16()));
                    break;

                case ObjectSerializer.TYPE_UINT32:
                    Properties.AddLast(new KeyValuePair <string, object>(FieldName, Reader.ReadUInt32()));
                    break;

                case ObjectSerializer.TYPE_UINT64:
                    Properties.AddLast(new KeyValuePair <string, object>(FieldName, Reader.ReadUInt64()));
                    break;

                case ObjectSerializer.TYPE_DECIMAL:
                    Properties.AddLast(new KeyValuePair <string, object>(FieldName, Reader.ReadDecimal()));
                    break;

                case ObjectSerializer.TYPE_DOUBLE:
                    Properties.AddLast(new KeyValuePair <string, object>(FieldName, Reader.ReadDouble()));
                    break;

                case ObjectSerializer.TYPE_SINGLE:
                    Properties.AddLast(new KeyValuePair <string, object>(FieldName, Reader.ReadSingle()));
                    break;

                case ObjectSerializer.TYPE_DATETIME:
                    Properties.AddLast(new KeyValuePair <string, object>(FieldName, Reader.ReadDateTime()));
                    break;

                case ObjectSerializer.TYPE_TIMESPAN:
                    Properties.AddLast(new KeyValuePair <string, object>(FieldName, Reader.ReadTimeSpan()));
                    break;

                case ObjectSerializer.TYPE_CHAR:
                    Properties.AddLast(new KeyValuePair <string, object>(FieldName, Reader.ReadChar()));
                    break;

                case ObjectSerializer.TYPE_STRING:
                case ObjectSerializer.TYPE_ENUM:
                    Properties.AddLast(new KeyValuePair <string, object>(FieldName, Reader.ReadString()));
                    break;

                case ObjectSerializer.TYPE_BYTEARRAY:
                    Properties.AddLast(new KeyValuePair <string, object>(FieldName, Reader.ReadByteArray()));
                    break;

                case ObjectSerializer.TYPE_GUID:
                    Properties.AddLast(new KeyValuePair <string, object>(FieldName, Reader.ReadGuid()));
                    break;

                case ObjectSerializer.TYPE_NULL:
                    Properties.AddLast(new KeyValuePair <string, object>(FieldName, null));
                    break;

                case ObjectSerializer.TYPE_ARRAY:
                    Properties.AddLast(new KeyValuePair <string, object>(FieldName, this.ReadGenericArray(Reader)));
                    break;

                case ObjectSerializer.TYPE_OBJECT:
                    Properties.AddLast(new KeyValuePair <string, object>(FieldName, this.Deserialize(Reader, FieldDataType, true)));
                    break;

                default:
                    throw new Exception("Unrecognized data type: " + FieldDataType.ToString());
                }
            }

            return(new GenericObject(CollectionName, TypeName, ObjectId, Properties));
        }
Пример #23
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 abstract object Deserialize(BinaryDeserializer Reader, uint?DataType, bool Embedded);