/// <summary>
        /// Deserializes an object from a binary source.
        /// </summary>
        /// <param name="Reader">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(IDeserializer 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.");
            }
        }
Example #2
0
        /// <summary>
        /// Deserializes an object from a binary source.
        /// </summary>
        /// <param name="Reader">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 Task <object> Deserialize(IDeserializer Reader, uint?DataType, bool Embedded)
        {
            if (!DataType.HasValue)
            {
                DataType = Reader.ReadBits(6);
            }

            switch (DataType.Value)
            {
            case ObjectSerializer.TYPE_CHAR: return(Task.FromResult <object>(Reader.ReadChar()));

            case ObjectSerializer.TYPE_BYTE: return(Task.FromResult <object>((char)Reader.ReadByte()));

            case ObjectSerializer.TYPE_INT16: return(Task.FromResult <object>((char)Reader.ReadInt16()));

            case ObjectSerializer.TYPE_INT32: return(Task.FromResult <object>((char)Reader.ReadInt32()));

            case ObjectSerializer.TYPE_INT64: return(Task.FromResult <object>((char)Reader.ReadInt64()));

            case ObjectSerializer.TYPE_SBYTE: return(Task.FromResult <object>((char)Reader.ReadSByte()));

            case ObjectSerializer.TYPE_UINT16: return(Task.FromResult <object>((char)Reader.ReadUInt16()));

            case ObjectSerializer.TYPE_UINT32: return(Task.FromResult <object>((char)Reader.ReadUInt32()));

            case ObjectSerializer.TYPE_UINT64: return(Task.FromResult <object>((char)Reader.ReadUInt64()));

            case ObjectSerializer.TYPE_DECIMAL: return(Task.FromResult <object>((char)Reader.ReadDecimal()));

            case ObjectSerializer.TYPE_DOUBLE: return(Task.FromResult <object>((char)Reader.ReadDouble()));

            case ObjectSerializer.TYPE_SINGLE: return(Task.FromResult <object>((char)Reader.ReadSingle()));

            case ObjectSerializer.TYPE_MIN: return(Task.FromResult <object>(char.MinValue));

            case ObjectSerializer.TYPE_MAX: return(Task.FromResult <object>(char.MaxValue));

            case ObjectSerializer.TYPE_NULL: return(Task.FromResult <object>(null));

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

            default: throw new Exception("Expected a char value.");
            }
        }
        /// <summary>
        /// Deserializes an object from a binary source.
        /// </summary>
        /// <param name="Reader">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(IDeserializer Reader, uint?DataType, bool Embedded)
        {
            if (!DataType.HasValue)
            {
                DataType = Reader.ReadBits(6);
            }

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

            case ObjectSerializer.TYPE_NULL: return(null);

            default: throw new Exception("Expected a nullable Int32 value.");
            }
        }
Example #4
0
        /// <summary>
        /// Deserializes an object from a binary source.
        /// </summary>
        /// <param name="Reader">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(IDeserializer 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.");
            }
        }
Example #5
0
        /// <summary>
        /// Deserializes an object from a binary source.
        /// </summary>
        /// <param name="Reader">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(IDeserializer 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.");
            }
        }
        /// <summary>
        /// Deserializes an object from a binary source.
        /// </summary>
        /// <param name="Reader">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(IDeserializer Reader, uint?DataType, bool Embedded, bool CheckFieldNames)
        {
            StreamBookmark Bookmark    = Reader.GetBookmark();
            uint?          DataTypeBak = DataType;
            uint           FieldDataType;
            ulong          FieldCode;
            Guid           ObjectId = Embedded ? Guid.Empty : Reader.ReadGuid();
            string         TypeName;
            string         FieldName;
            string         CollectionName;

            if (!Embedded)
            {
                Reader.SkipVariableLengthUInt64();                  // Content length.
            }
            if (!DataType.HasValue)
            {
                DataType = Reader.ReadBits(6);
                if (DataType.Value == ObjectSerializer.TYPE_NULL)
                {
                    return(null);
                }
            }

            switch (DataType.Value)
            {
            case ObjectSerializer.TYPE_OBJECT:
                if (Embedded && Reader.BitOffset > 0 && Reader.ReadBit())
                {
                    ObjectId = Reader.ReadGuid();
                }
                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_DATETIMEOFFSET:
                return(Reader.ReadDateTimeOffset());

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

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

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

            case ObjectSerializer.TYPE_CI_STRING:
                return(new CaseInsensitiveString(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);

            case ObjectSerializer.TYPE_ARRAY:
                throw new Exception("Arrays must be embedded in objects.");

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

            bool Normalized = this.NormalizedNames;

            if (Normalized)
            {
                FieldCode = Reader.ReadVariableLengthUInt64();
                TypeName  = null;
            }
            else
            {
                FieldCode = 0;
                TypeName  = Reader.ReadString();
            }

            if (Embedded)
            {
                if (Normalized)
                {
                    ulong CollectionCode = Reader.ReadVariableLengthUInt64();
                    CollectionName = this.context.GetFieldName(null, CollectionCode);
                }
                else
                {
                    CollectionName = Reader.ReadString();
                }
            }
            else
            {
                CollectionName = Reader.CollectionName;
            }

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

            if (this.returnTypedObjects && !string.IsNullOrEmpty(TypeName))
            {
                Type DesiredType = Types.GetType(TypeName);
                if (DesiredType is null)
                {
                    DesiredType = typeof(GenericObject);
                }

                if (DesiredType != typeof(GenericObject))
                {
                    IObjectSerializer Serializer2 = this.context.GetObjectSerializer(DesiredType);
                    Reader.SetBookmark(Bookmark);
                    return(Serializer2.Deserialize(Reader, DataTypeBak, Embedded));
                }
            }

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

            while (true)
            {
                if (Normalized)
                {
                    FieldCode = Reader.ReadVariableLengthUInt64();
                    if (FieldCode == 0)
                    {
                        break;
                    }

                    if (CheckFieldNames)
                    {
                        FieldName = this.context.GetFieldName(CollectionName, FieldCode);
                    }
                    else
                    {
                        FieldName = CollectionName + "." + FieldCode.ToString();
                    }
                }
                else
                {
                    FieldName = Reader.ReadString();
                    if (string.IsNullOrEmpty(FieldName))
                    {
                        break;
                    }
                }

                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_DATETIMEOFFSET:
                    Properties.AddLast(new KeyValuePair <string, object>(FieldName, Reader.ReadDateTimeOffset()));
                    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_CI_STRING:
                    Properties.AddLast(new KeyValuePair <string, object>(FieldName, new CaseInsensitiveString(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(IDeserializer 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_DATETIMEOFFSET:
                    Elements.Add(Reader.ReadDateTimeOffset());
                    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_CI_STRING:
                    Elements.Add(new CaseInsensitiveString(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());
        }
Example #8
0
        /// <summary>
        /// Deserializes an object from a binary source.
        /// </summary>
        /// <param name="Reader">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 async Task <object> Deserialize(IDeserializer Reader, uint?DataType, bool Embedded, bool CheckFieldNames)
        {
            if (!Reader.ReadBit())
            {
                return(null);
            }

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

            DataType = Reader.ReadBits(6);

            if (DataType == ObjectSerializer.TYPE_MAX)
            {
                Reader.SkipVariableLengthUInt64();
                DataType = Reader.ReadBits(6);
            }

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

                int    Pos            = Reader.Position;
                ulong  TypeCode       = Reader.ReadVariableLengthUInt64();
                ulong  CollectionCode = Reader.ReadVariableLengthUInt64();
                string CollectionName = await this.context.GetFieldName(null, CollectionCode);

                string TypeName = await this.context.GetFieldName(CollectionName, TypeCode);

                IObjectSerializer Serializer;

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

                Reader.Position = Pos;

                Value = await Serializer.Deserialize(Reader, ObjectSerializer.TYPE_OBJECT, true);

                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_DATETIMEOFFSET:
                Value = Reader.ReadDateTimeOffset();
                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_CI_STRING:
                Value = new CaseInsensitiveString(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;

            case ObjectSerializer.TYPE_ARRAY:
                throw new Exception("Arrays must be embedded in objects.");

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

            return(new KeyValuePair <string, object>(Key, Value));
        }