Пример #1
0
        private void CheckGuidLayoutImpl(MemoryStream ms, Guid value, string expected)
        {
            ms.Position = 0;
            ms.SetLength(0);
            using (var writer = ProtoWriter.Create(out var state, ms, RuntimeTypeModel.Default))
            {
                ProtoWriter.WriteFieldHeader(1, WireType.String, writer, ref state);
                BclHelpers.WriteGuid(value, writer, ref state);
                writer.Close(ref state);
            }

            if (expected != null)
            {
                var hex = BitConverter.ToString(
                    ms.GetBuffer(), 0, (int)ms.Length);
                Assert.Equal(expected, hex);
            }

            ms.Position = 0;
            using (var reader = ProtoReader.Create(out var state, ms, RuntimeTypeModel.Default))
            {
                Assert.Equal(1, reader.ReadFieldHeader(ref state));
                var result = BclHelpers.ReadGuid(reader, ref state);
                Assert.Equal(value, result);
                Assert.Equal(0, reader.ReadFieldHeader(ref state));
            }
        }
 public static List <Student> CreateProtoReaderBuffer(byte[] encoded)
 {
     using (MemoryStream rs = new MemoryStream(encoded))
     {
         ProtoReader.State pr       = ProtoReader.State.Create(rs, null, null);
         List <Student>    students = new List <Student>();
         Student           student  = new Student();
         while (pr.GetPosition() < rs.Length)
         {
             var fieldHeader = pr.ReadFieldHeader();
             if (fieldHeader == 1)
             {
                 student = new Student();
                 students.Add(student);
             }
             if (fieldHeader == 1)
             {
                 student.Id = BclHelpers.ReadGuid(ref pr);
             }
             else if (fieldHeader == 2)
             {
                 student.Name = pr.ReadString();
             }
             else if (fieldHeader == 3)
             {
                 student.AdmissionDate = BclHelpers.ReadDateTime(ref pr);
             }
             else if (fieldHeader == 4)
             {
                 student.Age = pr.ReadInt32();
             }
         }
         return(students);
     }
 }
Пример #3
0
        public object Read(object value, ProtoReader source)

        {
            Helpers.DebugAssert(value == null); // since replaces

            return(BclHelpers.ReadGuid(source));
        }
Пример #4
0
            public void ShouldSerializeGuidValue()
            {
                // Arrange
                var value      = Guid.NewGuid();
                var dataReader = this.CreateDataReader(value);

                // Act
                var reader = new ProtoReader(this.Serialize(dataReader), null, null);

                // Assert
                var readerContext = new ProtoReaderContext(reader);

                readerContext.ReadUntilFieldValue();

                Assert.Equal(value, BclHelpers.ReadGuid(reader));
            }
        public void ShouldSerializeGuidValue()
        {
            // Arrange
            var value      = Guid.NewGuid();
            var dataReader = this.CreateDataReader(value);

            // Act
            this.writer.Serialize(this.stream, dataReader);

            // Assert
            this.stream.Position = 0;

            this.ReadUntilFieldValue();

            Assert.Equal(value, BclHelpers.ReadGuid(this.reader));
        }
Пример #6
0
        private void CheckGuidLayoutImpl(MemoryStream ms, Guid value, string expected)
        {
            ms.Position = 0;
            ms.SetLength(0);
            var state = ProtoWriter.State.Create(ms, RuntimeTypeModel.Default);

            try
            {
                state.WriteFieldHeader(1, WireType.String);
                BclHelpers.WriteGuid(ref state, value);
                state.Close();
            }
            finally
            {
                state.Dispose();
            }

            if (expected != null)
            {
                var hex = BitConverter.ToString(
                    ms.GetBuffer(), 0, (int)ms.Length);
                Assert.Equal(expected, hex);
            }

            ms.Position = 0;
            var readState = ProtoReader.State.Create(ms, RuntimeTypeModel.Default);

            try
            {
                Assert.Equal(1, readState.ReadFieldHeader());
                var result = BclHelpers.ReadGuid(ref readState);
                Assert.Equal(value, result);
                Assert.Equal(0, readState.ReadFieldHeader());
            }
            finally
            {
                readState.Dispose();
            }
        }
Пример #7
0
        private static void ReadRecordValues(ProtoReaderContext context)
        {
            while (context.ReadFieldHeader() != NoneFieldHeader)
            {
                // Backwards compatibility or unnecessary?
                if (context.CurrentFieldHeader > context.Buffers.Length)
                {
                    context.Reader.SkipField();

                    continue;
                }

                var columnIndex = context.CurrentFieldHeader - 1;

                switch (context.Columns[columnIndex].ProtoDataType)
                {
                case ProtoDataType.String:
                    context.Buffers[columnIndex].String = context.Reader.ReadString();
                    context.Buffers[columnIndex].IsNull = false;
                    break;

                case ProtoDataType.DateTime:
                    context.Buffers[columnIndex].DateTime = BclHelpers.ReadDateTime(context.Reader);
                    context.Buffers[columnIndex].IsNull   = false;
                    break;

                case ProtoDataType.Int:
                    context.Buffers[columnIndex].Int    = context.Reader.ReadInt32();
                    context.Buffers[columnIndex].IsNull = false;
                    break;

                case ProtoDataType.Long:
                    context.Buffers[columnIndex].Long   = context.Reader.ReadInt64();
                    context.Buffers[columnIndex].IsNull = false;
                    break;

                case ProtoDataType.Short:
                    context.Buffers[columnIndex].Short  = context.Reader.ReadInt16();
                    context.Buffers[columnIndex].IsNull = false;
                    break;

                case ProtoDataType.Bool:
                    context.Buffers[columnIndex].Bool   = context.Reader.ReadBoolean();
                    context.Buffers[columnIndex].IsNull = false;
                    break;

                case ProtoDataType.Byte:
                    context.Buffers[columnIndex].Byte   = context.Reader.ReadByte();
                    context.Buffers[columnIndex].IsNull = false;
                    break;

                case ProtoDataType.Float:
                    context.Buffers[columnIndex].Float  = context.Reader.ReadSingle();
                    context.Buffers[columnIndex].IsNull = false;
                    break;

                case ProtoDataType.Double:
                    context.Buffers[columnIndex].Double = context.Reader.ReadDouble();
                    context.Buffers[columnIndex].IsNull = false;
                    break;

                case ProtoDataType.Guid:
                    context.Buffers[columnIndex].Guid   = BclHelpers.ReadGuid(context.Reader);
                    context.Buffers[columnIndex].IsNull = false;
                    break;

                case ProtoDataType.Char:
                    context.Buffers[columnIndex].Char   = (char)context.Reader.ReadInt16();
                    context.Buffers[columnIndex].IsNull = false;
                    break;

                case ProtoDataType.Decimal:
                    context.Buffers[columnIndex].Decimal = BclHelpers.ReadDecimal(context.Reader);
                    context.Buffers[columnIndex].IsNull  = false;
                    break;

                case ProtoDataType.ByteArray:
                    context.Buffers[columnIndex].ByteArray = ProtoReader.AppendBytes(null, context.Reader);
                    context.Buffers[columnIndex].IsNull    = false;
                    break;

                case ProtoDataType.CharArray:
                    context.Buffers[columnIndex].CharArray = context.Reader.ReadString().ToCharArray();
                    context.Buffers[columnIndex].IsNull    = false;
                    break;

                case ProtoDataType.TimeSpan:
                    context.Buffers[columnIndex].TimeSpan = BclHelpers.ReadTimeSpan(context.Reader);
                    context.Buffers[columnIndex].IsNull   = false;
                    break;
                }
            }
        }
 public object Read(object value, ProtoReader source)
 {
     return(BclHelpers.ReadGuid(source));
 }
Пример #9
0
        public static DataTable ProtoRead(Stream stream)
        {
            DataTable table = new DataTable();

            object[]      array  = null;
            Func <object> item   = null;
            Func <object> func2  = null;
            Func <object> func3  = null;
            Func <object> func4  = null;
            Func <object> func5  = null;
            Func <object> func6  = null;
            Func <object> func7  = null;
            Func <object> func8  = null;
            Func <object> func9  = null;
            Func <object> func10 = null;
            Func <object> func11 = null;

            using (ProtoReader reader = new ProtoReader(stream, null, null))
            {
                int num;
                List <Func <object> > list = new List <Func <object> >();
                while ((num = reader.ReadFieldHeader()) != 0)
                {
                    SubItemToken token;
                    string       str;
                    string       str2;
                    MappedType   type;
                    Type         type2;
                    switch (num)
                    {
                    case 1:
                    {
                        table.TableName = reader.ReadString();
                        continue;
                    }

                    case 2:
                        str   = null;
                        str2  = string.Empty;
                        type  = ~MappedType.Boolean;
                        token = ProtoReader.StartSubItem(reader);
                        goto Label_00F1;

                    case 3:
                        if (array != null)
                        {
                            goto Label_0382;
                        }
                        array = new object[table.Columns.Count];
                        goto Label_038C;

                    default:
                        goto Label_03F3;
                    }
Label_009B:
                    switch (num)
                    {
                    case 1:
                        str = reader.ReadString();
                        break;

                    case 2:
                        type = (MappedType)reader.ReadInt32();
                        break;

                    case 3:
                        str2 = reader.ReadString();
                        break;

                    default:
                        reader.SkipField();
                        break;
                    }
Label_00F1:
                    if ((num = reader.ReadFieldHeader()) != 0)
                    {
                        goto Label_009B;
                    }
                    switch (type)
                    {
                    case MappedType.Boolean:
                        type2 = typeof(bool);
                        if (item == null)
                        {
                            item = () => reader.ReadBoolean();
                        }
                        list.Add(item);
                        break;

                    case MappedType.Byte:
                        type2 = typeof(byte[]);
                        if (func2 == null)
                        {
                            func2 = () => ReadBytes(reader);
                        }
                        list.Add(func2);
                        break;

                    case MappedType.Double:
                        type2 = typeof(double);
                        if (func3 == null)
                        {
                            func3 = () => reader.ReadDouble();
                        }
                        list.Add(func3);
                        break;

                    case MappedType.Int16:
                        type2 = typeof(short);
                        if (func6 == null)
                        {
                            func6 = () => reader.ReadInt16();
                        }
                        list.Add(func6);
                        break;

                    case MappedType.Int32:
                        type2 = typeof(int);
                        if (func5 == null)
                        {
                            func5 = () => reader.ReadInt32();
                        }
                        list.Add(func5);
                        break;

                    case MappedType.Int64:
                        type2 = typeof(long);
                        if (func4 == null)
                        {
                            func4 = () => reader.ReadInt64();
                        }
                        list.Add(func4);
                        break;

                    case MappedType.String:
                        type2 = typeof(string);
                        if (func8 == null)
                        {
                            func8 = () => reader.ReadString();
                        }
                        list.Add(func8);
                        break;

                    case MappedType.Decimal:
                        type2 = typeof(decimal);
                        if (func7 == null)
                        {
                            func7 = () => BclHelpers.ReadDecimal(reader);
                        }
                        list.Add(func7);
                        break;

                    case MappedType.Guid:
                        type2 = typeof(Guid);
                        if (func9 == null)
                        {
                            func9 = () => BclHelpers.ReadGuid(reader);
                        }
                        list.Add(func9);
                        break;

                    case MappedType.DateTime:
                        type2 = typeof(DateTime);
                        if (func10 == null)
                        {
                            func10 = () => BclHelpers.ReadDateTime(reader);
                        }
                        list.Add(func10);
                        break;

                    case MappedType.TimeSpan:
                        type2 = typeof(TimeSpan);
                        if (func11 == null)
                        {
                            func11 = () => BclHelpers.ReadTimeSpan(reader);
                        }
                        list.Add(func11);
                        break;

                    default:
                        throw new NotSupportedException(type.ToString());
                    }
                    ProtoReader.EndSubItem(token, reader);
                    table.Columns.Add(str, type2);
                    if (!string.IsNullOrEmpty(str2))
                    {
                        table.Columns[str].Caption = str2;
                    }
                    array = null;
                    continue;
Label_0382:
                    Array.Clear(array, 0, array.Length);
Label_038C:
                    token = ProtoReader.StartSubItem(reader);
                    while ((num = reader.ReadFieldHeader()) != 0)
                    {
                        if (num > array.Length)
                        {
                            reader.SkipField();
                        }
                        else
                        {
                            int index = num - 1;
                            array[index] = list[index]();
                        }
                    }
                    ProtoReader.EndSubItem(token, reader);
                    table.Rows.Add(array);
                    continue;
Label_03F3:
                    reader.SkipField();
                }
            }
            return(table);
        }
Пример #10
0
        private void ReadColumn()
        {
            var    token = ProtoReader.StartSubItem(reader);
            int    field;
            string name          = null;
            var    protoDataType = (ProtoDataType)(-1);

            while ((field = reader.ReadFieldHeader()) != 0)
            {
                switch (field)
                {
                case 1:
                    name = reader.ReadString();
                    break;

                case 2:
                    protoDataType = (ProtoDataType)reader.ReadInt32();
                    break;

                default:
                    reader.SkipField();
                    break;
                }
            }

            switch (protoDataType)
            {
            case ProtoDataType.Int:
                colReaders.Add(() => reader.ReadInt32());
                break;

            case ProtoDataType.Short:
                colReaders.Add(() => reader.ReadInt16());
                break;

            case ProtoDataType.Decimal:
                colReaders.Add(() => BclHelpers.ReadDecimal(reader));
                break;

            case ProtoDataType.String:
                colReaders.Add(() => reader.ReadString());
                break;

            case ProtoDataType.Guid:
                colReaders.Add(() => BclHelpers.ReadGuid(reader));
                break;

            case ProtoDataType.DateTime:
                colReaders.Add(() => BclHelpers.ReadDateTime(reader));
                break;

            case ProtoDataType.Bool:
                colReaders.Add(() => reader.ReadBoolean());
                break;

            case ProtoDataType.Byte:
                colReaders.Add(() => reader.ReadByte());
                break;

            case ProtoDataType.Char:
                colReaders.Add(() => (char)reader.ReadInt16());
                break;

            case ProtoDataType.Double:
                colReaders.Add(() => reader.ReadDouble());
                break;

            case ProtoDataType.Float:
                colReaders.Add(() => reader.ReadSingle());
                break;

            case ProtoDataType.Long:
                colReaders.Add(() => reader.ReadInt64());
                break;

            case ProtoDataType.ByteArray:
                colReaders.Add(() => ProtoReader.AppendBytes(null, reader));
                break;

            case ProtoDataType.CharArray:
                colReaders.Add(() => reader.ReadString().ToCharArray());
                break;

            case ProtoDataType.TimeSpan:
                colReaders.Add(() => BclHelpers.ReadTimeSpan(reader));
                break;

            default:
                throw new NotSupportedException(protoDataType.ToString());
            }

            ProtoReader.EndSubItem(token, reader);
            dataTable.Columns.Add(name, ConvertProtoDataType.ToClrType(protoDataType));
        }
        private void ReadRecordValues()
        {
            int fieldHeader;

            while ((fieldHeader = this.protoReader.ReadFieldHeader()) != FieldHeaders.None)
            {
                var columnIndex = fieldHeader - 1;

                switch (this.columns[columnIndex].ProtoBufDataType)
                {
                case ProtoBufDataType.Bool:
                    this.buffers[columnIndex].Bool   = this.protoReader.ReadBoolean();
                    this.buffers[columnIndex].IsNull = false;
                    break;

                case ProtoBufDataType.Byte:
                    this.buffers[columnIndex].Byte   = this.protoReader.ReadByte();
                    this.buffers[columnIndex].IsNull = false;
                    break;

                case ProtoBufDataType.ByteArray:
                    this.buffers[columnIndex].ByteArray = ProtoReader.AppendBytes(null, this.protoReader);
                    this.buffers[columnIndex].IsNull    = false;
                    break;

                case ProtoBufDataType.Char:
                    this.buffers[columnIndex].Char   = (char)this.protoReader.ReadInt16();
                    this.buffers[columnIndex].IsNull = false;
                    break;

                case ProtoBufDataType.CharArray:
                    this.buffers[columnIndex].CharArray = this.protoReader.ReadString().ToCharArray();
                    this.buffers[columnIndex].IsNull    = false;
                    break;

                case ProtoBufDataType.DateTime:
                    this.buffers[columnIndex].DateTime = BclHelpers.ReadDateTime(this.protoReader);
                    this.buffers[columnIndex].IsNull   = false;
                    break;

                case ProtoBufDataType.Decimal:
                    this.buffers[columnIndex].Decimal = BclHelpers.ReadDecimal(this.protoReader);
                    this.buffers[columnIndex].IsNull  = false;
                    break;

                case ProtoBufDataType.Double:
                    this.buffers[columnIndex].Double = this.protoReader.ReadDouble();
                    this.buffers[columnIndex].IsNull = false;
                    break;

                case ProtoBufDataType.Float:
                    this.buffers[columnIndex].Float  = this.protoReader.ReadSingle();
                    this.buffers[columnIndex].IsNull = false;
                    break;

                case ProtoBufDataType.Guid:
                    this.buffers[columnIndex].Guid   = BclHelpers.ReadGuid(this.protoReader);
                    this.buffers[columnIndex].IsNull = false;
                    break;

                case ProtoBufDataType.Int:
                    this.buffers[columnIndex].Int    = this.protoReader.ReadInt32();
                    this.buffers[columnIndex].IsNull = false;
                    break;

                case ProtoBufDataType.Long:
                    this.buffers[columnIndex].Long   = this.protoReader.ReadInt64();
                    this.buffers[columnIndex].IsNull = false;
                    break;

                case ProtoBufDataType.Short:
                    this.buffers[columnIndex].Short  = this.protoReader.ReadInt16();
                    this.buffers[columnIndex].IsNull = false;
                    break;

                case ProtoBufDataType.String:
                    this.buffers[columnIndex].String = this.protoReader.ReadString();
                    this.buffers[columnIndex].IsNull = false;
                    break;

                case ProtoBufDataType.TimeSpan:
                    this.buffers[columnIndex].TimeSpan = BclHelpers.ReadTimeSpan(this.protoReader);
                    this.buffers[columnIndex].IsNull   = false;
                    break;
                }
            }
        }
Пример #12
0
        internal bool TryDeserializeAuxiliaryType(ProtoReader reader, DataFormat format, int tag, Type type, ref object value, bool skipOtherFields, bool asListItem, bool autoCreate, bool insideList)
        {
            if (type == null)
            {
                throw new ArgumentNullException("type");
            }
            Type          type2    = null;
            ProtoTypeCode typeCode = Helpers.GetTypeCode(type);
            int           modelKey;
            WireType      wireType = GetWireType(typeCode, format, ref type, out modelKey);
            bool          flag     = false;

            if (wireType == WireType.None)
            {
                type2 = GetListItemType(this, type);
                if (type2 == null && type.IsArray && type.GetArrayRank() == 1 && type != typeof(byte[]))
                {
                    type2 = type.GetElementType();
                }
                if (type2 != null)
                {
                    if (insideList)
                    {
                        throw CreateNestedListsNotSupported();
                    }
                    flag = TryDeserializeList(this, reader, format, tag, type, type2, ref value);
                    if (!flag && autoCreate)
                    {
                        value = CreateListInstance(type, type2);
                    }
                    return(flag);
                }
                ThrowUnexpectedType(type);
            }
            while (!flag || !asListItem)
            {
                int num = reader.ReadFieldHeader();
                if (num <= 0)
                {
                    break;
                }
                if (num != tag)
                {
                    if (skipOtherFields)
                    {
                        reader.SkipField();
                        continue;
                    }
                    throw ProtoReader.AddErrorData(new InvalidOperationException("Expected field " + tag.ToString() + ", but found " + num.ToString()), reader);
                }
                flag = true;
                reader.Hint(wireType);
                if (modelKey >= 0)
                {
                    if ((uint)(wireType - 2) <= 1u)
                    {
                        SubItemToken token = ProtoReader.StartSubItem(reader);
                        value = Deserialize(modelKey, value, reader);
                        ProtoReader.EndSubItem(token, reader);
                    }
                    else
                    {
                        value = Deserialize(modelKey, value, reader);
                    }
                    continue;
                }
                switch (typeCode)
                {
                case ProtoTypeCode.Int16:
                    value = reader.ReadInt16();
                    break;

                case ProtoTypeCode.Int32:
                    value = reader.ReadInt32();
                    break;

                case ProtoTypeCode.Int64:
                    value = reader.ReadInt64();
                    break;

                case ProtoTypeCode.UInt16:
                    value = reader.ReadUInt16();
                    break;

                case ProtoTypeCode.UInt32:
                    value = reader.ReadUInt32();
                    break;

                case ProtoTypeCode.UInt64:
                    value = reader.ReadUInt64();
                    break;

                case ProtoTypeCode.Boolean:
                    value = reader.ReadBoolean();
                    break;

                case ProtoTypeCode.SByte:
                    value = reader.ReadSByte();
                    break;

                case ProtoTypeCode.Byte:
                    value = reader.ReadByte();
                    break;

                case ProtoTypeCode.Char:
                    value = (char)reader.ReadUInt16();
                    break;

                case ProtoTypeCode.Double:
                    value = reader.ReadDouble();
                    break;

                case ProtoTypeCode.Single:
                    value = reader.ReadSingle();
                    break;

                case ProtoTypeCode.DateTime:
                    value = BclHelpers.ReadDateTime(reader);
                    break;

                case ProtoTypeCode.Decimal:
                    value = BclHelpers.ReadDecimal(reader);
                    break;

                case ProtoTypeCode.String:
                    value = reader.ReadString();
                    break;

                case ProtoTypeCode.ByteArray:
                    value = ProtoReader.AppendBytes((byte[])value, reader);
                    break;

                case ProtoTypeCode.TimeSpan:
                    value = BclHelpers.ReadTimeSpan(reader);
                    break;

                case ProtoTypeCode.Guid:
                    value = BclHelpers.ReadGuid(reader);
                    break;

                case ProtoTypeCode.Uri:
                    value = new Uri(reader.ReadString());
                    break;
                }
            }
            if (((!flag && !asListItem) & autoCreate) && type != typeof(string))
            {
                value = Activator.CreateInstance(type);
            }
            return(flag);
        }
Пример #13
0
        internal bool TryDeserializeAuxiliaryType(ProtoReader reader, DataFormat format, int tag, Type type, ref object value, bool skipOtherFields, bool asListItem, bool autoCreate, bool insideList)
        {
            if (type == null)
            {
                throw new ArgumentNullException("type");
            }
            ProtoTypeCode typecode = Helpers.GetTypeCode(type);
            int           modelKey;
            WireType      wiretype = this.GetWireType(typecode, format, ref type, out modelKey);
            bool          found    = false;

            if (wiretype == WireType.None)
            {
                Type itemType = TypeModel.GetListItemType(this, type);
                if (itemType == null && type.IsArray && type.GetArrayRank() == 1 && type != typeof(byte[]))
                {
                    itemType = type.GetElementType();
                }
                if (itemType != null)
                {
                    if (insideList)
                    {
                        throw TypeModel.CreateNestedListsNotSupported();
                    }
                    found = this.TryDeserializeList(this, reader, format, tag, type, itemType, ref value);
                    if (!found && autoCreate)
                    {
                        value = TypeModel.CreateListInstance(type, itemType);
                    }
                    return(found);
                }
                else
                {
                    TypeModel.ThrowUnexpectedType(type);
                }
            }
            while (!found || !asListItem)
            {
                int fieldNumber = reader.ReadFieldHeader();
                if (fieldNumber <= 0)
                {
                    break;
                }
                if (fieldNumber != tag)
                {
                    if (!skipOtherFields)
                    {
                        throw ProtoReader.AddErrorData(new InvalidOperationException("Expected field " + tag.ToString() + ", but found " + fieldNumber.ToString()), reader);
                    }
                    reader.SkipField();
                }
                else
                {
                    found = true;
                    reader.Hint(wiretype);
                    if (modelKey >= 0)
                    {
                        switch (wiretype)
                        {
                        case WireType.String:
                        case WireType.StartGroup:
                        {
                            SubItemToken token = ProtoReader.StartSubItem(reader);
                            value = this.Deserialize(modelKey, value, reader);
                            ProtoReader.EndSubItem(token, reader);
                            break;
                        }

                        default:
                            value = this.Deserialize(modelKey, value, reader);
                            break;
                        }
                    }
                    else
                    {
                        ProtoTypeCode protoTypeCode = typecode;
                        switch (protoTypeCode)
                        {
                        case ProtoTypeCode.Boolean:
                            value = reader.ReadBoolean();
                            break;

                        case ProtoTypeCode.Char:
                            value = (char)reader.ReadUInt16();
                            break;

                        case ProtoTypeCode.SByte:
                            value = reader.ReadSByte();
                            break;

                        case ProtoTypeCode.Byte:
                            value = reader.ReadByte();
                            break;

                        case ProtoTypeCode.Int16:
                            value = reader.ReadInt16();
                            break;

                        case ProtoTypeCode.UInt16:
                            value = reader.ReadUInt16();
                            break;

                        case ProtoTypeCode.Int32:
                            value = reader.ReadInt32();
                            break;

                        case ProtoTypeCode.UInt32:
                            value = reader.ReadUInt32();
                            break;

                        case ProtoTypeCode.Int64:
                            value = reader.ReadInt64();
                            break;

                        case ProtoTypeCode.UInt64:
                            value = reader.ReadUInt64();
                            break;

                        case ProtoTypeCode.Single:
                            value = reader.ReadSingle();
                            break;

                        case ProtoTypeCode.Double:
                            value = reader.ReadDouble();
                            break;

                        case ProtoTypeCode.Decimal:
                            value = BclHelpers.ReadDecimal(reader);
                            break;

                        case ProtoTypeCode.DateTime:
                            value = BclHelpers.ReadDateTime(reader);
                            break;

                        case (ProtoTypeCode)17:
                            break;

                        case ProtoTypeCode.String:
                            value = reader.ReadString();
                            break;

                        default:
                            switch (protoTypeCode)
                            {
                            case ProtoTypeCode.TimeSpan:
                                value = BclHelpers.ReadTimeSpan(reader);
                                break;

                            case ProtoTypeCode.ByteArray:
                                value = ProtoReader.AppendBytes((byte[])value, reader);
                                break;

                            case ProtoTypeCode.Guid:
                                value = BclHelpers.ReadGuid(reader);
                                break;

                            case ProtoTypeCode.Uri:
                                value = new Uri(reader.ReadString());
                                break;
                            }
                            break;
                        }
                    }
                }
            }
            if (!found && !asListItem && autoCreate && type != typeof(string))
            {
                value = Activator.CreateInstance(type);
            }
            return(found);
        }
Пример #14
0
        private void WriteFieldsToTable(IWritableReactiveTable table,
                                        Dictionary <int, string> fieldIdsToColumns,
                                        ProtoReader reader,
                                        int rowId)
        {
            int fieldId;

            while ((fieldId = reader.ReadFieldHeader()) != 0)
            {
                var columnId = fieldIdsToColumns[fieldId];
                var column   = table.GetColumnByName(columnId);

                if (column.Type == typeof(int))
                {
                    table.SetValue(columnId, rowId, reader.ReadInt32());
                }
                else if (column.Type == typeof(short))
                {
                    table.SetValue(columnId, rowId, reader.ReadInt16());
                }
                else if (column.Type == typeof(string))
                {
                    var value = reader.ReadString();
                    table.SetValue(columnId, rowId, value);
                }
                else if (column.Type == typeof(bool))
                {
                    table.SetValue(columnId, rowId, reader.ReadBoolean());
                }
                else if (column.Type == typeof(double))
                {
                    table.SetValue(columnId, rowId, reader.ReadDouble());
                }
                else if (column.Type == typeof(long))
                {
                    table.SetValue(columnId, rowId, reader.ReadInt64());
                }
                else if (column.Type == typeof(decimal))
                {
                    table.SetValue(columnId, rowId, BclHelpers.ReadDecimal(reader));
                }
                else if (column.Type == typeof(DateTime))
                {
                    table.SetValue(columnId, rowId, BclHelpers.ReadDateTime(reader));
                }
                else if (column.Type == typeof(TimeSpan))
                {
                    table.SetValue(columnId, rowId, BclHelpers.ReadTimeSpan(reader));
                }
                else if (column.Type == typeof(Guid))
                {
                    table.SetValue(columnId, rowId, BclHelpers.ReadGuid(reader));
                }
                else if (column.Type == typeof(byte))
                {
                    table.SetValue(columnId, rowId, reader.ReadByte());
                }
                else if (column.Type == typeof(char))
                {
                    table.SetValue(columnId, rowId, (char)reader.ReadInt16());
                }
                else if (column.Type == typeof(float))
                {
                    table.SetValue(columnId, rowId, reader.ReadSingle());
                }
            }
        }
Пример #15
0
        static DataTable ProtoRead(Stream stream)
        {
            DataTable table = new DataTable();

            object[] values = null;
            using (ProtoReader reader = new ProtoReader(stream, null, null))
            {
                int field;
                List <Func <object> > colReaders = new List <Func <object> >();
                SubItemToken          token;
                while ((field = reader.ReadFieldHeader()) != 0)
                {
                    switch (field)
                    {
                    case 1:
                        table.TableName = reader.ReadString();
                        break;

                    case 2:
                        string     name       = null;
                        MappedType mappedType = (MappedType)(-1);
                        token = ProtoReader.StartSubItem(reader);
                        while ((field = reader.ReadFieldHeader()) != 0)
                        {
                            switch (field)
                            {
                            case 1:
                                name = reader.ReadString();
                                break;

                            case 2:
                                mappedType = (MappedType)reader.ReadInt32();
                                break;

                            default:
                                reader.SkipField();
                                break;
                            }
                        }
                        Type type;
                        switch (mappedType)
                        {
                        case MappedType.Int32:
                            type = typeof(int);
                            colReaders.Add(() => reader.ReadInt32());
                            break;

                        case MappedType.Int16:
                            type = typeof(short);
                            colReaders.Add(() => reader.ReadInt16());
                            break;

                        case MappedType.Decimal:
                            type = typeof(decimal);
                            colReaders.Add(() => BclHelpers.ReadDecimal(reader));
                            break;

                        case MappedType.String:
                            type = typeof(string);
                            colReaders.Add(() => reader.ReadString());
                            break;

                        case MappedType.Guid:
                            type = typeof(Guid);
                            colReaders.Add(() => BclHelpers.ReadGuid(reader));
                            break;

                        case MappedType.DateTime:
                            type = typeof(DateTime);
                            colReaders.Add(() => BclHelpers.ReadDateTime(reader));
                            break;

                        default:
                            throw new NotSupportedException(mappedType.ToString());
                        }
                        ProtoReader.EndSubItem(token, reader);
                        table.Columns.Add(name, type);
                        values = null;
                        break;

                    case 3:
                        if (values == null)
                        {
                            values = new object[table.Columns.Count];
                        }
                        else
                        {
                            Array.Clear(values, 0, values.Length);
                        }
                        token = ProtoReader.StartSubItem(reader);
                        while ((field = reader.ReadFieldHeader()) != 0)
                        {
                            if (field > values.Length)
                            {
                                reader.SkipField();
                            }
                            else
                            {
                                int i = field - 1;
                                values[i] = colReaders[i]();
                            }
                        }
                        ProtoReader.EndSubItem(token, reader);
                        table.Rows.Add(values);
                        break;

                    default:
                        reader.SkipField();
                        break;
                    }
                }
            }
            return(table);
        }
Пример #16
0
 public object Read(ref ProtoReader.State state, object value)
 {
     Debug.Assert(value == null); // since replaces
     return(BclHelpers.ReadGuid(ref state));
 }
Пример #17
0
        /// <summary>
        /// This is the more "complete" version of Deserialize, which handles single instances of mapped types.
        /// The value is read as a complete field, including field-header and (for sub-objects) a
        /// length-prefix..kmc
        ///
        /// In addition to that, this provides support for:
        ///  - basic values; individual int / string / Guid / etc
        ///  - IList sets of any type handled by TryDeserializeAuxiliaryType
        /// </summary>
        private bool TryDeserializeAuxiliaryType(ProtoReader reader, DataFormat format, int tag, Type type, ref object value, bool skipOtherFields, bool asListItem)
        {
            if (type == null)
            {
                throw new ArgumentNullException("type");
            }
            Type     itemType = null;
            TypeCode typecode = Type.GetTypeCode(type);
            int      modelKey;
            WireType wiretype = GetWireType(typecode, format, ref type, out modelKey);

            bool found = false;

            if (wiretype == WireType.None)
            {
                itemType = GetListItemType(type);

                if (itemType != null)
                {
                    return(TryDeserializeList(reader, format, tag, type, itemType, ref value));
                }
                // otherwise, not a happy bunny...
                ThrowUnexpectedType(type);
            }

            // to treat correctly, should read all values

            while (true)
            {
                // for convenience (re complex exit conditions), additional exit test here:
                // if we've got the value, are only looking for one, and we aren't a list - then exit
                if (found && asListItem)
                {
                    break;
                }

                // read the next item
                int fieldNumber = reader.ReadFieldHeader();
                if (fieldNumber <= 0)
                {
                    break;
                }
                if (fieldNumber != tag)
                {
                    if (skipOtherFields)
                    {
                        reader.SkipField();
                        continue;
                    }
                    throw ProtoReader.AddErrorData(new InvalidOperationException(
                                                       "Expected field " + tag + ", but found " + fieldNumber), reader);
                }
                found = true;
                reader.Hint(wiretype); // handle signed data etc

                if (modelKey >= 0)
                {
                    switch (wiretype)
                    {
                    case WireType.String:
                    case WireType.StartGroup:
                        SubItemToken token = ProtoReader.StartSubItem(reader);
                        value = Deserialize(modelKey, value, reader);
                        ProtoReader.EndSubItem(token, reader);
                        continue;

                    default:
                        value = Deserialize(modelKey, value, reader);
                        continue;
                    }
                }
                switch (typecode)
                {
                case TypeCode.Int16: value = reader.ReadInt16(); continue;

                case TypeCode.Int32: value = reader.ReadInt32(); continue;

                case TypeCode.Int64: value = reader.ReadInt64(); continue;

                case TypeCode.UInt16: value = reader.ReadUInt16(); continue;

                case TypeCode.UInt32: value = reader.ReadUInt32(); continue;

                case TypeCode.UInt64: value = reader.ReadUInt64(); continue;

                case TypeCode.Boolean: value = reader.ReadBoolean(); continue;

                case TypeCode.SByte: value = reader.ReadSByte(); continue;

                case TypeCode.Byte: value = reader.ReadByte(); continue;

                case TypeCode.Char: value = (char)reader.ReadUInt16(); continue;

                case TypeCode.Double: value = reader.ReadDouble(); continue;

                case TypeCode.Single: value = reader.ReadSingle(); continue;

                case TypeCode.DateTime: value = BclHelpers.ReadDateTime(reader); continue;

                case TypeCode.Decimal: BclHelpers.ReadDecimal(reader); continue;

                case TypeCode.String: value = reader.ReadString(); continue;
                }
                if (type == typeof(byte[]))
                {
                    value = ProtoReader.AppendBytes((byte[])value, reader); continue;
                }
                if (type == typeof(TimeSpan))
                {
                    value = BclHelpers.ReadTimeSpan(reader); continue;
                }
                if (type == typeof(Guid))
                {
                    value = BclHelpers.ReadGuid(reader); continue;
                }
                if (type == typeof(Uri))
                {
                    value = new Uri(reader.ReadString()); continue;
                }
            }
            if (!found && !asListItem)
            {
                value = Activator.CreateInstance(type);
            }
            return(found);
        }