public static void WriteClassWithNullKeyValuePairValues_NullWrittenAsEmptyObject()
        {
            var value = new SimpleClassWithKeyValuePairs()
            {
                KvpWStrVal      = new KeyValuePair <string, string>("key", null),
                KvpWObjVal      = new KeyValuePair <string, object>("key", null),
                KvpWClassVal    = new KeyValuePair <string, SimpleClassWithKeyValuePairs>("key", null),
                KvpWStrKvpVal   = new KeyValuePair <string, KeyValuePair <string, string> >("key", new KeyValuePair <string, string>("key", null)),
                KvpWObjKvpVal   = new KeyValuePair <string, KeyValuePair <string, object> >("key", new KeyValuePair <string, object>("key", null)),
                KvpWClassKvpVal = new KeyValuePair <string, KeyValuePair <string, SimpleClassWithKeyValuePairs> >("key", new KeyValuePair <string, SimpleClassWithKeyValuePairs>("key", null)),
            };

            string result = JsonSerializer.Serialize(value);

            // Roundtrip to ensure serialize was correct.
            value = JsonSerializer.Deserialize <SimpleClassWithKeyValuePairs>(result);
            Assert.Equal("key", value.KvpWStrVal.Key);
            Assert.Equal("key", value.KvpWObjVal.Key);
            Assert.Equal("key", value.KvpWClassVal.Key);
            Assert.Equal("key", value.KvpWStrKvpVal.Key);
            Assert.Equal("key", value.KvpWObjKvpVal.Key);
            Assert.Equal("key", value.KvpWClassKvpVal.Key);
            Assert.Equal("key", value.KvpWStrKvpVal.Value.Key);
            Assert.Equal("key", value.KvpWObjKvpVal.Value.Key);
            Assert.Equal("key", value.KvpWClassKvpVal.Value.Key);

            Assert.Null(value.KvpWStrVal.Value);
            Assert.Null(value.KvpWObjVal.Value);
            Assert.Null(value.KvpWClassVal.Value);
            Assert.Null(value.KvpWStrKvpVal.Value.Value);
            Assert.Null(value.KvpWObjKvpVal.Value.Value);
            Assert.Null(value.KvpWClassKvpVal.Value.Value);
        }
        public static void ReadClassWithNullKeyValuePairValues()
        {
            string json =
                @"{" +
                @"""KvpWStrVal"":{" +
                @"""Key"":""key""," +
                @"""Value"":null" +
                @"}," +
                @"""KvpWObjVal"":{" +
                @"""Key"":""key""," +
                @"""Value"":null" +
                @"}," +
                @"""KvpWClassVal"":{" +
                @"""Key"":""key""," +
                @"""Value"":null" +
                @"}," +
                @"""KvpWStrKvpVal"":{" +
                @"""Key"":""key""," +
                @"""Value"":{" +
                @"""Key"":""key""," +
                @"""Value"":null" +
                @"}" +
                @"}," +
                @"""KvpWObjKvpVal"":{" +
                @"""Key"":""key""," +
                @"""Value"":{" +
                @"""Key"":""key""," +
                @"""Value"":null" +
                @"}" +
                @"}," +
                @"""KvpWClassKvpVal"":{" +
                @"""Key"":""key""," +
                @"""Value"":{" +
                @"""Key"":""key""," +
                @"""Value"":null" +
                @"}" +
                @"}" +
                @"}";
            SimpleClassWithKeyValuePairs obj = JsonSerializer.Deserialize <SimpleClassWithKeyValuePairs>(json);

            Assert.Equal("key", obj.KvpWStrVal.Key);
            Assert.Equal("key", obj.KvpWObjVal.Key);
            Assert.Equal("key", obj.KvpWClassVal.Key);
            Assert.Equal("key", obj.KvpWStrKvpVal.Key);
            Assert.Equal("key", obj.KvpWObjKvpVal.Key);
            Assert.Equal("key", obj.KvpWClassKvpVal.Key);
            Assert.Equal("key", obj.KvpWStrKvpVal.Value.Key);
            Assert.Equal("key", obj.KvpWObjKvpVal.Value.Key);
            Assert.Equal("key", obj.KvpWClassKvpVal.Value.Key);

            Assert.Null(obj.KvpWStrVal.Value);
            Assert.Null(obj.KvpWObjVal.Value);
            Assert.Null(obj.KvpWClassVal.Value);
            Assert.Null(obj.KvpWStrKvpVal.Value.Value);
            Assert.Null(obj.KvpWObjKvpVal.Value.Value);
            Assert.Null(obj.KvpWClassKvpVal.Value.Value);
        }
        public async Task ReadClassWithNullKeyValuePairValues()
        {
            string json =
                @"{" +
                @"""KvpWStrVal"":{" +
                @"""Key"":""key""," +
                @"""Value"":null" +
                @"}," +
                @"""KvpWObjVal"":{" +
                @"""Key"":""key""," +
                @"""Value"":null" +
                @"}," +
                @"""KvpWClassVal"":{" +
                @"""Key"":""key""," +
                @"""Value"":null" +
                @"}," +
                @"""KvpWStrKvpVal"":{" +
                @"""Key"":""key""," +
                @"""Value"":{" +
                @"""Key"":""key""," +
                @"""Value"":null" +
                @"}" +
                @"}," +
                @"""KvpWObjKvpVal"":{" +
                @"""Key"":""key""," +
                @"""Value"":{" +
                @"""Key"":""key""," +
                @"""Value"":null" +
                @"}" +
                @"}," +
                @"""KvpWClassKvpVal"":{" +
                @"""Key"":""key""," +
                @"""Value"":{" +
                @"""Key"":""key""," +
                @"""Value"":null" +
                @"}" +
                @"}" +
                @"}";
            SimpleClassWithKeyValuePairs obj = await JsonSerializerWrapperForString.DeserializeWrapper <SimpleClassWithKeyValuePairs>(json);

            Assert.Equal("key", obj.KvpWStrVal.Key);
            Assert.Equal("key", obj.KvpWObjVal.Key);
            Assert.Equal("key", obj.KvpWClassVal.Key);
            Assert.Equal("key", obj.KvpWStrKvpVal.Key);
            Assert.Equal("key", obj.KvpWObjKvpVal.Key);
            Assert.Equal("key", obj.KvpWClassKvpVal.Key);
            Assert.Equal("key", obj.KvpWStrKvpVal.Value.Key);
            Assert.Equal("key", obj.KvpWObjKvpVal.Value.Key);
            Assert.Equal("key", obj.KvpWClassKvpVal.Value.Key);

            Assert.Null(obj.KvpWStrVal.Value);
            Assert.Null(obj.KvpWObjVal.Value);
            Assert.Null(obj.KvpWClassVal.Value);
            Assert.Null(obj.KvpWStrKvpVal.Value.Value);
            Assert.Null(obj.KvpWObjKvpVal.Value.Value);
            Assert.Null(obj.KvpWClassKvpVal.Value.Value);
        }
Exemple #4
0
        public static void WriteClassWithNullKeyValuePairValues_Regression39808()
        {
            var value = new SimpleClassWithKeyValuePairs()
            {
                KvpWStrVal      = new KeyValuePair <string, string>("key", null),
                KvpWObjVal      = new KeyValuePair <string, object>("key", null),
                KvpWClassVal    = new KeyValuePair <string, SimpleClassWithKeyValuePairs>("key", null),
                KvpWStrKvpVal   = new KeyValuePair <string, KeyValuePair <string, string> >("key", new KeyValuePair <string, string>("key", null)),
                KvpWObjKvpVal   = new KeyValuePair <string, KeyValuePair <string, object> >("key", new KeyValuePair <string, object>("key", null)),
                KvpWClassKvpVal = new KeyValuePair <string, KeyValuePair <string, SimpleClassWithKeyValuePairs> >("key", new KeyValuePair <string, SimpleClassWithKeyValuePairs>("key", null)),
            };

            string expectedJson = @"{""KvpWStrVal"":{""Key"":""key"",""Value"":null},""KvpWObjVal"":{""Key"":""key"",""Value"":null},""KvpWClassVal"":{""Key"":""key"",""Value"":null},""KvpWStrKvpVal"":{""Key"":""key"",""Value"":{""Key"":""key"",""Value"":null}},""KvpWObjKvpVal"":{""Key"":""key"",""Value"":{""Key"":""key"",""Value"":null}},""KvpWClassKvpVal"":{""Key"":""key"",""Value"":{""Key"":""key"",""Value"":null}}}";
            string result       = JsonSerializer.Serialize(value);

            Assert.Equal(expectedJson, result);
        }