Ejemplo n.º 1
0
        public void WritePolymorphicObject(DiscriminatorPolicy discriminatorPolicy, string expected)
        {
            JsonSerializerOptions options = new JsonSerializerOptions();

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

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

            BaseObjectHolder obj = new BaseObjectHolder
            {
                BaseObject = new NameObject
                {
                    Id   = 1,
                    Name = "foo"
                },
                NameObject = new NameObject
                {
                    Id   = 2,
                    Name = "bar"
                }
            };

            string actual = JsonSerializer.Serialize(obj, options);

            Assert.Equal(expected, actual);
        }
        public void ReadWithCustomDiscriminator()
        {
            JsonSerializerOptions options = new JsonSerializerOptions();

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

            registry.RegisterConvention(new CustomDiscriminatorConvention());
            registry.RegisterType(typeof(NameObject));

            const string     json = @"{""BaseObject"":{""type"":571690115,""Name"":""foo"",""Id"":1}}";
            BaseObjectHolder obj  = JsonSerializer.Deserialize <BaseObjectHolder>(json, options);

            Assert.NotNull(obj);
            Assert.IsType <NameObject>(obj.BaseObject);
            Assert.Equal("foo", ((NameObject)obj.BaseObject).Name);
            Assert.Equal(1, obj.BaseObject.Id);
        }
        public void ReadPolymorphicObject()
        {
            const string json = @"{""BaseObject"":{""$type"":12,""Name"":""foo"",""Id"":1}}";

            JsonSerializerOptions options = new JsonSerializerOptions();

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

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

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

            Assert.NotNull(obj);
            Assert.IsType <NameObject>(obj.BaseObject);
            Assert.Equal("foo", ((NameObject)obj.BaseObject).Name);
            Assert.Equal(1, obj.BaseObject.Id);
        }
Ejemplo n.º 4
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);
        }
        public void WriteWithCustomDiscriminator()
        {
            JsonSerializerOptions options = new JsonSerializerOptions();

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

            registry.RegisterConvention(new CustomDiscriminatorConvention());
            registry.RegisterType(typeof(NameObject));

            BaseObjectHolder obj = new BaseObjectHolder
            {
                BaseObject = new NameObject
                {
                    Id   = 1,
                    Name = "foo"
                },
            };

            string       actual   = JsonSerializer.Serialize(obj, options);
            const string expected = @"{""BaseObject"":{""type"":571690115,""Name"":""foo"",""Id"":1},""NameObject"":null}";

            Assert.Equal(expected, actual);
        }