public void EventListSerializtionRountTrip() { var evt = new Event { EventType = "AAA", Payload = new byte[8] }; var batch = new KafkaEventBusBatchContainer { Events = new List <Event> { evt } }; var output = new OutputBuffer(); var bondWriter = new SimpleBinaryWriter <OutputBuffer>(output); Serialize.To(bondWriter, batch); var data = output.Data.ToArray(); var input = new InputBuffer(data); var bondReader = new SimpleBinaryReader <InputBuffer>(input); var result = Deserialize <KafkaEventBusBatchContainer> .From(bondReader); result.Events.Should().BeEquivalentTo(batch.Events); }
void RoundtripObjectStream <T>(T obj) { // SimpleBinary { var output = new OutputBuffer(); var writer = new SimpleBinaryWriter <OutputBuffer>(output); Serialize.To(writer, obj); var input = new InputStream(new MemoryStream(output.buffer)); var reader = new SimpleBinaryReader <InputStream>(input); T obj2 = Deserialize <T> .From(reader); Assert.True(Comparer.Equal <T>(obj, obj2)); } // CompactBinary { var output = new OutputBuffer(); var writer = new CompactBinaryWriter <OutputBuffer>(output); Serialize.To(writer, obj); var input = new InputStream(new MemoryStream(output.buffer)); var reader = new CompactBinaryReader <InputStream>(input); T obj2 = new Deserializer <CompactBinaryReader <InputStream> >(typeof(T)).Deserialize <T>(reader); Assert.True(Comparer.Equal <T>(obj, obj2)); } }
public void TestEventDataSerialization() { //const string cvText = "CV:1234"; var variables = new KeyValuePair <string, object>[] { new KeyValuePair <string, object>("string", "string.Value"), new KeyValuePair <string, object>("int", 33), new KeyValuePair <string, object>("long", 101), new KeyValuePair <string, object>("date", DateTime.Now), }; var data = Utility.CreateData(1); EventDataRecord record = data.ConvertTo(); var output = new OutputBuffer(); var writer = new SimpleBinaryWriter <OutputBuffer>(output); var writeSerializer = new Serializer <SimpleBinaryWriter <OutputBuffer> >(typeof(EventDataRecord)); writeSerializer.Serialize(record, writer); var input = new InputBuffer(output.Data); var reader = new SimpleBinaryReader <InputBuffer>(input); var readSerializer = new Deserializer <SimpleBinaryReader <InputBuffer> >(typeof(EventDataRecord)); EventDataRecord resultRecord = readSerializer.Deserialize <EventDataRecord>(reader); EventData result = resultRecord.ConverTo(); Utility.VerifyEventData(data, result); }
static void Main() { // Get runtime schemas for two "versions" of Example schema. // When using untagged protocols the consumer must have access to runtime schema of the data. var schema1 = Schema <Example1> .RuntimeSchema; var schema2 = Schema <Example2> .RuntimeSchema; // Create and cache deserializers for objects of type Example2 from payloads using the two schemas var deserializers = new Dictionary <string, Deserializer <SimpleBinaryReader <InputBuffer> > > { { "Example1", new Deserializer <SimpleBinaryReader <InputBuffer> >(typeof(Example2), schema1) }, { "Example2", new Deserializer <SimpleBinaryReader <InputBuffer> >(typeof(Example2), schema2) } }; // Create payload serializing an instance of Example1 var src = new Example1 { Enabled = true, Name = "Foo" }; var output = new OutputBuffer(); var writer = new SimpleBinaryWriter <OutputBuffer>(output); Serialize.To(writer, src); var input = new InputBuffer(output.Data); var reader = new SimpleBinaryReader <InputBuffer>(input); // Use the precreated deserializer to deserialize an instance of Example2 schema for Example1 var dst = deserializers["Example1"].Deserialize <Example2>(reader); Debug.Assert(src.Enabled == dst.Enabled); Debug.Assert(src.Name == dst.Name); }
static void Write <R>(R reader, OutputStream output, string toProtocol) { if (toProtocol == "compact") { var writer = new CompactBinaryWriter <OutputStream>(output); Serialize.To(writer, Deserialize <Compat> .From(reader)); } else if (toProtocol == "fast") { var writer = new FastBinaryWriter <OutputStream>(output); Serialize.To(writer, Deserialize <Compat> .From(reader)); } else if (toProtocol == "simple") { var writer = new SimpleBinaryWriter <OutputStream>(output); Serialize.To(writer, Deserialize <Compat> .From(reader)); } else if (toProtocol == "simple2") { var writer = new SimpleBinaryWriter <OutputStream>(output, 2); Serialize.To(writer, Deserialize <Compat> .From(reader)); } else { Console.WriteLine("Unsupported output protocol {0}", toProtocol); } output.Flush(); }
static void Main() { // Get runtime schemas for two "versions" of Example schema. // When using untagged protocols the consumer must have access to runtime schema of the data. var schema1 = Schema<Example1>.RuntimeSchema; var schema2 = Schema<Example2>.RuntimeSchema; // Create and cache deserializers for objects of type Example2 from payloads using the two schemas var deserializers = new Dictionary<string, Deserializer<SimpleBinaryReader<InputBuffer>>> { {"Example1", new Deserializer<SimpleBinaryReader<InputBuffer>>(typeof(Example2), schema1)}, {"Example2", new Deserializer<SimpleBinaryReader<InputBuffer>>(typeof(Example2), schema2)} }; // Create payload serializing an instance of Example1 var src = new Example1 { Enabled = true, Name = "Foo" }; var output = new OutputBuffer(); var writer = new SimpleBinaryWriter<OutputBuffer>(output); Serialize.To(writer, src); var input = new InputBuffer(output.Data); var reader = new SimpleBinaryReader<InputBuffer>(input); // Use the precreated deserializer to deserialize an instance of Example2 schema for Example1 var dst = deserializers["Example1"].Deserialize<Example2>(reader); Debug.Assert(src.Enabled == dst.Enabled); Debug.Assert(src.Name == dst.Name); }
public static void SerializeSP <T>(T obj, Stream stream, ushort version) { var output = new OutputStream(stream, 11); var writer = new SimpleBinaryWriter <OutputStream>(output, version); Serialize.To(writer, obj); output.Flush(); }
public static void SerializeSP <T>(IBonded <T> obj, Stream stream) { var output = new OutputStream(stream, 11); var writer = new SimpleBinaryWriter <OutputStream>(output); Serialize.To(writer, obj); output.Flush(); }
public static ArraySegment <byte> SerializeSP <T>(T obj, ushort version) { var output = new OutputBuffer(); var writer = new SimpleBinaryWriter <OutputBuffer>(output, version); Serialize.To(writer, obj); return(output.Data); }
public static void MarshalSP <T>(T obj, Stream stream) { var output = new OutputStream(stream, 11); var writer = new SimpleBinaryWriter <OutputStream>(output); Marshal.To(writer, obj); output.Flush(); }
public async Task QueueMessageBatchAsync <T>(Guid streamGuid, string streamNamespace, IEnumerable <T> events, StreamSequenceToken token, Dictionary <string, object> requestContext) { if (token != null) { throw new NotImplementedException("EventHub stream provider currently does not support non-null StreamSequenceToken."); } var cnt = events.Count(); if (cnt == 0) { return; } KafkaEventBusBatchContainer batch = new KafkaEventBusBatchContainer(); byte[] val = null; byte[] key = null; if (events is IEnumerable <DomainEvent> des) { if (cnt > 1) { throw new NotSupportedException("DomainEvent should only produce one by one"); } var aid = des.First().AggregateId; if (string.IsNullOrEmpty(aid)) { throw new NotSupportedException("DomainEvent must have aggregate id"); } key = Encoding.UTF8.GetBytes(aid); batch.Events = des.Select(de => de.Event).ToList(); } else if (events is IEnumerable <IntegrationEvent> ies) { key = null; batch.Events = ies.Select(ie => ie.Event).ToList(); } else { throw new NotSupportedException("Only support DomainEvent or IntegrationEvent"); } var output = new OutputBuffer(); var bondWriter = new SimpleBinaryWriter <OutputBuffer>(output); serializer.Serialize(batch, bondWriter); val = output.Data.ToArray(); var msg = await this.producer.ProduceAsync(streamNamespace, key, val); if (msg.Error.HasError) { // handle err // already throwed //throw new Confluent.Kafka.KafkaException(msg.Error); } }
public byte[] BondUnsafeSimpleReusedBuffer() { this.outputBuffer.Position = 0; var writer = new SimpleBinaryWriter <OutputBuffer>(this.outputBuffer); this.simpleBondSerializer.Serialize(this.bondObject, writer); return(this.outputBuffer.Data.Array); }
public byte[] BondUnsafeSimpleReused() { var output = new OutputBuffer(); var writer = new SimpleBinaryWriter <OutputBuffer>(output); this.simpleBondSerializer.Serialize(this.bondObject, writer); return(output.Data.Array); }
public int BondUnsafeSimple() { var output = new OutputBuffer(); var writer = new SimpleBinaryWriter <OutputBuffer>(output); Serialize.To(writer, this.bondObject); return(output.Data.Count); }
public Task Open() { Verify.Assert(_file == null, "Log file already opened"); Directory.CreateDirectory(FolderPath); _file = new FileStream(Path.Combine(FolderPath, $"Log_{Guid.NewGuid().ToString()}.txt"), FileMode.Create); _outputStream = new OutputStream(_file); _simpleBinaryWriter = new SimpleBinaryWriter <OutputStream>(_outputStream); return(Task.FromResult(0)); }
static void Main() { Console.Write("Reading all Application log events... "); List <EventRecordData> allEvents = Reader.ReadAllEvents().ToList(); Console.WriteLine("{0} events successfully read.", allEvents.Count); // OutputBuffer will receive serialized data var output = new OutputBuffer(); // Choose another protocol writer to match your needs var writer = new SimpleBinaryWriter <OutputBuffer>(output); Console.Write("Serializing all records... "); Stopwatch stopwatch = new Stopwatch(); // Serialize.To performs actual serialization stopwatch.Start(); allEvents.ForEach(x => Serialize.To(writer, x)); stopwatch.Stop(); Console.WriteLine("done in {0} ms, output size = {1} Kb", stopwatch.ElapsedMilliseconds, output.Position / 1024); // InputBuffer represents octet stream source for deserialization var input = new InputBuffer(output.Data); // Protocol reader shall match the writer from above var reader = new SimpleBinaryReader <InputBuffer>(input); Console.Write("Deserializing all records... "); Dictionary <EventLevel, int> counts = new Dictionary <EventLevel, int>(); stopwatch.Restart(); for (int i = 0; i < allEvents.Count; i++) { // Deserializing.. EventRecordData record = Deserialize <EventRecordData> .From(reader); if (!counts.ContainsKey(record.Level)) { counts[record.Level] = 0; } counts[record.Level]++; } stopwatch.Stop(); Console.WriteLine("done in {0} ms", stopwatch.ElapsedMilliseconds); Console.WriteLine("Statistics:"); foreach (KeyValuePair <EventLevel, int> pair in counts.OrderByDescending(x => x.Value)) { Console.WriteLine("\t{0} events of level {1}", pair.Value, pair.Key); } }
public static void TranscodeCBSP <From>(Stream from, Stream to) { var input = new InputStream(from, 11); var reader = new CompactBinaryReader <InputStream>(input); var output = new OutputStream(to, 19); var writer = new SimpleBinaryWriter <OutputStream>(output); Transcode <From> .FromTo(reader, writer); output.Flush(); }
public byte[] BondUnsafeSimpleCopied() { var output = new OutputBuffer(); var writer = new SimpleBinaryWriter <OutputBuffer>(output); Serialize.To(writer, this.bondObject); var resultArray = new byte[output.Data.Count]; Array.Copy(output.Data.Array, resultArray, output.Data.Count); return(resultArray); }
public Task <byte[]> Serialize <TValue>(TValue value) { var output = new OutputBuffer(); var writer = new SimpleBinaryWriter <OutputBuffer>(output); global::Bond.Serialize.To(writer, value); var data = output.Data; return(Task.FromResult(data.Array .Skip(data.Offset) .Take(data.Count) .ToArray())); }
/// <summary> /// Serializes a Q# compilation object to its Bond simple binary representation. /// </summary> /// <param name="qsCompilation">Q# compilation object to serialize.</param> /// <param name="stream">Stream to write the serialization to.</param> /// <remarks>This method waits for <see cref="Task"/>s to complete and may deadlock if invoked through a <see cref="Task"/>.</remarks> public static void SerializeQsCompilationToSimpleBinary( SyntaxTree.QsCompilation qsCompilation, Stream stream) { lock (BondSharedDataStructuresLock) { var outputBuffer = new OutputBuffer(); var serializer = GetSimpleBinarySerializer(); var writer = new SimpleBinaryWriter <OutputBuffer>(outputBuffer); var bondCompilation = BondSchemaTranslator.CreateBondCompilation(qsCompilation); serializer.Serialize(bondCompilation, writer); stream.Write(outputBuffer.Data); } stream.Flush(); stream.Position = 0; }
void RoundtripObjectBuffer <T>(T obj) { // SimpleBinary { var output = new OutputBuffer(); var writer = new SimpleBinaryWriter <OutputBuffer>(output); Serialize.To(writer, obj); var input = new InputBuffer(output.Data); var reader = new SimpleBinaryReader <InputBuffer>(input); T obj2 = Deserialize <T> .From(reader); Assert.True(Comparer.Equal <T>(obj, obj2)); } // CompactBinary { var output = new OutputBuffer(); var writer = new CompactBinaryWriter <OutputBuffer>(output); Serialize.To(writer, obj); var input = new InputBuffer(output.Data); var reader = new CompactBinaryReader <InputBuffer>(input); T obj2 = new Deserializer <CompactBinaryReader <InputBuffer> >(typeof(T)).Deserialize <T>(reader); Assert.True(Comparer.Equal <T>(obj, obj2)); } // SimpleXML { var xmlString = new StringBuilder(); var xmlWriter = new SimpleXmlWriter(XmlWriter.Create(xmlString)); Serialize.To(xmlWriter, obj); xmlWriter.Flush(); var reader = new SimpleXmlReader(XmlReader.Create(new StringReader(xmlString.ToString()))); T obj2 = new Deserializer <SimpleXmlReader>(typeof(T)).Deserialize <T>(reader); Assert.True(Comparer.Equal <T>(obj, obj2)); } }
void RoundtripObjectStream <T>(T obj) { // SimpleBinary { var output = new OutputBuffer(); var writer = new SimpleBinaryWriter <OutputBuffer>(output); Serialize.To(writer, obj); var input = new InputStream( new MemoryStream( output.Data.Array, output.Data.Offset, output.Data.Count, writable: false, publiclyVisible: true)); var reader = new SimpleBinaryReader <InputStream>(input); T obj2 = Deserialize <T> .From(reader); Assert.True(Comparer.Equal <T>(obj, obj2)); } // CompactBinary { var output = new OutputBuffer(); var writer = new CompactBinaryWriter <OutputBuffer>(output); Serialize.To(writer, obj); var input = new InputStream( new MemoryStream( output.Data.Array, output.Data.Offset, output.Data.Count, writable: false, publiclyVisible: true)); var reader = new CompactBinaryReader <InputStream>(input); T obj2 = new Deserializer <CompactBinaryReader <InputStream> >(typeof(T)).Deserialize <T>(reader); Assert.True(Comparer.Equal <T>(obj, obj2)); } }
public void SimpleBinaryReader_ReadLength_NegativeLength_Throws() { var f = new Foo { _str = "abc" }; var output = new Bond.IO.Safe.OutputBuffer(16); var writer = new SimpleBinaryWriter <OutputBuffer>(output, 2); Serialize.To(writer, f); var data = new byte[] { 0x00, 0xff, // count (5 byte) 0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x61, }; var input = new InputBuffer(data); var reader = new SimpleBinaryReader <InputBuffer>(input); Assert.Throws <OverflowException>(() => Deserialize <Foo> .From(reader)); input = new InputBuffer(data); reader = new SimpleBinaryReader <InputBuffer>(input, 2); Assert.Throws <OverflowException>(() => Deserialize <Foo> .From(reader)); }
public void TestMultiple() { const int count = 10; var eventDataItems = new List <EventData>(); var rnd = new Random(); var output = new OutputBuffer(); var writer = new SimpleBinaryWriter <OutputBuffer>(output); var writeSerializer = new Serializer <SimpleBinaryWriter <OutputBuffer> >(typeof(EventDataRecord)); foreach (var index in Enumerable.Range(0, count)) { var data = Utility.CreateData(index); EventDataRecord record = data.ConvertTo(); eventDataItems.Add(data); writeSerializer.Serialize(record, writer); } var input = new InputBuffer(output.Data); var reader = new SimpleBinaryReader <InputBuffer>(input); var readSerializer = new Deserializer <SimpleBinaryReader <InputBuffer> >(typeof(EventDataRecord)); foreach (var index in Enumerable.Range(0, count)) { EventDataRecord resultRecord = readSerializer.Deserialize <EventDataRecord>(reader); EventData result = resultRecord.ConverTo(); Utility.VerifyEventData(eventDataItems[index], result); } Action act = () => Deserialize <EventDataRecord> .From(reader); act.Should().Throw <EndOfStreamException>(); }
/// <summary> /// Serializes an object to a SimpleBinaryWriter stream. /// </summary> /// <typeparam name="T">Object type to serialize.</typeparam> /// <typeparam name="O">Destination stream type.</typeparam> /// <param name="writer">Writer to emit to.</param> /// <param name="obj">Object to write.</param> public static void Write <T, O>(this SimpleBinaryWriter <O> writer, T obj) where O : IOutputStream { Serialize.To(writer, obj); }