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 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 T DeserializeBadDataHelper <T, U>(CreationDelegate <U> creator) where U : Bond.IO.IInputStream, Bond.IO.ICloneable <U> { // Untagged protocol used to trigger errors more easily var reader = new SimpleBinaryReader <U>(creator(badData)); return(new Deserializer <SimpleBinaryReader <U> >(typeof(T)).Deserialize <T>(reader)); }
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); }
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); }
static T DeserializeBadData <T>() { // Untagged protocol used to trigger errors more easily var reader = new SimpleBinaryReader <InputBuffer>(new InputBuffer(badData)); return(new Deserializer <SimpleBinaryReader <InputBuffer> >(typeof(T)).Deserialize <T>(reader)); }
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 static To DeserializeSafeSP <From, To>(ArraySegment <byte> data) { var input = new Bond.IO.Safe.InputBuffer(data.Array, data.Offset, data.Count); var reader = new SimpleBinaryReader <Bond.IO.Safe.InputBuffer>(input); var deserializer = new Deserializer <SimpleBinaryReader <Bond.IO.Safe.InputBuffer> >(typeof(To), Schema <From> .RuntimeSchema); return(deserializer.Deserialize <To>(reader)); }
public static To DeserializeUnsafeSP <From, To>(ArraySegment <byte> data, ushort version) { var input = new InputBuffer(data); var reader = new SimpleBinaryReader <InputBuffer>(input, version); var deserializer = new Deserializer <SimpleBinaryReader <InputBuffer> >(typeof(To), Schema <From> .RuntimeSchema); return(deserializer.Deserialize <To>(reader)); }
public static To DeserializeSP <From, To>(Stream stream, ushort version) { var input = new InputStream(stream); var reader = new SimpleBinaryReader <InputStream>(input, version); var deserializer = new Deserializer <SimpleBinaryReader <InputStream> >(typeof(To), Schema <From> .RuntimeSchema); return(deserializer.Deserialize <To>(reader)); }
public Task <TValue> Deserialize <TValue>(byte[] data) { var input = new InputBuffer(data); var reader = new SimpleBinaryReader <InputBuffer>(input); return(Task.FromResult(global::Bond.Deserialize <TValue> .From(reader))); }
public static To DeserializePointerSP <From, To>(IntPtr data, int length) { var input = new InputPointer(data, length); var reader = new SimpleBinaryReader <InputPointer>(input); var deserializer = new Deserializer <SimpleBinaryReader <InputPointer> >(typeof(To), Schema <From> .RuntimeSchema); return(deserializer.Deserialize <To>(reader)); }
public unsafe static To DeserializeUnsafeSPWithPtr <From, To>(Tuple <IntPtr, int> data) { byte *ptr = (byte *)data.Item1.ToPointer(); var input = new InputPtrBuffer(ptr, data.Item2); var reader = new SimpleBinaryReader <InputPtrBuffer>(input); var deserializer = new Deserializer <SimpleBinaryReader <InputPtrBuffer> >(typeof(To), Schema <From> .RuntimeSchema); return(deserializer.Deserialize <To>(reader)); }
public void Read(SimpleBinaryReader reader) { string str; this.FormatStringLength = reader.ReadUInt16(); this.OptionFlags = reader.ReadByte(); reader.Seek(-1, (SeekOrigin)SeekOrigin.Current); reader.ReadUncompressedString((short)this.FormatStringLength, out str); this.FormatString = str; }
public static void TranscodeSPJson <From>(Stream from, Stream to) { var input = new InputStream(from, 11); var reader = new SimpleBinaryReader <InputStream>(input); var writer = new SimpleJsonWriter(to); Transcode <From> .FromTo(reader, writer); writer.Flush(); }
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 void Read(SimpleBinaryReader reader) { this.fHighByte = (byte)(reader.ReadByte() & 1); if (this.fHighByte == 1) { this.rgb = reader.ReadBytes(this.cch * 2); } else { this.rgb = reader.ReadBytes(this.cch); } }
public static void TranscodeSPFB <From>(Stream from, Stream to) { var input = new InputStream(from, 11); var reader = new SimpleBinaryReader <InputStream>(input); var output = new OutputStream(to, 19); var writer = new FastBinaryWriter <OutputStream>(output); Transcode <From> .FromTo(reader, writer); output.Flush(); }
public void Read(SimpleBinaryReader reader) { this.FontIndex = reader.ReadUInt16(); this.FormatIndex = reader.ReadUInt16(); this.ReadXFTyleCellProtectionAndParentXF(reader.ReadUInt16()); this.ReadAlignmentAndTextBreak(reader.ReadByte()); this.TextRotation = reader.ReadByte(); this.ReadIndentShrinkAndTextDirection(reader.ReadByte()); this.ReadFlags(reader.ReadByte()); this.ReadBorder(reader.ReadUInt32()); this.ReadBorderAndFillPattern(reader.ReadUInt32()); this.ReadFillPatternColor(reader.ReadUInt16()); }
/// <summary> /// Deserializes a Q# compilation object from its Bond simple binary representation. /// </summary> /// <param name="byteArray">Bond simple binary representation of a Q# compilation object.</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 SyntaxTree.QsCompilation? DeserializeQsCompilationFromSimpleBinary( byte[] byteArray) { QsCompilation? bondCompilation = null; var inputBuffer = new InputBuffer(byteArray); var reader = new SimpleBinaryReader<InputBuffer>(inputBuffer); lock (BondSharedDataStructuresLock) { var deserializer = GetSimpleBinaryDeserializer(); bondCompilation = deserializer.Deserialize<QsCompilation>(reader); } return CompilerObjectTranslator.CreateQsCompilation(bondCompilation); }
public static void TranscodeSPXml <From>(Stream from, Stream to) { var input = new InputStream(from, 11); var reader = new SimpleBinaryReader <InputStream>(input); var hasBase = Schema <From> .RuntimeSchema.HasBase; var writer = new SimpleXmlWriter(to, new SimpleXmlWriter.Settings { UseNamespaces = hasBase }); Transcode <From> .FromTo(reader, writer); writer.Flush(); }
public static IBonded <T> MakeBondedSP <T>(T obj) { var stream = new MemoryStream(); SerializeSP(obj, stream); stream.Position = 0; // Create new MemoryStream at non-zero offset in a buffer var buffer = new byte[stream.Length + 2]; stream.Read(buffer, 1, buffer.Length - 2); var input = new InputStream(new MemoryStream(buffer, 1, buffer.Length - 2, false, true)); var reader = new SimpleBinaryReader <InputStream>(input); return(new Bonded <T, SimpleBinaryReader <InputStream> >(reader)); }
public void Read(SimpleBinaryReader reader) { string str; this.FontHeight = ((double)reader.ReadUInt16()) / 20.0; this.InitFontAttribute(reader.ReadUInt16()); this.ColorIndex = reader.ReadUInt16(); this.IsBold = reader.ReadUInt16() == 700; this.SuperScript = reader.ReadUInt16(); this.UnderlineStyle = reader.ReadByte(); this.FontFamily = reader.ReadByte(); this.CharacterSet = reader.ReadByte(); reader.ReadByte(); this.FontNameLength = reader.ReadByte(); reader.ReadUncompressedString(this.FontNameLength, out str); this.FontName = str; }
public void CanReadAndWriteSimpleProtocol() { using (var stream = new MemoryStream()) using (var writerStream = new WriterStream(stream, this.mgr)) { var writer = writerStream.CreateSimpleBinaryWriter(); writer.Write(this.testObject); stream.Position = 0; using (var reader = ReaderStream.FromMemoryStreamBuffer(stream, this.mgr)) { var sbReader = new SimpleBinaryReader <ReaderStream>(reader); var anotherObject = sbReader.Read <AnotherDerivedThing>(); Assert.IsTrue(Comparer.Equal(anotherObject, this.testObject)); } } }
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>(); }
public static KafkaEventBusBatchContainer FromKafkaMessage(Message msg, BatchDeserializer deserializer, long seqNumber) { var input = new InputBuffer(msg.Value); var bondReader = new SimpleBinaryReader <InputBuffer>(input); var container = deserializer.Deserialize <KafkaEventBusBatchContainer>(bondReader); var aggIdString = msg.Key == null ? null : Encoding.UTF8.GetString(msg.Key); if (!string.IsNullOrEmpty(aggIdString) && Guid.TryParse(aggIdString, out var guid)) { container.StreamGuid = guid; } else { container.StreamGuid = new Guid(msg.Partition, 0, 0, zero8); } container.StreamNamespace = msg.Topic; container.EventSequenceToken = new EventSequenceToken(seqNumber); container.TopicPartitionOffset = msg.TopicPartitionOffset; return(container); }
public static void AllSerializeDeserialize <From, To>(From from, bool noTranscoding = false) where From : class where To : class { RoundtripMemory <From, To> memoryRoundtrip = (serialize, deserialize) => { var data = serialize(from); var to = deserialize(data); Assert.IsTrue(from.IsEqual(to)); }; RoundtripPointer <From, To> pointerRoundtrip = (serialize, deserialize) => { var ptr = RMarshal.AllocHGlobal(UnsafeBufferSize); var data = serialize(from, ptr, UnsafeBufferSize); var to = deserialize(data, UnsafeBufferSize); Assert.IsTrue(from.IsEqual(to)); RMarshal.FreeHGlobal(data); }; RoundtripMemoryPointer <From, To> memoryPointerRoundtrip = (serialize, deserialize) => { var data = serialize(from); var pinned = GCHandle.Alloc(data.Array, GCHandleType.Pinned); var to = deserialize(RMarshal.UnsafeAddrOfPinnedArrayElement(data.Array, data.Offset), data.Count); Assert.IsTrue(from.IsEqual(to)); pinned.Free(); }; RoundtripStream <From, To> streamRoundtrip = (serialize, deserialize) => { var stream = new MemoryStream(); serialize(from, stream); stream.Position = 0; var to = deserialize(stream); Assert.IsTrue(from.IsEqual(to)); }; MarshalStream <From> streamMarshal = serialize => streamRoundtrip(serialize, stream => { stream.Position = 0; return(Unmarshal <To> .From(new InputStream(stream))); }); MarshalStream <From> streamMarshalSchema = serialize => streamRoundtrip(serialize, stream => { stream.Position = 0; return(Unmarshal.From(new InputStream(stream), Schema <From> .RuntimeSchema).Deserialize <To>()); }); MarshalStream <From> streamMarshalNoSchema = serialize => streamRoundtrip(serialize, stream => { stream.Position = 0; return(Unmarshal.From(new InputStream(stream)).Deserialize <To>()); }); MarshalMemory <From> memoryMarshal = serialize => memoryRoundtrip(serialize, Unmarshal <To> .From); TranscodeStream <From, To> streamTranscode = (serialize, transcode, deserialize) => streamRoundtrip((obj, stream) => { using (var tmp = new MemoryStream()) { serialize(obj, tmp); tmp.Position = 0; transcode(tmp, stream); } }, deserialize); if (noTranscoding) { streamTranscode = (serialize, transcode, deserialize) => { } } ; // Compact Binary streamRoundtrip(SerializeCB, DeserializeCB <To>); memoryRoundtrip(SerializeUnsafeCB, DeserializeSafeCB <To>); memoryRoundtrip(SerializeUnsafeCB, DeserializeUnsafeCB <To>); memoryPointerRoundtrip(SerializeUnsafeCB, DeserializePointerCB <To>); pointerRoundtrip(SerializePointerCB, DeserializePointerCB <To>); memoryRoundtrip(SerializeSafeCB, DeserializeSafeCB <To>); memoryRoundtrip(SerializeSafeCB, DeserializeUnsafeCB <To>); memoryPointerRoundtrip(SerializeSafeCB, DeserializePointerCB <To>); memoryRoundtrip(SerializeSafeCBNoInlining, DeserializeSafeCB <To>); streamMarshal(MarshalCB); streamMarshal(SerializerMarshalCB); streamMarshalSchema(MarshalCB); streamMarshalNoSchema(MarshalCB); memoryMarshal(MarshalCB); streamTranscode(SerializeCB, TranscodeCBCB, DeserializeCB <To>); streamTranscode(SerializeCB, TranscodeCBFB, DeserializeFB <To>); streamRoundtrip(SerializeCB, stream => { var input = new InputStream(stream); var reader = new CompactBinaryReader <InputStream>(input); return(DeserializeTagged <To>(reader)); }); // Fast Binary streamRoundtrip(SerializeFB, DeserializeFB <To>); memoryRoundtrip(SerializeFB, DeserializeSafeFB <To>); memoryRoundtrip(SerializeFB, DeserializeUnsafeFB <To>); memoryPointerRoundtrip(SerializeFB, DeserializePointerFB <To>); streamMarshal(MarshalFB); streamMarshal(SerializerMarshalFB); streamMarshalSchema(MarshalFB); streamMarshalNoSchema(MarshalFB); memoryMarshal(MarshalFB); streamTranscode(SerializeFB, TranscodeFBFB, DeserializeFB <To>); streamTranscode(SerializeFB, TranscodeFBCB, DeserializeCB <To>); streamRoundtrip(SerializeFB, stream => { var input = new InputStream(stream); var reader = new FastBinaryReader <InputStream>(input); return(DeserializeTagged <To>(reader)); }); // Simple doesn't support omitting fields if (typeof(From) != typeof(Nothing) && typeof(From) != typeof(GenericsWithNothing)) { streamRoundtrip(SerializeSP, DeserializeSP <From, To>); memoryRoundtrip(SerializeSP, DeserializeSafeSP <From, To>); memoryRoundtrip(SerializeSP, DeserializeUnsafeSP <From, To>); memoryPointerRoundtrip(SerializeSP, DeserializePointerSP <From, To>); streamRoundtrip(SerializeSP2, DeserializeSP2 <From, To>); memoryRoundtrip(SerializeSP2, DeserializeSafeSP2 <From, To>); memoryRoundtrip(SerializeSP2, DeserializeUnsafeSP2 <From, To>); streamTranscode(SerializeCB, TranscodeCBSP <From>, DeserializeSP <From, To>); streamTranscode(SerializeFB, TranscodeFBSP <From>, DeserializeSP <From, To>); streamTranscode(SerializeSP, TranscodeSPSP <From>, DeserializeSP <From, To>); streamTranscode(SerializeSP, TranscodeSPCB <From>, DeserializeCB <To>); streamTranscode(SerializeSP, TranscodeSPFB <From>, DeserializeFB <To>); // Pull parser doesn't supprot bonded<T> if (AnyField <From>(Reflection.IsBonded)) { streamTranscode(SerializeSP, TranscodeSPXml <From>, DeserializeXml <To>); // NewtonSoft JSON doesn't support uint64 if (typeof(From) != typeof(MaxUInt64)) { streamTranscode(SerializeSP, TranscodeSPJson <From>, DeserializeJson <To>); } } streamRoundtrip(SerializeSP, stream => { var input = new InputStream(stream); var reader = new SimpleBinaryReader <InputStream>(input); return(DeserializeUntagged <From, To>(reader)); }); streamMarshalSchema(MarshalSP); } // Pull parser doesn't supprot bonded<T> if (AnyField <From>(Reflection.IsBonded)) { streamRoundtrip(SerializeXml, DeserializeXml <To>); streamTranscode(SerializeCB, TranscodeCBXml <From>, DeserializeXml <To>); streamTranscode(SerializeFB, TranscodeFBXml <From>, DeserializeXml <To>); // NewtonSoft JSON doesn't support uint64 if (typeof(From) != typeof(MaxUInt64)) { streamRoundtrip(SerializeJson, DeserializeJson <To>); streamTranscode(SerializeCB, TranscodeCBJson <From>, DeserializeJson <To>); streamTranscode(SerializeFB, TranscodeFBJson <From>, DeserializeJson <To>); } } } delegate bool TypePredicate(Type field);
public static void AllSerializeDeserialize <From, To>(From from, bool noTranscoding = false) where From : class where To : class { RoundtripMemory <From, To> memoryRoundtrip = (serialize, deserialize) => { var data = serialize(from); var to = deserialize(data); Assert.IsTrue(from.IsEqual(to)); }; RoundtripStream <From, To> streamRoundtrip = (serialize, deserialize) => { var stream = new MemoryStream(); serialize(from, stream); stream.Position = 0; var to = deserialize(stream); Assert.IsTrue(from.IsEqual(to)); }; MarshalStream <From> streamMarshal = serialize => streamRoundtrip(serialize, stream => { stream.Position = 0; return(Unmarshal <To> .From(new InputStream(stream))); }); MarshalStream <From> streamMarshalSchema = serialize => streamRoundtrip(serialize, stream => { stream.Position = 0; return(Unmarshal.From(new InputStream(stream), Schema <From> .RuntimeSchema).Deserialize <To>()); }); MarshalStream <From> streamMarshalNoSchema = serialize => streamRoundtrip(serialize, stream => { stream.Position = 0; return(Unmarshal.From(new InputStream(stream)).Deserialize <To>()); }); MarshalMemory <From> memoryMarshal = serialize => memoryRoundtrip(serialize, Unmarshal <To> .From); TranscodeStream <From, To> streamTranscode = (serialize, transcode, deserialize) => streamRoundtrip((obj, stream) => { using (var tmp = new MemoryStream()) { serialize(obj, tmp); tmp.Position = 0; transcode(tmp, stream); } }, deserialize); if (noTranscoding) { streamTranscode = (serialize, transcode, deserialize) => { } } ; // Compact Binary streamRoundtrip(SerializeCB, DeserializeCB <To>); memoryRoundtrip(SerializeUnsafeCB, DeserializeSafeCB <To>); memoryRoundtrip(SerializeUnsafeCB, DeserializeUnsafeCB <To>); memoryRoundtrip(SerializeSafeCB, DeserializeSafeCB <To>); memoryRoundtrip(SerializeSafeCB, DeserializeUnsafeCB <To>); streamMarshal(MarshalCB); streamMarshal(SerializerMarshalCB); streamMarshalSchema(MarshalCB); streamMarshalNoSchema(MarshalCB); memoryMarshal(MarshalCB); streamTranscode(SerializeCB, TranscodeCBCB, DeserializeCB <To>); streamTranscode(SerializeCB, TranscodeCBFB, DeserializeFB <To>); streamRoundtrip(SerializeCB, stream => { var input = new InputStream(stream); var reader = new CompactBinaryReader <InputStream>(input); return(DeserializeTagged <To>(reader)); }); // Fast Binary streamRoundtrip(SerializeFB, DeserializeFB <To>); memoryRoundtrip(SerializeFB, DeserializeSafeFB <To>); memoryRoundtrip(SerializeFB, DeserializeUnsafeFB <To>); streamMarshal(MarshalFB); streamMarshal(SerializerMarshalFB); streamMarshalSchema(MarshalFB); streamMarshalNoSchema(MarshalFB); memoryMarshal(MarshalFB); streamTranscode(SerializeFB, TranscodeFBFB, DeserializeFB <To>); streamTranscode(SerializeFB, TranscodeFBCB, DeserializeCB <To>); streamRoundtrip(SerializeFB, stream => { var input = new InputStream(stream); var reader = new FastBinaryReader <InputStream>(input); return(DeserializeTagged <To>(reader)); }); // Simple doesn't support omitting fields if (typeof(From) != typeof(Nothing) && typeof(From) != typeof(GenericsWithNothing)) { streamRoundtrip(SerializeSP, DeserializeSP <From, To>); memoryRoundtrip(SerializeSP, DeserializeSafeSP <From, To>); memoryRoundtrip(SerializeSP, DeserializeUnsafeSP <From, To>); streamTranscode(SerializeCB, TranscodeCBSP <From>, DeserializeSP <From, To>); streamTranscode(SerializeFB, TranscodeFBSP <From>, DeserializeSP <From, To>); streamTranscode(SerializeSP, TranscodeSPSP <From>, DeserializeSP <From, To>); streamTranscode(SerializeSP, TranscodeSPCB <From>, DeserializeCB <To>); streamTranscode(SerializeSP, TranscodeSPFB <From>, DeserializeFB <To>); streamTranscode(SerializeSP, TranscodeSPXml <From>, DeserializeXml <To>); streamTranscode(SerializeSP, TranscodeSPJson <From>, DeserializeJson <To>); streamRoundtrip(SerializeSP, stream => { var input = new InputStream(stream); var reader = new SimpleBinaryReader <InputStream>(input); return(DeserializeUntagged <From, To>(reader)); }); streamMarshalSchema(MarshalSP); } streamRoundtrip(SerializeXml, DeserializeXml <To>); streamRoundtrip(SerializeJson, DeserializeJson <To>); streamTranscode(SerializeCB, TranscodeCBXml <From>, DeserializeXml <To>); streamTranscode(SerializeCB, TranscodeCBJson <From>, DeserializeJson <To>); streamTranscode(SerializeFB, TranscodeFBXml <From>, DeserializeXml <To>); streamTranscode(SerializeFB, TranscodeFBJson <From>, DeserializeJson <To>); } }