예제 #1
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 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.");
            }
        }
예제 #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 object Deserialize(IDeserializer Reader, uint?DataType, bool Embedded)
        {
            if (!DataType.HasValue)
            {
                DataType = Reader.ReadBits(6);
            }

            switch (DataType.Value)
            {
            case ObjectSerializer.TYPE_GUID: return(Reader.ReadGuid());

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

            case ObjectSerializer.TYPE_NULL: return(null);

            default: throw new Exception("Expected a Guid 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());
        }
예제 #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>
        /// <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));
        }