예제 #1
0
        public static void EnableIgnoreNullValuesOnWrite()
        {
            JsonSerializerOptions options = new JsonSerializerOptions();

            options.IgnoreNullValues = true;

            var obj = new TestClassWithInitializedProperties
            {
                MyString          = null,
                MyInt             = null,
                MyDateTime        = null,
                MyIntArray        = null,
                MyIntList         = null,
                MyNullableIntList = null,
                MyObjectList      = new List <object> {
                    null
                },
                MyListList = new List <List <object> > {
                    new List <object> {
                        null
                    }
                },
                MyDictionaryList = new List <Dictionary <string, string> > {
                    new Dictionary <string, string>()
                    {
                        ["key"] = null
                    }
                },
                MyStringDictionary = new Dictionary <string, string>()
                {
                    ["key"] = null
                },
                MyNullableDateTimeDictionary = new Dictionary <string, DateTime?>()
                {
                    ["key"] = null
                },
                MyObjectDictionary = new Dictionary <string, object>()
                {
                    ["key"] = null
                },
                MyStringDictionaryDictionary = new Dictionary <string, Dictionary <string, string> >()
                {
                    ["key"] = new Dictionary <string, string>()
                    {
                        ["key"] = null
                    }
                },
                MyListDictionary = new Dictionary <string, List <object> >()
                {
                    ["key"] = new List <object> {
                        null
                    }
                },
                MyObjectDictionaryDictionary = new Dictionary <string, Dictionary <string, object> >()
                {
                    ["key"] = new Dictionary <string, object>()
                    {
                        ["key"] = null
                    }
                }
            };

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

            // Roundtrip to verify serialize is accurate.
            TestClassWithInitializedProperties newObj = JsonSerializer.Deserialize <TestClassWithInitializedProperties>(json);

            Assert.Equal("Hello", newObj.MyString);
            Assert.Equal(1, newObj.MyInt);
            Assert.Equal(new DateTime(1995, 4, 16), newObj.MyDateTime);
            Assert.Equal(1, newObj.MyIntArray[0]);
            Assert.Equal(1, newObj.MyIntList[0]);
            Assert.Equal(1, newObj.MyNullableIntList[0]);

            Assert.Null(newObj.MyObjectList[0]);
            Assert.Null(newObj.MyObjectList[0]);
            Assert.Null(newObj.MyListList[0][0]);
            Assert.Null(newObj.MyDictionaryList[0]["key"]);
            Assert.Null(newObj.MyStringDictionary["key"]);
            Assert.Null(newObj.MyNullableDateTimeDictionary["key"]);
            Assert.Null(newObj.MyObjectDictionary["key"]);
            Assert.Null(newObj.MyStringDictionaryDictionary["key"]["key"]);
            Assert.Null(newObj.MyListDictionary["key"][0]);
            Assert.Null(newObj.MyObjectDictionaryDictionary["key"]["key"]);

            var parentObj = new WrapperForTestClassWithInitializedProperties
            {
                MyClass = obj
            };

            json = JsonSerializer.Serialize(parentObj, options);

            // Roundtrip to ensure serialize is accurate.
            WrapperForTestClassWithInitializedProperties newParentObj = JsonSerializer.Deserialize <WrapperForTestClassWithInitializedProperties>(json);
            TestClassWithInitializedProperties           nestedObj    = newParentObj.MyClass;

            Assert.Equal("Hello", nestedObj.MyString);
            Assert.Equal(1, nestedObj.MyInt);
            Assert.Equal(new DateTime(1995, 4, 16), nestedObj.MyDateTime);
            Assert.Equal(1, nestedObj.MyIntArray[0]);
            Assert.Equal(1, nestedObj.MyIntList[0]);
            Assert.Equal(1, nestedObj.MyNullableIntList[0]);

            Assert.Null(nestedObj.MyObjectList[0]);
            Assert.Null(nestedObj.MyObjectList[0]);
            Assert.Null(nestedObj.MyListList[0][0]);
            Assert.Null(nestedObj.MyDictionaryList[0]["key"]);
            Assert.Null(nestedObj.MyStringDictionary["key"]);
            Assert.Null(nestedObj.MyNullableDateTimeDictionary["key"]);
            Assert.Null(nestedObj.MyObjectDictionary["key"]);
            Assert.Null(nestedObj.MyStringDictionaryDictionary["key"]["key"]);
            Assert.Null(nestedObj.MyListDictionary["key"][0]);
            Assert.Null(nestedObj.MyObjectDictionaryDictionary["key"]["key"]);
        }
예제 #2
0
        public static void EnableIgnoreNullValuesOnWrite()
        {
            JsonSerializerOptions options = new JsonSerializerOptions();

            options.IgnoreNullValues = true;

            var obj = new TestClassWithInitializedProperties
            {
                MyString     = null,
                MyInt        = null,
                MyIntArray   = null,
                MyIntList    = null,
                MyObjectList = new List <object> {
                    null
                },
                MyListList = new List <List <object> > {
                    new List <object> {
                        null
                    }
                },
                MyDictionaryList = new List <Dictionary <string, string> > {
                    new Dictionary <string, string>()
                    {
                        ["key"] = null
                    }
                },
                MyStringDictionary = new Dictionary <string, string>()
                {
                    ["key"] = null
                },
                MyObjectDictionary = new Dictionary <string, object>()
                {
                    ["key"] = null
                },
                MyStringDictionaryDictionary = new Dictionary <string, Dictionary <string, string> >()
                {
                    ["key"] = null
                },
                MyListDictionary = new Dictionary <string, List <object> >()
                {
                    ["key"] = null
                },
                MyObjectDictionaryDictionary = new Dictionary <string, Dictionary <string, object> >()
                {
                    ["key"] = null
                }
            };

            string expectedJson =
                @"{" +
                @"""MyObjectList"":[null]," +
                @"""MyListList"":[[null]]," +
                @"""MyDictionaryList"":[" +
                @"{" +
                @"""key"":null" +
                @"}" +
                @"]," +
                @"""MyStringDictionary"":{" +
                @"""key"":null" +
                @"}," +
                @"""MyObjectDictionary"":{" +
                @"""key"":null" +
                @"}," +
                @"""MyStringDictionaryDictionary"":{" +
                @"""key"":null" +
                @"}," +
                @"""MyListDictionary"":{" +
                @"""key"":null" +
                @"}," +
                @"""MyObjectDictionaryDictionary"":{" +
                @"""key"":null" +
                @"}" +
                @"}";

            Assert.Equal(expectedJson, JsonSerializer.Serialize(obj, options));

            var parentObj = new WrapperForTestClassWithInitializedProperties
            {
                MyClass = obj
            };

            expectedJson = @"{""MyClass"":" + expectedJson + "}";
            Assert.Equal(expectedJson, JsonSerializer.Serialize(parentObj, options));
        }