Avro序列化将数据结构反序列化为基本类型

时间:2015-10-06 13:32:53

标签: c# serialization avro

我想知道使用Avro序列化是否可以序列化类型并反序列化为基类型? 例如,如果我运行以下测试,我会得到arithmeticOperationOverflow异常:

    [DataContract(Name = "BaseMessage")]
    public class BaseMessage
    {
        [DataMember(Name = "Id")]
        public Guid Id { get; set; }

        [DataMember(Name = "Topic")]
        public string Topic { get; set; }
    }
    [DataContract(Name = "MyMessage")]
    public class MyMessage : BaseMessage
    {
        [DataMember(Name = "Aggregate", IsRequired=false)]
        public byte[] Aggregate { get; set; }
    }

    public class AvroSPec
    {
        [Fact]
        public void I_class_can_be_serializated_an_then_deserializated_to_its_base_type()
        {
            BaseMessage actual = null;
            var expected = new MyMessage
            {
                Id = Guid.NewGuid(),
                Topic = StringExtensions.RandomString(),
                Aggregate = Encoding.UTF8.GetBytes(StringExtensions.RandomString())//random
            };
            byte[] SerilizedStream = null;
            using (MemoryStream stream = new MemoryStream())
            {
                AvroSerializer.Create<MyMessage>().Serialize(stream, expected);
                SerilizedStream = stream.GetBuffer();
            }
            using (MemoryStream stream = new MemoryStream(SerilizedStream))
            {
                actual = AvroSerializer.Create<BaseMessage>().Deserialize(stream);
            }
            Assert.Equal(actual.Id, expected.Id);
            Assert.Equal(actual.Topic, expected.Topic);
        }
    }
}

提前致谢

0 个答案:

没有答案