Exemplo n.º 1
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));
            }
        }
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>
        /// <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));
        }
        /// <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));
        }
        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());
        }