예제 #1
0
        public static void DefaultReadValue()
        {
            TestClassWithNullButInitialized obj = JsonSerializer.Parse <TestClassWithNullButInitialized>(TestClassWithNullButInitialized.s_json);

            Assert.Equal(null, obj.MyString);
            Assert.Equal(null, obj.MyInt);
        }
예제 #2
0
        public static void OverrideReadOnOption()
        {
            var options = new JsonSerializerOptions();

            options.IgnoreNullPropertyValueOnRead = true;

            TestClassWithNullButInitialized obj = JsonSerializer.Parse <TestClassWithNullButInitialized>(TestClassWithNullButInitialized.s_json, options);

            Assert.Equal("Hello", obj.MyString);
            Assert.Equal(1, obj.MyInt);
        }
예제 #3
0
        public static void OverrideReadOnAttribute()
        {
            JsonSerializerOptions options = new JsonSerializerOptions();
            JsonPropertyAttribute attr    = new JsonPropertyAttribute();

            attr.SkipNullValuesOnRead = true;
            options.AddAttribute(typeof(TestClassWithNullButInitialized), attr);

            TestClassWithNullButInitialized obj = JsonSerializer.ReadString <TestClassWithNullButInitialized>(TestClassWithNullButInitialized.s_json, options);

            Assert.Equal("Hello", obj.MyString);
            Assert.Equal(1, obj.MyInt);
        }