Пример #1
0
        public static void EnableIgnoreNullValuesOnRead()
        {
            var options = new JsonSerializerOptions();

            options.IgnoreNullValues = true;

            TestClassWithInitializedProperties obj = JsonSerializer.Deserialize <TestClassWithInitializedProperties>(TestClassWithInitializedProperties.s_null_json, options);

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

            Assert.Null(obj.MyObjectList[0]);
            Assert.Null(obj.MyObjectList[0]);
            Assert.Null(obj.MyListList[0][0]);
            Assert.Null(obj.MyDictionaryList[0]["key"]);
            Assert.Null(obj.MyNullableDateTimeDictionary["key"]);
            Assert.Null(obj.MyStringDictionary["key"]);
            Assert.Null(obj.MyObjectDictionary["key"]);
            Assert.Null(obj.MyStringDictionaryDictionary["key"]["key"]);
            Assert.Null(obj.MyListDictionary["key"][0]);
            Assert.Null(obj.MyObjectDictionaryDictionary["key"]["key"]);
        }
Пример #2
0
        public static void DefaultIgnoreNullValuesOnRead()
        {
            TestClassWithInitializedProperties obj = JsonSerializer.Parse <TestClassWithInitializedProperties>(TestClassWithInitializedProperties.s_null_json);

            Assert.Equal(null, obj.MyString);
            Assert.Equal(null, obj.MyInt);
        }
Пример #3
0
        public static void DefaultIgnoreNullValuesOnWrite()
        {
            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 json = JsonSerializer.Serialize(obj);

            Assert.Contains(@"""MyString"":null", json);
            Assert.Contains(@"""MyInt"":null", json);
            Assert.Contains(@"""MyIntArray"":null", json);
            Assert.Contains(@"""MyIntList"":null", json);
            Assert.Contains(@"""MyObjectList"":[null],", json);
            Assert.Contains(@"""MyListList"":[[null]],", json);
            Assert.Contains(@"""MyDictionaryList"":[{""key"":null}],", json);
            Assert.Contains(@"""MyStringDictionary"":{""key"":null},", json);
            Assert.Contains(@"""MyObjectDictionary"":{""key"":null},", json);
            Assert.Contains(@"""MyStringDictionaryDictionary"":{""key"":null},", json);
            Assert.Contains(@"""MyListDictionary"":{""key"":null},", json);
            Assert.Contains(@"""MyObjectDictionaryDictionary"":{""key"":null}", json);
        }
Пример #4
0
        public static void DefaultIgnoreNullValuesOnRead()
        {
            TestClassWithInitializedProperties obj = JsonSerializer.Deserialize <TestClassWithInitializedProperties>(TestClassWithInitializedProperties.s_null_json);

            Assert.Null(obj.MyString);
            Assert.Null(obj.MyInt);
            Assert.Null(obj.MyIntArray);
            Assert.Null(obj.MyIntList);
        }
Пример #5
0
        public static void EnableIgnoreNullValuesOnRead()
        {
            var options = new JsonSerializerOptions();

            options.IgnoreNullValues = true;

            TestClassWithInitializedProperties obj = JsonSerializer.Parse <TestClassWithInitializedProperties>(TestClassWithInitializedProperties.s_null_json, options);

            Assert.Equal("Hello", obj.MyString);
            Assert.Equal(1, obj.MyInt);
        }
Пример #6
0
        public static void DefaultIgnoreNullValuesOnWrite()
        {
            var obj = new TestClassWithInitializedProperties();

            obj.MyString = null;
            obj.MyInt    = null;

            string json = JsonSerializer.ToString(obj);

            Assert.Contains(@"""MyString"":null", json);
            Assert.Contains(@"""MyInt"":null", json);
        }
Пример #7
0
        public static void EnableIgnoreNullValuesOnWrite()
        {
            JsonSerializerOptions options = new JsonSerializerOptions();

            options.IgnoreNullValues = true;

            var obj = new TestClassWithInitializedProperties();

            obj.MyString = null;
            obj.MyInt    = null;

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

            Assert.Equal(@"{}", json);
        }
Пример #8
0
        public static void DefaultIgnoreNullValuesOnRead()
        {
            TestClassWithInitializedProperties obj = JsonSerializer.Deserialize <TestClassWithInitializedProperties>(TestClassWithInitializedProperties.s_null_json);

            Assert.Null(obj.MyString);
            Assert.Null(obj.MyInt);
            Assert.Null(obj.MyIntArray);
            Assert.Null(obj.MyIntList);
            Assert.Null(obj.MyObjectList[0]);
            Assert.Null(obj.MyListList[0][0]);
            Assert.Null(obj.MyDictionaryList[0]["key"]);
            Assert.Null(obj.MyStringDictionary["key"]);
            Assert.Null(obj.MyObjectDictionary["key"]);
            Assert.Null(obj.MyStringDictionaryDictionary["key"]["key"]);
            Assert.Null(obj.MyListDictionary["key"][0]);
            Assert.Null(obj.MyObjectDictionaryDictionary["key"]["key"]);
        }
Пример #9
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"]);
        }
Пример #10
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));
        }