public void SelectedTypes() { var schema = new RecordSchema(nameof(EventContainer), new[] { new RecordField("event", new UnionSchema(new[] { new RecordSchema(nameof(OrderCreatedEvent), new[] { new RecordField("timestamp", new StringSchema()), new RecordField("total", new BytesSchema() { LogicalType = new DecimalLogicalType(5, 2) }) }), new RecordSchema(nameof(OrderCancelledEvent), new[] { new RecordField("timestamp", new StringSchema()) }) })) }); var codec = new BinaryCodec(); var resolver = new ReflectionResolver(); var deserializer = new BinaryDeserializerBuilder(BinaryDeserializerBuilder.CreateBinaryDeserializerCaseBuilders(codec) .Prepend(builder => new OrderDeserializerBuilderCase(resolver, codec, builder))) .BuildDeserializer <EventContainer>(schema); var serializer = new BinarySerializerBuilder(BinarySerializerBuilder.CreateBinarySerializerCaseBuilders(codec) .Prepend(builder => new OrderSerializerBuilderCase(resolver, codec, builder))) .BuildSerializer <EventContainer>(schema); var creation = new EventContainer { Event = new OrderCreatedEvent { Timestamp = DateTime.UtcNow, Total = 40M } }; var cancellation = new EventContainer { Event = new OrderCancelledEvent { Timestamp = DateTime.UtcNow } }; Assert.IsType <OrderCreatedEvent>(deserializer.Deserialize(serializer.Serialize(creation)).Event); Assert.IsType <OrderCancelledEvent>(deserializer.Deserialize(serializer.Serialize(cancellation)).Event); }
private static void RunTestForRecord() { Schema schema; using (var stream = new FileStream("./Schemas/Widget.avsc", FileMode.Open)) { var reader = new JsonSchemaReader(); schema = reader.Read(stream); } var serializer = new BinarySerializerBuilder() .BuildSerializer <Widget>(schema); var deserializer = new BinaryDeserializerBuilder() .BuildDeserializer <Widget>(schema); var widgetIn = new Widget { Id = 1010, Name = "One Thousand Ten", Cost = 3829.7401m, Lifetime = TimeSpan.FromMilliseconds(987654321), GlobalId = new Guid("21d45c13-76b1-459d-8571-ba0ad0fa27de"), CreatedAt = DateTime.UtcNow, CreatedAtLocal = DateTimeOffset.Now }; var bytes = serializer.Serialize(widgetIn); var widgetOut = deserializer.Deserialize(bytes); Console.WriteLine(); Console.WriteLine($"Serialized widget to {bytes.Length} bytes."); Console.WriteLine($"Deserialized widget {widgetOut.Id} (\"{widgetOut.Name}\")."); }
private static void RunTestForDateTimeOffset() { /* * var dtoIn = DateTimeOffset.Now; * var dtoStr = dtoIn.ToString("O"); * var dtoOut = DateTimeOffset.Parse(dtoStr); * * var loc = DateTime.Now; * var utc = loc.ToUniversalTime(); * * var dtoFromLocal = new DateTimeOffset(loc); * var dtoFromUtc = new DateTimeOffset(utc); * * Console.WriteLine((dtoFromUtc == dtoFromLocal) ? "==" : "!="); */ var schema = new StringSchema(); var serializer = new BinarySerializerBuilder() .BuildSerializer <DateTimeOffset>(schema); var deserializer = new BinaryDeserializerBuilder() .BuildDeserializer <DateTimeOffset>(schema); var dtoIn = DateTimeOffset.Now; var bytes = serializer.Serialize(dtoIn); var dtoOut = deserializer.Deserialize(bytes); Console.WriteLine(); Console.WriteLine($"Serialized: {dtoIn.ToString("O")}"); Console.WriteLine($"Deserialized: {dtoOut.ToString("O")}"); }
public void PartiallySelectedTypes() { var schema = new UnionSchema(new[] { new RecordSchema(nameof(OrderCancelledEvent), new[] { new RecordField("timestamp", new StringSchema()) }), new RecordSchema(nameof(OrderCreatedEvent), new[] { new RecordField("timestamp", new StringSchema()), new RecordField("total", new BytesSchema() { LogicalType = new DecimalLogicalType(5, 2) }) }) }); var codec = new BinaryCodec(); var resolver = new ReflectionResolver(); var deserializer = DeserializerBuilder.BuildDeserializer <OrderCreatedEvent>(schema); var serializer = new BinarySerializerBuilder(BinarySerializerBuilder.CreateBinarySerializerCaseBuilders(codec) .Prepend(builder => new OrderSerializerBuilderCase(resolver, codec, builder))) .BuildSerializer <OrderEvent>(schema); var value = new OrderCreatedEvent { Timestamp = DateTime.UtcNow, Total = 40M }; var result = deserializer.Deserialize(serializer.Serialize(value)); Assert.Equal(value.Timestamp, result.Timestamp); Assert.Equal(value.Total, result.Total); }