예제 #1
0
            public void ShouldNotSerializeExpressionColumn()
            {
                var isRunningOnMono = Type.GetType("Mono.Runtime") != null;

                if (!isRunningOnMono)
                {
                    // Arrange
                    var columnName = "foo";
                    var dataTable  = new DataTable();

                    dataTable.Columns.Add(columnName, typeof(int));

                    dataTable.Rows.Add(1);

                    var dataReader = dataTable.CreateDataReader();

                    dataReader.GetSchemaTable().Rows[0]["Expression"] = true;

                    var options = new ProtoDataWriterOptions()
                    {
                        IncludeComputedColumns = false
                    };

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

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

                    readerContext.ReadExpectedFieldHeader(ResultFieldHeader);
                    readerContext.StartSubItem();

                    Assert.Equal(RecordFieldHeader, reader.ReadFieldHeader());
                }
            }
예제 #2
0
    public object Deserialize(Stream source, string className)
    {
        CLS_Type_Class sClass = m_clsEnv.GetTypeByKeywordQuiet(className) as CLS_Type_Class;

        if (sClass == null)
        {
            throw new NotImplementedException("未实现类型: " + className);
        }

        if (!sClass.compiled)
        {
            RuntimeCompilerClass(className);
        }

        CLS_Content.Value retVal    = (sClass.function as SType).New(m_clsContent, m_emptyParams);
        SInstance         sInstance = (SInstance)retVal.value;

        ProtoReader reader = null;

        try
        {
            reader = ProtoReader.Create(source, null, null, ProtoReader.TO_EOF);
            ReadSInstance(reader, sInstance, m_clsEnv);
            reader.CheckFullyConsumed();
            return(sInstance);
        }
        finally
        {
            ProtoReader.Recycle(reader);
        }
    }
예제 #3
0
            public void ShouldSerializeSuccessiveResultColumns()
            {
                // Arrange
                var dataSet = new DataSet();

                dataSet.Tables.Add(new DataTable());
                dataSet.Tables.Add(new DataTable());

                dataSet.Tables[0].Columns.Add("foo", typeof(int));
                dataSet.Tables[0].Rows.Add(1);

                var columnName = "bar";

                dataSet.Tables[1].Columns.Add(columnName, typeof(int));
                dataSet.Tables[1].Rows.Add(1);

                var dataReader = dataSet.CreateDataReader();

                var stream = new ProtoDataStream(dataReader);

                // Act
                var reader = ProtoReader.Create(this.CopyStream(stream), null, null);

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

                readerContext.ReadUntilResultEnd();
                readerContext.ReadUntilColumnName();

                Assert.Equal(columnName, reader.ReadString());
            }
예제 #4
0
 private static ProtoReader ReadProto(byte[] payload)
 {
     if (payload == null || payload.Length == 0)
     {
         return(null);
     }
     try
     {
         var ms = new MemoryStream(payload);
         using (var reader = ProtoReader.Create(ms, null, null))
         {
             int field = reader.ReadFieldHeader();
             while (field > 0)
             {
                 reader.SkipField();
                 field = reader.ReadFieldHeader();
             }
         }
         ms.Position = 0;
         return(ProtoReader.Create(ms, null, null));
     }
     catch
     {
         return(null);
     }
 }
        public void Setup()
        {
            _data = File.ReadAllBytes("test.bin");
            var model = RuntimeTypeModel.Create();

            model.Add(typeof(Database), true);
            model.Add(typeof(Order), true);
            model.Add(typeof(OrderLine), true);
            model.CompileInPlace();
            _cip = model;
            _c   = model.Compile();
#if WRITE_DLL
            _dll = model.Compile("MySerializer", "DalSerializer.dll");
            Console.WriteLine("Serializer written to " + Directory.GetCurrentDirectory());
#endif
#if NEW_API
            _auto = RuntimeTypeModel.CreateForAssembly <Database>();
#endif

#pragma warning disable CS0618
            using var reader = ProtoReader.Create(Exposable(_data), model);
            _database        = (Database)model.Deserialize(reader, null, typeof(Database));
            _googleModel     = protoc.Database.Parser.ParseFrom(_data);
#pragma warning restore CS0618
        }
예제 #6
0
        static public JObject ProtoToJson(byte[] protodata)
        {
            Stream      stream  = new MemoryStream(protodata);
            ProtoReader s       = ProtoReader.Create(stream, stream.Length);
            Regex       regex   = new Regex("[\u0000-\u0009\u000e-\u001f]");
            JObject     jObject = new JObject();

            for (; s.Position < protodata.Length;)
            {
                int    fieldNumber = s.ReadFieldHeader();
                string name        = "field_" + fieldNumber;

                switch (s.WireType)
                {
                case WireType.Variant:
                case WireType.Fixed32:
                case WireType.Fixed64:
                case WireType.SignedVariant:
                {
                    string data = "0x" + s.ReadInt64().ToString("X");
                    jObject.AddEx(name, data);
                    break;
                }

                case WireType.String:
                {
                    byte[] b       = s.ReadStringBytes();
                    string readstr = "";
                    if (b != null)
                    {
                        readstr = Encoding.UTF8.GetString(b);
                    }

                    if (b != null && regex.IsMatch(readstr))
                    {
                        try
                        {
                            jObject.AddEx(name, ProtoToJson(b));
                        }
                        catch
                        {
                            jObject.AddEx(name, readstr);
                        }
                    }
                    else
                    {
                        jObject.AddEx(name, readstr);
                    }
                    break;
                }

                case WireType.StartGroup:
                //@string.Append('\t', depth + 1); @string.Append("Start Group\n"); break;
                case WireType.EndGroup:
                //@string.Append('\t', depth + 1); @string.Append("End Group\n"); break;
                default: break;
                }
            }
            return(jObject);
        }
        public void ShouldSerializeMultipleResults()
        {
            // Arrange
            var dataSet = new DataSet();

            dataSet.Tables.Add(new DataTable());
            dataSet.Tables.Add(new DataTable());

            dataSet.Tables[0].Columns.Add("foo", typeof(int));
            dataSet.Tables[0].Rows.Add(1);

            dataSet.Tables[1].Columns.Add("bar", typeof(int));
            dataSet.Tables[1].Rows.Add(1);

            var dataReader = dataSet.CreateDataReader();

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

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

            readerContext.ReadUntilResultEnd();

            Assert.Equal(ResultFieldHeader, reader.ReadFieldHeader());
        }
예제 #8
0
            public void ShouldSerializeExpressionColumn()
            {
                // Arrange
                var columnName = "foo";
                var dataTable  = new DataTable();

                dataTable.Columns.Add(columnName, typeof(int));

                dataTable.Rows.Add(1);

                var dataReader = dataTable.CreateDataReader();

                dataReader.GetSchemaTable().Rows[0]["Expression"] = true;

                var options = new ProtoDataWriterOptions()
                {
                    IncludeComputedColumns = true
                };

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

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

                readerContext.ReadUntilColumnName();

                Assert.Equal(columnName, reader.ReadString());
            }
예제 #9
0
        private protogen.Database MemoryStream_Legacy(TypeModel model)
        {
#pragma warning disable CS0618
            using var reader = ProtoReader.Create(ExposableData(), model ?? Throw());
            return((protogen.Database)model.Deserialize(reader, null, typeof(protogen.Database)));

#pragma warning restore CS0618
        }
예제 #10
0
 public ProtoReader ReadROM(out ProtoReader.State state)
 {
     if (!_ros.IsSingleSegment)
     {
         throw new InvalidOperationException("Expected single segment");
     }
     return(ProtoReader.Create(out state, _ros.First, Model));
 }
예제 #11
0
        public ProtoReader GetReader()
        {
            var ms = new MemoryStream(Data, 0, Data.Length, false);

#pragma warning disable CS0618
            return(ProtoReader.Create(ms, null, null));

#pragma warning restore CS0618
        }
예제 #12
0
        /// <summary>
        /// Initializes a new instance of the <see cref="ProtoDataReader"/> class.
        /// </summary>
        /// <param name="stream">A <see cref="Stream"/> that represents the stream the <see cref="ProtoDataReader"/> reads from.</param>
        public ProtoDataReader(Stream stream)
        {
            if (stream == null)
            {
                throw new ArgumentNullException("stream");
            }

            this.stream  = stream;
            this.reader  = ProtoReader.Create(stream, null, null);
            this.context = new ProtoReaderContext(this.reader);

            ResultReader.ReadResult(this.context);
        }
예제 #13
0
        public void Execute(byte boolvalue, bool expected)
        {
            byte[] buffer = { 8, boolvalue };
            using (var ms = new MemoryStream(buffer))
            {
                using (var protoReader = ProtoReader.Create(ms, null))
                {
                    var fieldNumber = protoReader.ReadFieldHeader();
                    var value       = protoReader.ReadBoolean();

                    Assert.Equal(expected, value);
                }
            }
        }
예제 #14
0
        private T Deserialize <T>(byte[] payload, int offset, int count)
        {
#if PLAT_PBN_NOSPAN
            using var ms     = new MemoryStream(payload, offset, count);
            using var reader = ProtoReader.Create(ms, _model);
            return((T)_model.Deserialize(reader, null, typeof(T)));
#else
            var range = new ReadOnlyMemory <byte>(payload, offset, count);
            using (var reader = ProtoReader.Create(out var state, range, _model))
            {
                return((T)_model.Deserialize(reader, ref state, null, typeof(T)));
            }
#endif
        }
예제 #15
0
            public void ShouldSerializeDateTimeColumnType()
            {
                // Arrange
                var dataReader = this.CreateDataReader(new DateTime(1969, 10, 29, 22, 30, 0));

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

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

                readerContext.ReadUntilColumnType();

                Assert.Equal(2, reader.ReadInt32());
            }
예제 #16
0
            public void ShouldSerializeCharArrayColumnType()
            {
                // Arrange
                var dataReader = this.CreateDataReader(new[] { 'f', 'o', 'o' });

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

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

                readerContext.ReadUntilColumnType();

                Assert.Equal(14, reader.ReadInt32());
            }
        /// <summary>
        /// Deserializes an object from a payload
        /// </summary>
        protected override T Deserialize <T>(byte[] payload)
        {
#if PLAT_NOSPAN
            using (var ms = new MemoryStream(payload))
                using (var reader = ProtoReader.Create(ms, _model))
                {
                    return((T)_model.Deserialize(reader, null, typeof(T)));
                }
#else
            using (var reader = ProtoReader.Create(out var state, payload, _model))
            {
                return((T)_model.Deserialize(reader, ref state, null, typeof(T)));
            }
#endif
        }
예제 #18
0
            public void ShouldSerializeDecimalColumnType()
            {
                // Arrange
                var dataReader = this.CreateDataReader(42m);

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

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

                readerContext.ReadUntilColumnType();

                Assert.Equal(12, reader.ReadInt32());
            }
예제 #19
0
            public void ShouldSerializeTimeSpanColumnType()
            {
                // Arrange
                var dataReader = this.CreateDataReader(TimeSpan.FromTicks(1));

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

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

                readerContext.ReadUntilColumnType();

                Assert.Equal(15, reader.ReadInt32());
            }
예제 #20
0
            public void ShouldSerializeDecimalValue()
            {
                // Arrange
                var value      = 42m;
                var dataReader = this.CreateDataReader(value);

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

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

                readerContext.ReadUntilFieldValue();

                Assert.Equal(value, BclHelpers.ReadDecimal(reader));
            }
예제 #21
0
            public void ShouldSerializeInt32Value()
            {
                // Arrange
                var value      = 42;
                var dataReader = this.CreateDataReader(value);

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

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

                readerContext.ReadUntilFieldValue();

                Assert.Equal(value, reader.ReadInt32());
            }
예제 #22
0
            public void ShouldSerializeCharArrayValue()
            {
                // Arrange
                var value      = new[] { 'f', 'o', 'o' };
                var dataReader = this.CreateDataReader(value);

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

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

                readerContext.ReadUntilFieldValue();

                Assert.Equal(value, reader.ReadString().ToCharArray());
            }
예제 #23
0
            public void ShouldSerializeByteArrayValue()
            {
                // Arrange
                var value      = new[] { (byte)42, (byte)42 };
                var dataReader = this.CreateDataReader(value);

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

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

                readerContext.ReadUntilFieldValue();

                Assert.Equal(value, ProtoReader.AppendBytes(null, reader));
            }
예제 #24
0
            public void ShouldSerializeDateTimeValue()
            {
                // Arrange
                var value      = new DateTime(1969, 10, 29, 22, 30, 0);
                var dataReader = this.CreateDataReader(value);

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

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

                readerContext.ReadUntilFieldValue();

                Assert.Equal(value, BclHelpers.ReadDateTime(reader));
            }
예제 #25
0
            public void ShouldSerializeTimeSpanValue()
            {
                // Arrange
                var value      = TimeSpan.FromTicks(1);
                var dataReader = this.CreateDataReader(value);

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

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

                readerContext.ReadUntilFieldValue();

                Assert.Equal(value, BclHelpers.ReadTimeSpan(reader));
            }
예제 #26
0
            public void ShouldNotSerializeIfValueIsNull()
            {
                // Arrange
                string value      = null;
                var    dataReader = this.CreateDataReader(value);

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

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

                readerContext.ReadUntilField();
                readerContext.ReadExpectedFieldHeader(3);

                Assert.Equal(0, reader.ReadFieldHeader());
            }
예제 #27
0
        /// <summary>
        /// All this does is call GetExtendedValuesTyped with the correct type for "instance";
        /// this ensures that we don't get issues with subclasses declaring conflicting types -
        /// the caller must respect the fields defined for the type they pass in.
        /// </summary>
        internal static IEnumerable GetExtendedValues(TypeModel model, Type type, IExtensible instance, int tag, DataFormat format, bool singleton, bool allowDefinedTag)
        {
            if (instance == null)
            {
                throw new ArgumentNullException(nameof(instance));
            }
            if (tag <= 0)
            {
                throw new ArgumentOutOfRangeException(nameof(tag));
            }
            IExtension extn = instance.GetExtensionObject(false);

            if (extn == null)
            {
                yield break;
            }

            Stream      stream = extn.BeginQuery();
            object      value  = null;
            ProtoReader reader = null;

            try
            {
                SerializationContext ctx = new SerializationContext();
                reader = ProtoReader.Create(stream, model, ctx, ProtoReader.TO_EOF);
                while (model.TryDeserializeAuxiliaryType(reader, format, tag, type, ref value, true, true, false, false, null) && value != null)
                {
                    if (!singleton)
                    {
                        yield return(value);

                        value = null; // fresh item each time
                    }
                }
                if (singleton && value != null)
                {
                    yield return(value);
                }
            }
            finally
            {
                ProtoReader.Recycle(reader);
                extn.EndQuery(stream);
            }
        }
예제 #28
0
            public void ShouldSerializeField()
            {
                // Arrange
                var value      = "foo";
                var dataReader = this.CreateDataReader(value);

                var stream = new ProtoDataStream(dataReader);

                // Act
                var reader = ProtoReader.Create(this.CopyStream(stream), null, null);

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

                readerContext.ReadUntilFieldValue();

                Assert.Equal(value, reader.ReadString());
            }
예제 #29
0
        object Deserialize <T>(TypeModel model, byte[] data)
        {
            Type type = typeof(T);
            int  key  = model.GetKey(ref type);

            using (MemoryStream ms = new MemoryStream(data))
            {
                ProtoReader reader = null;
                try
                {
                    reader = ProtoReader.Create(ms, model, null, ProtoReader.TO_EOF);
                    return(model.Deserialize(key, null, reader));
                }
                finally
                {
                    ProtoReader.Recycle(reader);
                }
            }
        }
예제 #30
0
        private void ReadStream(Stream stream, Dictionary <int, int> remoteToLocalRowIds)
        {
            var len = 0;

            if (WithLengthPrefix)
            {
                // Read the length of the message from the stream.
                // DirectReadVarintInt32 calls Stream.ReadByte() which allocates a one byte array on every call - yuck!
                var header = ProtoReader.DirectReadVarintInt32(stream);
                len = ProtoReader.DirectReadVarintInt32(stream);
            }
            // Allocation of protoreader for each message - blurghh!
            using (var reader = ProtoReader.Create(stream, null, null, len))
            {
                int fieldId;
                while ((fieldId = reader.ReadFieldHeader()) != 0)
                {
                    if (fieldId == ProtobufOperationTypes.Add)
                    {
                        var rowId = ReadRowId(reader);
                        if (rowId >= 0)
                        {
                            remoteToLocalRowIds.Add(rowId, _table.AddRow());
                            var dummyFieldId = reader.ReadFieldHeader();
                            ReadUpdate(remoteToLocalRowIds, reader);
                        }
                    }
                    else if (fieldId == ProtobufOperationTypes.Update)
                    {
                        ReadUpdate(remoteToLocalRowIds, reader);
                    }
                    else if (fieldId == ProtobufOperationTypes.Delete)
                    {
                        var rowId = ReadRowId(reader);
                        if (rowId >= 0)
                        {
                            _table.DeleteRow(remoteToLocalRowIds[rowId]);
                            remoteToLocalRowIds.Remove(rowId);
                        }
                    }
                }
            }
        }