public void TypeFilterShouldThrowOnNaughtyType()
        {
            var typeFilter = TypeFilterBuilder.Create()
                             .Include <ClassA>()
                             .Include <ClassB>()
                             .Build();

            var options = SerializerOptions.Default
                          .WithTypeFilter(typeFilter);

            var serializer   = new Serializer(SerializerOptions.Default.WithDisallowUnsafeType(false));
            var deserializer = new Serializer(options);

            using (var stream = new MemoryStream())
            {
                serializer.Serialize(new ClassA(), stream);
                stream.Position = 0;
                Action act = () => deserializer.Deserialize <ClassA>(stream);
                act.Should().NotThrow();

                stream.Position = 0;
                Action actObj = () => deserializer.Deserialize <object>(stream);
                actObj.Should().NotThrow();
            }

            using (var stream = new MemoryStream())
            {
                serializer.Serialize(new ClassB(), stream);
                stream.Position = 0;
                Action act = () => deserializer.Deserialize <ClassB>(stream);
                act.Should().NotThrow();

                stream.Position = 0;
                Action actObj = () => deserializer.Deserialize <object>(stream);
                actObj.Should().NotThrow();
            }

            using (var stream = new MemoryStream())
            {
                serializer.Serialize(new ClassC(), stream);
                stream.Position = 0;
                Action act = () => deserializer.Deserialize <ClassC>(stream);
                act.Should().Throw <UserEvilDeserializationException>();

                stream.Position = 0;
                Action actObj = () => deserializer.Deserialize <object>(stream);
                actObj.Should().Throw <UserEvilDeserializationException>();
            }
        }
        protected override void Init()
        {
            base.Init();

            var filteredOptions = SerializerOptions.Default
                                  .WithTypeFilter(
                TypeFilterBuilder.Create()
                .Include <StandardStruct>()
                .Include <BlittableStruct>()
                .Include <TestEnum>()
                .Build());

            _filteredSerializer = new Serializer(filteredOptions);

            standardValue  = new StandardStruct(1, "John", "Doe", isLoggedIn: false);
            blittableValue = new BlittableStruct(59, 92);
            testEnum       = TestEnum.HatesAll;
        }
Beispiel #3
0
        protected override void Init()
        {
            var baseOptions = new SerializerOptions(preserveObjectReferences: true);

            Serializer = new Serializer(baseOptions);

            var filteredOptions = baseOptions
                                  .WithTypeFilter(
                TypeFilterBuilder.Create()
                .Include <CyclicClassA>()
                .Include <CyclicClassB>()
                .Include <VirtualTestClass>()
                .Include <LargeSealedClass>()
                .Include <GenericClass <int, string, bool, DateTime, Guid> >()
                .Include <TestEnum>()
                .Build());

            _filteredSerializer = new Serializer(filteredOptions);

            var a = new CyclicClassA();
            var b = new CyclicClassB();

            a.B    = b;
            b.A    = a;
            cyclic = a;

            virtualObject = new VirtualTestClass
            {
                DateProp   = DateTime.Now,
                GuidProp   = Guid.NewGuid(),
                IntProp    = 812342354,
                StringProp = new string('x', 30)
            };

            sealedObject = LargeSealedClass.MakeRandom();

            genericObject = new GenericClass <int, string, bool, DateTime, Guid>(123, "hello-world", true, DateTime.Now, Guid.NewGuid());
        }