Exemplo n.º 1
0
        public void ScopeTestss()
        {
            Base b = new Derived
            {
                Id  = 10,
                Age = 12
            };


            using (var config = new SerializerConfig())
            {
                using (var stream = CreateStream())
                {
                    using (var serializer = config.Create())
                    {
                        serializer.Serialize(stream, b);
                        stream.Seek(0);
                        var result = serializer.Deserialize <Base>(stream);

                        Assert.IsType <Base>(result);

                        Assert.Equal(10, result.Id);
                    }
                }

                using (var scope = config.CreateScope())
                {
                    scope.Match <Derived>();

                    using (var stream = CreateStream())
                    {
                        using (var serializer = scope.Create())
                        {
                            serializer.Serialize(stream, b);
                            stream.Seek(0);
                            var result = serializer.Deserialize <Base>(stream);

                            Assert.IsType <Derived>(result);
                        }
                    }
                }

                using (var stream = CreateStream())
                {
                    using (var serializer = config.Create())
                    {
                        serializer.Serialize(stream, b);
                        stream.Seek(0);
                        var result = serializer.Deserialize <Base>(stream);

                        Assert.IsType <Base>(result);
                    }
                }
            }
        }
Exemplo n.º 2
0
        public void NullInArrayRestores()
        {
            using (var config = new SerializerConfig())
            {
                config.Match <EntityForNull>();

                var input = new EntityForNull[]
                {
                    new EntityForNull(),
                    null,
                    new EntityForNull()
                };

                using (var serializer = config.Create())
                {
                    using (var stream = CreateStream())
                    {
                        serializer.Serialize(stream, input);
                        stream.Seek(0);
                        var result = serializer.Deserialize <EntityForNull[]>(stream);

                        Assert.Equal(3, result.Length);
                        Assert.NotNull(result[0]);
                        Assert.Null(result[1]);
                        Assert.NotNull(result[2]);
                    }
                }
            }
        }
Exemplo n.º 3
0
        public void ComplexTypeSerializing()
        {
            var config = new SerializerConfig();

            var serializer = config.Create();

            using (var stream = CreateStream())
            {
                var input = new[]
                {
                    new SimplePerson
                    {
                        Name = "Person1Name",
                        Age  = 12
                    },
                    new SimplePerson
                    {
                        Name = "Person2Name",
                        Age  = 24
                    }
                };

                serializer.Serialize(stream, input);
                stream.Seek(0);
                var result = serializer.Deserialize <SimplePerson[]>(stream);

                Assert.Equal(2, result.Length);

                for (var i = 0; i < input.Length; i++)
                {
                    Assert.Equal(input[i].Name, result[i].Name);
                    Assert.Equal(input[i].Age, result[i].Age);
                }
            }
        }
Exemplo n.º 4
0
        public void ExtendedPersonInBasePersonArraySerializing()
        {
            var config = new SerializerConfig();

            var serializer = config.Create();

            var input = new[]
            {
                new B
                {
                    Prop1 = 11,
                    Prop2 = 22
                },
                new A
                {
                    Prop1 = 33
                }
            };

            using (var stream = CreateStream())
            {
                serializer.Serialize(stream, input);
                stream.Seek(0);
                var result = serializer.Deserialize <A[]>(stream);

                var a1 = Assert.IsType <A>(result[0]);
                Assert.Equal(11, a1.Prop1);

                var a2 = Assert.IsType <A>(result[1]);
                Assert.Equal(33, a2.Prop1);
            }
        }
Exemplo n.º 5
0
        public void ExtendedPersonAsBasePersonTypeListSerializing()
        {
            var config = new SerializerConfig();

            config.Match <BasePerson>();
            config.Match <ExtendedPerson>();

            var serializer = config.Create();

            IList <BasePerson> input = new List <BasePerson>
            {
                new BasePerson
                {
                    Name = "Simon",
                    Age  = 27
                },
                new ExtendedPerson
                {
                    Name    = "Maria",
                    Age     = 22,
                    Address = "City, Street",
                    House   = 5,
                    Floor   = 3
                },
                new BasePerson
                {
                    Name = "Michael",
                    Age  = 44
                }
            };

            using (var stream = CreateStream())
            {
                serializer.Serialize(stream, input);
                stream.Seek(0);
                var result = serializer.Deserialize <IList <BasePerson> >(stream);

                Assert.Equal(3, result.Count);

                var simon = result[0];
                Assert.IsType <BasePerson>(simon);
                Assert.Equal("Simon", simon.Name);
                Assert.Equal(27, simon.Age);

                var maria = result[1] as ExtendedPerson;
                Assert.NotNull(maria);
                Assert.Equal("Maria", maria.Name);
                Assert.Equal(22, maria.Age);
                Assert.Equal("City, Street", maria.Address);
                Assert.Equal(5, maria.House);
                Assert.Equal(3, maria.Floor);

                var michael = result[2];
                Assert.IsType <BasePerson>(michael);
                Assert.Equal("Michael", michael.Name);
                Assert.Equal(44, michael.Age);
            }
        }
Exemplo n.º 6
0
 public void UInt64MaxValueSerializing()
 {
     using (var config = new SerializerConfig())
     {
         using (var serializer = config.Create())
         {
             using (var stream = CreateStream())
             {
                 serializer.Serialize(stream, ulong.MaxValue);
                 stream.Seek(0);
                 var result = serializer.Deserialize <ulong>(stream);
                 Assert.Equal(ulong.MaxValue, result);
             }
         }
     }
 }
Exemplo n.º 7
0
        public void DerivedClassInBaseClassArrayInClassHolderSerializing()
        {
            var config = new SerializerConfig();

            config.Match <DerivedClass>();

            var serializer = config.Create();

            var input = new ClassHolder
            {
                BaseClasses = new BaseClass[]
                {
                    new BaseClass
                    {
                        Name = "Base",
                        Age  = 11
                    },
                    new DerivedClass
                    {
                        Name  = "Derived",
                        Age   = 22,
                        House = 33
                    }
                }
            };

            using (var stream = CreateStream())
            {
                serializer.Serialize(stream, input);
                stream.Seek(0);
                var result = serializer.Deserialize <ClassHolder>(stream);

                Assert.NotNull(result.BaseClasses);
                Assert.Equal(2, result.BaseClasses.Length);

                var baseClass = result.BaseClasses[0];
                Assert.Equal("Base", baseClass.Name);
                Assert.Equal(11, baseClass.Age);

                var derivedClass = result.BaseClasses[1] as DerivedClass;
                Assert.NotNull(derivedClass);

                Assert.Equal("Derived", derivedClass.Name);
                Assert.Equal(22, derivedClass.Age);
                Assert.Equal(33, derivedClass.House);
            }
        }
Exemplo n.º 8
0
        public void NullRestores()
        {
            using (var config = new SerializerConfig())
            {
                config.Match <EntityForNull>();

                using (var serializer = config.Create())
                {
                    using (var stream = CreateStream())
                    {
                        serializer.Serialize(stream, (EntityForNull)null);
                        stream.Seek(0);
                        var result = serializer.Deserialize <EntityForNull>(stream);
                        Assert.Null(result);
                    }
                }
            }
        }
Exemplo n.º 9
0
        public void GuidSerializing()
        {
            var input = Guid.NewGuid();

            using (var config = new SerializerConfig())
            {
                using (var serializer = config.Create())
                {
                    using (var stream = CreateStream())
                    {
                        serializer.Serialize(stream, input);
                        stream.Seek(0);
                        var result = serializer.Deserialize <Guid>(stream);

                        Assert.Equal(input, result);
                    }
                }
            }
        }
Exemplo n.º 10
0
        static void Main(string[] args)
        {
            Test[] test = new Test[] { new Test3 {
                                           Prop = false, Prop2 = 123
                                       }, new Test2 {
                                           Prop = false, Prop2 = 12
                                       }, new Test3() };

            var config = new SerializerConfig();

            config.Match <Test>();
            config.Match <Test3>();

            var serializer = config.Create();

            using (var stream = new ValueStream())
            {
                serializer.Serialize(stream, test);
                stream.Seek(0);
                var instance = serializer.Deserialize <Test[]>(stream);
            }
        }
Exemplo n.º 11
0
        public void NullHolderWithNullSerializing()
        {
            var input = new NullHolder
            {
                Null = null
            };

            using (var config = new SerializerConfig())
            {
                using (var serializer = config.Create())
                {
                    using (var stream = CreateStream())
                    {
                        serializer.Serialize(stream, input);
                        stream.Seek(0);
                        var result = serializer.Deserialize <NullHolder>(stream);

                        Assert.NotNull(result);
                        Assert.Null(result.Null);
                    }
                }
            }
        }
Exemplo n.º 12
0
        public void SimplePersonListSerializing()
        {
            var config = new SerializerConfig();

            config.Match <SimplePerson>();

            var serializer = config.Create();

            using (var stream = CreateStream())
            {
                IList <SimplePerson> input = new List <SimplePerson>
                {
                    new SimplePerson
                    {
                        Name = "Person1Name",
                        Age  = 12
                    },
                    new SimplePerson
                    {
                        Name = "Person2Name",
                        Age  = 24
                    }
                };

                serializer.Serialize(stream, input);
                stream.Seek(0);
                var result = serializer.Deserialize <IList <SimplePerson> >(stream);

                Assert.Equal(2, result.Count);

                for (int i = 0; i < input.Count; i++)
                {
                    Assert.Equal(input[i].Name, result[i].Name);
                    Assert.Equal(input[i].Age, result[i].Age);
                }
            }
        }
Exemplo n.º 13
0
        public void ComplexPersonSerializing()
        {
            var config = new SerializerConfig();

            var serializer = config.Create();

            var input = new[]
            {
                new ComplexPerson
                {
                    Name     = "Sarah",
                    Age      = 37,
                    Children = new[]
                    {
                        new ComplexPerson
                        {
                            Name     = "John",
                            Age      = 14,
                            Children = new ComplexPerson[0]
                        }
                    }
                },
                new ComplexPerson
                {
                    Name     = "Julia",
                    Age      = 42,
                    Children = new[]
                    {
                        new ComplexPerson
                        {
                            Name     = "Sandra",
                            Age      = 20,
                            Children = null
                        }
                    }
                },
                new ComplexPerson
                {
                    Name     = "Sam",
                    Age      = 14,
                    Children = null
                }
            };

            using (var stream = CreateStream())
            {
                serializer.Serialize(stream, input);
                stream.Seek(0);
                var result = serializer.Deserialize <ComplexPerson[]>(stream);

                Assert.Equal(3, result.Length);

                var sarah = result[0];
                Assert.Equal("Sarah", sarah.Name);
                Assert.Equal(37, sarah.Age);
                Assert.Single(sarah.Children);

                var john = sarah.Children[0];
                Assert.Equal("John", john.Name);
                Assert.Equal(14, john.Age);
                Assert.Empty(john.Children);

                var julia = result[1];
                Assert.Equal("Julia", julia.Name);
                Assert.Equal(42, julia.Age);
                Assert.Single(julia.Children);

                var sandra = julia.Children[0];
                Assert.Equal("Sandra", sandra.Name);
                Assert.Equal(20, sandra.Age);
                Assert.Null(sandra.Children);

                var sam = result[2];
                Assert.Equal("Sam", sam.Name);
                Assert.Equal(14, sam.Age);
                Assert.Null(sam.Children);
            }
        }
        public void CustomTest()
        {
            var config = new SerializerConfig();

            config.Match <CustomTestDerivedRegisteredEntity>();
            config.Match <ListDerivedRegisteredEntity>();
            config.Match <ArrayBaseRegisteredEntity>();

            var serializer = config.Create();

            var input = new CustomTestHolder
            {
                BaseEntity = new CustomTestDerivedRegisteredEntity
                {
                    Id   = 11,
                    Name = "22"
                },
                ListBaseEntity = new List <ListBaseNotRegisteredEntity>
                {
                    new ListBaseNotRegisteredEntity
                    {
                        Name = "List1"
                    },
                    new ListDerivedRegisteredEntity
                    {
                        Name  = "Derived",
                        Email = "*****@*****.**"
                    },
                    new ListBaseNotRegisteredEntity
                    {
                        Name = "List2"
                    }
                },
                ArrayBaseEntity = new ArrayBaseRegisteredEntity[]
                {
                    new ArrayDerivedNotRegisteredEntity
                    {
                        Id   = 11,
                        Post = 22
                    },
                    new ArrayDerivedNotRegisteredEntity
                    {
                        Id   = 33,
                        Post = 44
                    },
                    new ArrayBaseRegisteredEntity
                    {
                        Id = 55
                    }
                }
            };

            using (var stream = CreateStream())
            {
                serializer.Serialize(stream, input);
                stream.Seek(0);
                var result = serializer.Deserialize <CustomTestHolder>(stream);

                Assert.NotNull(result);

                Assert.IsType <CustomTestDerivedRegisteredEntity>(result.BaseEntity);
                var derivedEntity = result.BaseEntity as CustomTestDerivedRegisteredEntity;
                Assert.Equal(11, derivedEntity.Id);
                Assert.Equal("22", derivedEntity.Name);

                Assert.Equal(3, result.ListBaseEntity.Count);
                var list1 = result.ListBaseEntity[0];
                Assert.IsType <ListBaseNotRegisteredEntity>(list1);
                Assert.Equal("List1", list1.Name);

                var list2 = result.ListBaseEntity[1] as ListDerivedRegisteredEntity;
                Assert.NotNull(list2);
                Assert.Equal("Derived", list2.Name);
                Assert.Equal("*****@*****.**", list2.Email);

                var list3 = result.ListBaseEntity[2];
                Assert.IsType <ListBaseNotRegisteredEntity>(list3);
                Assert.Equal("List2", list3.Name);

                Assert.Equal(3, result.ArrayBaseEntity.Length);

                var element1 = result.ArrayBaseEntity[0];
                Assert.IsType <ArrayBaseRegisteredEntity>(element1);
                Assert.Equal(11, element1.Id);

                var element2 = result.ArrayBaseEntity[1];
                Assert.IsType <ArrayBaseRegisteredEntity>(element2);
                Assert.Equal(33, element2.Id);

                var element3 = result.ArrayBaseEntity[2];
                Assert.IsType <ArrayBaseRegisteredEntity>(element3);
                Assert.Equal(55, element3.Id);
            }
        }