static void Main() { // The Example type internally uses instances generic schemas Generic1 and Generic2 var src = new Example { Field = { Field = new Generic2<int> { Field = 13 } } }; // We can also instantiate generic schema in the C# program var src1 = new Generic1<Example> { Field = src }; var src2 = new Generic2<double> {Field = 3.14}; var output = new OutputBuffer(); var writer = new CompactBinaryWriter<OutputBuffer>(output); Serialize.To(writer, src); Serialize.To(writer, src1); Serialize.To(writer, src2); var input = new InputBuffer(output.Data); var reader = new CompactBinaryReader<InputBuffer>(input); var dst = Deserialize<Example>.From(reader); Debug.Assert(Comparer.Equal(src, dst)); var dst1 = Deserialize<Generic1<Example>>.From(reader); Debug.Assert(Comparer.Equal(src1, dst1)); var dst2 = Deserialize<Generic2<double>>.From(reader); Debug.Assert(Comparer.Equal(src2, dst2)); }
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 void Varint() { IntTest<ushort> test16 = (value) => { var output = new OutputBuffer(); output.WriteVarUInt16(value); var input = new InputBuffer(output.Data); Assert.AreEqual(value, input.ReadVarUInt16()); }; IntTest<uint> test32 = (value) => { var output = new OutputBuffer(); output.WriteVarUInt32(value); var input = new InputBuffer(output.Data); Assert.AreEqual(value, input.ReadVarUInt32()); }; IntTest<ulong> test64 = (value) => { var output = new OutputBuffer(); output.WriteVarUInt64(value); var input = new InputBuffer(output.Data); Assert.AreEqual(value, input.ReadVarUInt64()); }; test16(ushort.MinValue); test16(ushort.MaxValue); test32(uint.MinValue); test32(uint.MaxValue); test64(ulong.MinValue); test64(ulong.MaxValue); }
static void Main() { var data = Enumerable.Range(0, 255).Select(i => (byte)i).ToArray(); var src = new Example { ListOfBlobs = { new ArraySegment<byte>(data, 0, 10), new ArraySegment<byte>(data, 10, 10) }, NullableBlob = new ArraySegment<byte>(data, 20, 10), UninitializeBlob = new ArraySegment<byte>(data, 30, 70) }; var output = new OutputBuffer(); var writer = new CompactBinaryWriter<OutputBuffer>(output); Serialize.To(writer, src); var input = new InputBuffer(output.Data); var reader = new CompactBinaryReader<InputBuffer>(input); var dst = Deserialize<Example>.From(reader); Debug.Assert(Comparer.Equal(src, dst)); }
public override string Serialize(object serializable) { Initialize(); var output = new OutputBuffer(2*1024); var writer = new FastBinaryWriter<OutputBuffer>(output); _serializer.Serialize(serializable, writer); return Convert.ToBase64String(output.Data.Array, output.Data.Offset, output.Data.Count); }
static void Main() { var src = new Example { id = new Guid("{DC37ECC5-9E39-49C9-931B-51138D648262}") }; var output = new OutputBuffer(); var writer = new CompactBinaryWriter<OutputBuffer>(output); Serialize.To(writer, src); var input = new InputBuffer(output.Data); var reader = new CompactBinaryReader<InputBuffer>(input); var dst = Deserialize<Example>.From(reader); Debug.Assert(dst.id == src.id); }
static void Main() { var src = new Example { Price = 9999999999999999999999999999M, Numbers = { 79228162514264337593543950335M } }; var output = new OutputBuffer(); var writer = new CompactBinaryWriter<OutputBuffer>(output); Serialize.To(writer, src); var input = new InputBuffer(output.Data); var reader = new CompactBinaryReader<InputBuffer>(input); var dst = Deserialize<Example>.From(reader); Debug.Assert(Comparer.Equal(src, dst)); }
static void Main() { var src = new Example { Now = DateTime.Now, Dates = { new DateTime(2017, 1, 29) } }; var output = new OutputBuffer(); var writer = new CompactBinaryWriter<OutputBuffer>(output); Serialize.To(writer, src); var input = new InputBuffer(output.Data); var reader = new CompactBinaryReader<InputBuffer>(input); var dst = Deserialize<Example>.From(reader); Debug.Assert(Comparer.Equal(src, dst)); }
static void Main() { var src = new Example { Name = "FooBar", Constants = { 3.14, 6.28 } }; var output = new OutputBuffer(); var writer = new CompactBinaryWriter<OutputBuffer>(output); Serialize.To(writer, src); var input = new InputBuffer(output.Data); var reader = new CompactBinaryReader<InputBuffer>(input); var dst = Deserialize<Example>.From(reader); Debug.Assert(Comparer.Equal(src, dst)); }
static void Main() { var src = new Message { Header = new Header { Origin = "contoso.com", Destination = "fabrikam.com" }, Priority = Priority.Normal, MessagePayload = 42 }; var output = new OutputBuffer(); var writer = new CompactBinaryWriter<OutputBuffer>(output); Serialize.To(writer, src); var input = new InputBuffer(output.Data); var reader = new CompactBinaryReader<InputBuffer>(input); var dst = Deserialize<Message>.From(reader); Debug.Assert(Comparer.Equal(src, dst)); }
static void Main() { var src = new Example { Name = "foo", Constants = { 3.14, 6.28 } }; var output = new OutputBuffer(); var writer = new CompactBinaryWriter<OutputBuffer>(output); Marshal.To(writer, src); var input = new InputBuffer(output.Data); // We don't need to specify protocol for unmarshaling, // it is determined from information stored in the payload. var dst = Unmarshal<Example>.From(input); Debug.Assert(Comparer.Equal(src, dst)); }
static void Main() { var src = new Example { id_str = new Guid("{DC37ECC5-9E39-49C9-931B-51138D648262}"), id_bin = new Guid("{0F5F6768-1608-4D5C-A11D-B176D0D792B5}"), }; var output = new OutputBuffer(); var writer = new CompactBinaryWriter<OutputBuffer>(output); Serialize.To(writer, src); var input = new InputBuffer(output.Data); var reader = new CompactBinaryReader<InputBuffer>(input); var dst = Deserialize<Example>.From(reader); Debug.Assert(dst.id_str == src.id_str); Debug.Assert(dst.id_bin == src.id_bin); }
static void Main() { var output = new OutputBuffer(); var writer = new CompactBinaryWriter<OutputBuffer>(output); // Get runtime schema for type Example and serialize SchemaDef Serialize.To(writer, Schema<Example>.RuntimeSchema.SchemaDef); var input = new InputBuffer(output.Data); var reader = new CompactBinaryReader<InputBuffer>(input); var schemaDef = Deserialize<SchemaDef>.From(reader); var schema = new RuntimeSchema(schemaDef); Debug.Assert(schema.IsStruct); Debug.Assert(schema.StructDef.metadata.qualified_name == "Examples.Example"); Debug.Assert(schema.StructDef.metadata.attributes["StructAttribute"] == "Value of the attribute"); Debug.Assert(schema.StructDef.fields[0].metadata.attributes["FieldAttribute"] == "Value of the attribute"); Debug.Assert(schema.StructDef.fields[0].type.key.id == BondDataType.BT_UINT32); Debug.Assert(schema.SchemaDef.structs[1].fields[0].metadata.default_value.string_value == "this is a string"); }
static void Main() { var src = new Example { Widgets = { new Widget { Name = "Konfabulator", Number = 3.14 } } }; var output = new OutputBuffer(); var writer = new CompactBinaryWriter<OutputBuffer>(output); Serialize.To(writer, src); var input = new InputBuffer(output.Data); var reader = new CompactBinaryReader<InputBuffer>(input); var xmlString = new StringBuilder(); var xmlWriter = new SimpleXmlWriter(XmlWriter.Create(xmlString, new XmlWriterSettings { OmitXmlDeclaration = true, Indent = true })); Transcode<Example>.FromTo(reader, xmlWriter); xmlWriter.Flush(); Debug.Assert(xmlString.ToString() == @"<Example> <Widgets> <Item> <Widget> <Name>Konfabulator</Name> <Number>3.14</Number> </Widget> </Item> </Widgets> </Example>"); }
static void Main() { var src = new Example { Name = "FooBar", Constants = { 3.14, 6.28 } }; var output = new OutputBuffer(); var writer = new CompactBinaryWriter<OutputBuffer>(output); // The first calls to Serialize.To and Deserialize<T>.From can take // a relatively long time because they generate the de/serializer // for a given type and protocol. Serialize.To(writer, src); var input = new InputBuffer(output.Data); var reader = new CompactBinaryReader<InputBuffer>(input); var dst = Deserialize<Example>.From(reader); Debug.Assert(Comparer.Equal(src, dst)); }
static void Main() { var src = new Example { Name = "FooBar", Constants = { 3.14, 6.28 } }; // Create de/serializer for the Example class and Compact Binary protocol. // This may take relatively long time so usually the objects should be cached and reused. var exampleSerializer = new Serializer<CompactBinaryWriter<OutputBuffer>>(typeof(Example)); var exampleDeserializer = new Deserializer<CompactBinaryReader<InputBuffer>>(typeof(Example)); var output = new OutputBuffer(); var writer = new CompactBinaryWriter<OutputBuffer>(output); exampleSerializer.Serialize(src, writer); var input = new InputBuffer(output.Data); var reader = new CompactBinaryReader<InputBuffer>(input); var dst = exampleDeserializer.Deserialize<Example>(reader); Debug.Assert(Comparer.Equal(src, dst)); }
static void Main() { var circle = new Circle { Type = Type.Circle, Radius = 3.14 }; var rectangle = new Rectangle { Type = Type.Rectange, Width = 10, Height = 5.5 }; var src = new Polymorphic { Shapes = { new Bonded<Circle>(circle), new Bonded<Rectangle>(rectangle) } }; var output = new OutputBuffer(); var writer = new CompactBinaryWriter<OutputBuffer>(output); Serialize.To(writer, src); var input = new InputBuffer(output.Data); var reader = new CompactBinaryReader<InputBuffer>(input); var dst = Deserialize<Polymorphic>.From(reader); var deserializers = new Dictionary<Type, Deserializer<CompactBinaryReader<InputBuffer>>> { {Type.Circle, new Deserializer<CompactBinaryReader<InputBuffer>>(typeof(Circle))}, {Type.Rectange, new Deserializer<CompactBinaryReader<InputBuffer>>(typeof(Rectangle))} }; foreach (var item in dst.Shapes) { // Deserialize item as Shape and extract object type var type = item.Deserialize().Type; // Select one of the precreated deserializers based on the item type var shape = deserializers[type].Deserialize(item); if (shape.GetType() == typeof(Circle)) { Debug.Assert(Comparer.Equal(circle, shape as Circle)); } if (shape.GetType() == typeof(Rectangle)) { Debug.Assert(Comparer.Equal(rectangle, shape as Rectangle)); } // Alternatively the generic method IBonded<T>.Deserialize<U> can be used if (type == Type.Circle) { var c = item.Deserialize<Circle>(); Debug.Assert(Comparer.Equal(circle, c)); } if (type == Type.Rectange) { var r = item.Deserialize<Rectangle>(); Debug.Assert(Comparer.Equal(rectangle, r)); } } }