Ejemplo n.º 1
0
        public static void CustomAttributeOnTypeAndProperty()
        {
            const string json = @"{""Point1"":""1,2""}";

            ClassWithJsonConverterAttributeOverride point = JsonSerializer.Deserialize <ClassWithJsonConverterAttributeOverride>(json);

            // The property attribute overides the type attribute.
            Assert.Equal(101, point.Point1.X);
            Assert.Equal(102, point.Point1.Y);

            string jsonSerialized = JsonSerializer.Serialize(point);

            Assert.Equal(json, jsonSerialized);
        }
Ejemplo n.º 2
0
        public static void CustomAttributeOnPropertyAndRuntime()
        {
            const string json = @"{""Point1"":""1,2""}";

            var options = new JsonSerializerOptions();

            options.Converters.Add(new AttributedPointConverter(200));

            ClassWithJsonConverterAttributeOverride point = JsonSerializer.Deserialize <ClassWithJsonConverterAttributeOverride>(json);

            // The property attribute overides the runtime.
            Assert.Equal(101, point.Point1.X);
            Assert.Equal(102, point.Point1.Y);

            string jsonSerialized = JsonSerializer.Serialize(point);

            Assert.Equal(json, jsonSerialized);
        }