Exemplo n.º 1
0
        public ObjectMapping <T> AutoMap()
        {
            IObjectMappingConvention convention = _options.GetObjectMappingConventionRegistry().Lookup <T>();

            convention.Apply(_options, this);
            return(this);
        }
Exemplo n.º 2
0
        public void Read()
        {
            JsonSerializerOptions options = new JsonSerializerOptions();

            options.SetupExtensions();
            options.GetObjectMappingConventionRegistry().RegisterProvider(
                new DiscriminatorObjectMappingConventionProvider(t => t == typeof(Foo))
                );
            options.GetDiscriminatorConventionRegistry().DiscriminatorPolicy = DiscriminatorPolicy.Always;

            Assert.Equal(@"{""$type"":""Foo"",""Id"":1}", JsonSerializer.Serialize(new Foo {
                Id = 1
            }, options));
            Assert.Equal(@"{""Id"":1}", JsonSerializer.Serialize(new Bar {
                Id = 1
            }, options));
        }
Exemplo n.º 3
0
        public void Test()
        {
            JsonSerializerOptions options = new JsonSerializerOptions();

            options.SetupExtensions();
            options.GetObjectMappingConventionRegistry().RegisterConvention(
                typeof(Employee), new SelectiveSerializer("FirstName,Email,Id"));

            Employee employee = new Employee
            {
                Id        = 12,
                FirstName = "John",
                LastName  = "Doe",
                Email     = "*****@*****.**"
            };

            const string expected = @"{""Id"":12,""FirstName"":""John"",""Email"":""*****@*****.**""}";
            string       actual   = JsonSerializer.Serialize(employee, options);

            Assert.Equal(expected, actual);
        }
Exemplo n.º 4
0
        public void Test()
        {
            JsonSerializerOptions options = new JsonSerializerOptions();

            options.SetupExtensions();
            options.GetObjectMappingConventionRegistry().RegisterProvider(
                new CustomObjectMappingConventionProvider
            {
                FieldNameChanges = new Dictionary <string, string>
                {
                    ["Name"] = "foo"
                }
            }
                );

            const string json = @"{""foo"":""bar"",""Id"":1}";
            Object       obj  = Helper.Read <Object>(json, options);

            Assert.NotNull(obj);
            Assert.Equal("bar", obj.Name);
            Assert.Equal(1, obj.Id);
        }
Exemplo n.º 5
0
        public void OptIn()
        {
            JsonSerializerOptions options = new JsonSerializerOptions();

            options.SetupExtensions();
            options.GetObjectMappingConventionRegistry().RegisterProvider(
                new OptInObjectMappingConventionProvider()
                );

            OptInObject1 obj1 = new OptInObject1 {
                Id = 12, Name = "foo"
            };
            const string json1 = @"{""Id"":12}";

            Helper.TestWrite(obj1, json1, options);

            OptInObject2 obj2 = new OptInObject2 {
                Id = 12, Name = "foo"
            };
            const string json2 = @"{""Name"":""foo""}";

            Helper.TestWrite(obj2, json2, options);
        }