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()); } }
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); } }
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()); }
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 }
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()); }
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()); }
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 }
public ProtoReader ReadROM(out ProtoReader.State state) { if (!_ros.IsSingleSegment) { throw new InvalidOperationException("Expected single segment"); } return(ProtoReader.Create(out state, _ros.First, Model)); }
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 }
/// <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); }
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); } } }
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 }
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()); }
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 }
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()); }
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()); }
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)); }
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()); }
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()); }
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)); }
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)); }
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)); }
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()); }
/// <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); } }
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()); }
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); } } }
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); } } } } }