Exemplo n.º 1
0
        public void SerializeClassTest()
        {
            var guid = Guid.NewGuid();

            Console.WriteLine(guid);
            ParentUnionType subUnionType1 = new SubUnionType1 {
                MyProperty = guid, MyProperty1 = 20
            };
            var bytes = JsonSerializer.Serialize(subUnionType1);

            Assert.Throws <InvalidOperationException>(() => JsonSerializer.Deserialize <ParentUnionType>(bytes));
            var newSubUnionType = JsonSerializer.Deserialize <SubUnionType1>(bytes);

            Assert.NotNull(newSubUnionType);
            Assert.IsType <SubUnionType1>(newSubUnionType);
            Assert.Equal(guid, newSubUnionType.MyProperty);
            Assert.Equal(0, newSubUnionType.MyProperty1); // 说明 MyProperty1 并没有序列化

            bytes = Utf8JsonMessageFormatter.DefaultInstance.Serialize(subUnionType1);
            var newSubUnionType1 = Utf8JsonMessageFormatter.DefaultInstance.Deserialize <SubUnionType1>(bytes);

            Assert.Equal(guid, newSubUnionType1.MyProperty); Assert.Equal(20, newSubUnionType1.MyProperty1);

            bytes            = Utf8JsonMessageFormatter.DefaultInstance.SerializeObject(subUnionType1);
            newSubUnionType1 = (SubUnionType1)Utf8JsonMessageFormatter.DefaultInstance.Deserialize(typeof(SubUnionType1), bytes);
            Assert.Equal(guid, newSubUnionType1.MyProperty); Assert.Equal(20, newSubUnionType1.MyProperty1);
            newSubUnionType1 = (SubUnionType1)Utf8JsonMessageFormatter.DefaultInstance.Deserialize <ParentUnionType>(typeof(SubUnionType1), bytes);
            Assert.Equal(guid, newSubUnionType1.MyProperty); Assert.Equal(20, newSubUnionType1.MyProperty1);

            var copy = (SubUnionType1)Utf8JsonMessageFormatter.DefaultInstance.DeepCopy(subUnionType1);

            Assert.Equal(guid, copy.MyProperty); Assert.Equal(20, copy.MyProperty1);
        }
Exemplo n.º 2
0
        public void SerializeClassTest()
        {
            var guid = Guid.NewGuid();

            Console.WriteLine(guid);
            ParentUnionType subUnionType1 = new SubUnionType1 {
                MyProperty = guid, MyProperty1 = 20
            };
            var bytes           = MessagePackSerializer.Serialize(subUnionType1);
            var newSubUnionType = MessagePackSerializer.Deserialize <ParentUnionType>(bytes);

            Assert.NotNull(newSubUnionType);
            Assert.IsType <SubUnionType1>(newSubUnionType);
            Assert.Equal(guid, newSubUnionType.MyProperty);

            // 实际应用中,无法确定消费者是按哪个类型(ParentUnionType or SubUnionType1)来反序列化的
            Assert.Throws <InvalidOperationException>(() => MessagePackSerializer.Deserialize <SubUnionType1>(bytes));

            bytes = MessagePackMessageFormatter.DefaultInstance.Serialize(subUnionType1);
            var newSubUnionType1 = MessagePackMessageFormatter.DefaultInstance.Deserialize <SubUnionType1>(bytes);

            Assert.Equal(guid, newSubUnionType1.MyProperty); Assert.Equal(20, newSubUnionType1.MyProperty1);

            bytes            = MessagePackMessageFormatter.DefaultInstance.SerializeObject(subUnionType1);
            newSubUnionType1 = (SubUnionType1)MessagePackMessageFormatter.DefaultInstance.Deserialize(typeof(SubUnionType1), bytes);
            Assert.Equal(guid, newSubUnionType1.MyProperty); Assert.Equal(20, newSubUnionType1.MyProperty1);
            newSubUnionType1 = (SubUnionType1)MessagePackMessageFormatter.DefaultInstance.Deserialize <ParentUnionType>(typeof(SubUnionType1), bytes);
            Assert.Equal(guid, newSubUnionType1.MyProperty); Assert.Equal(20, newSubUnionType1.MyProperty1);

            bytes            = TypelessMessagePackMessageFormatter.DefaultInstance.Serialize(subUnionType1);
            newSubUnionType1 = TypelessMessagePackMessageFormatter.DefaultInstance.Deserialize <SubUnionType1>(bytes);
            Assert.Equal(guid, newSubUnionType1.MyProperty); Assert.Equal(20, newSubUnionType1.MyProperty1);

            bytes            = TypelessMessagePackMessageFormatter.DefaultInstance.SerializeObject(subUnionType1);
            newSubUnionType1 = (SubUnionType1)TypelessMessagePackMessageFormatter.DefaultInstance.Deserialize(typeof(SubUnionType1), bytes);
            Assert.Equal(guid, newSubUnionType1.MyProperty); Assert.Equal(20, newSubUnionType1.MyProperty1);
            newSubUnionType1 = (SubUnionType1)TypelessMessagePackMessageFormatter.DefaultInstance.Deserialize <ParentUnionType>(typeof(SubUnionType1), bytes);
            Assert.Equal(guid, newSubUnionType1.MyProperty); Assert.Equal(20, newSubUnionType1.MyProperty1);

            var copy = (SubUnionType1)MessagePackMessageFormatter.DefaultInstance.DeepCopy(subUnionType1);

            Assert.Equal(guid, copy.MyProperty); Assert.Equal(20, copy.MyProperty1);
            copy = (SubUnionType1)LZ4MessagePackMessageFormatter.DefaultInstance.DeepCopy(subUnionType1);
            Assert.Equal(guid, copy.MyProperty); Assert.Equal(20, copy.MyProperty1);
            copy = (SubUnionType1)TypelessMessagePackMessageFormatter.DefaultInstance.DeepCopy(subUnionType1);
            Assert.Equal(guid, copy.MyProperty); Assert.Equal(20, copy.MyProperty1);
            copy = (SubUnionType1)LZ4TypelessMessagePackMessageFormatter.DefaultInstance.DeepCopy(subUnionType1);
            Assert.Equal(guid, copy.MyProperty); Assert.Equal(20, copy.MyProperty1);
        }