Exemplo n.º 1
0
        public void WriteWithNoDiscriminatorConvention()
        {
            JsonSerializerOptions options = new JsonSerializerOptions();

            options.SetupExtensions();
            DiscriminatorConventionRegistry registry = options.GetDiscriminatorConventionRegistry();

            registry.ClearConventions();

            NameObject obj = new NameObject
            {
                Id   = 12,
                Name = "foo"
            };

            string       actual   = JsonSerializer.Serialize <BaseObject>(obj, options);
            const string expected = @"{""Name"":""foo"",""Id"":12}";

            Assert.Equal(expected, actual);
        }
Exemplo n.º 2
0
        public void ReadPolymorphicObjectWithDeferredTypeProperty()
        {
            const string json = @"{""BaseObject"":{""Name"":""foo"",""Id"":1,""Unknown"":{""Prop1"":12,""Prop2"":""bar""},""$type"":12}}";

            JsonSerializerOptions options = new JsonSerializerOptions();

            options.SetupExtensions();
            DiscriminatorConventionRegistry registry = options.GetDiscriminatorConventionRegistry();

            registry.ClearConventions();
            registry.RegisterConvention(new DefaultDiscriminatorConvention <int>(options));
            registry.RegisterType <NameObject>();

            BaseObjectHolder obj = JsonSerializer.Deserialize <BaseObjectHolder>(json, options);

            Assert.NotNull(obj);
            NameObject nameObject = Assert.IsType <NameObject>(obj.BaseObject);

            Assert.Equal("foo", nameObject.Name);
            Assert.Equal(1, nameObject.Id);
        }