Ejemplo n.º 1
0
        public void TestObject()
        {
            var json      = @"{""a"": {""first"": 1, ""second"": ""two""}}";
            var options   = new JsonSerializerOptions();
            var converter = new InferredTypeConverter();

            options.Converters.Add(converter);
            var    result = JsonSerializer.Deserialize <Dictionary <string, object> >(json, options);
            object value;

            Assert.True(result.TryGetValue("a", out value));
            Assert.IsType <Dictionary <string, object> >(value);
            var dictionary = (Dictionary <string, object>)value;

            Assert.True(dictionary.TryGetValue("first", out value));
            Assert.IsType <long>(value);
            Assert.Equal(1, (long)value);
            Assert.True(dictionary.TryGetValue("second", out value));
            Assert.IsType <string>(value);
            Assert.Equal("two", (string)value);

            json   = @"{""a"": {}}";
            result = JsonSerializer.Deserialize <Dictionary <string, object> >(json, options);
            Assert.True(result.TryGetValue("a", out value));
            Assert.IsType <Dictionary <string, object> >(value);
            dictionary = (Dictionary <string, object>)value;
            Assert.Empty(dictionary);

            json   = @"{""a"": {""first"": [1, false], ""second"": {""b"": ""hi""}}}";
            result = JsonSerializer.Deserialize <Dictionary <string, object> >(json, options);
            Assert.True(result.TryGetValue("a", out value));
            Assert.IsType <Dictionary <string, object> >(value);
            dictionary = (Dictionary <string, object>)value;

            Assert.True(dictionary.TryGetValue("first", out value));
            Assert.IsType <object[]>(value);
            var first = (object[])value;

            Assert.Equal(2, first.Length);
            Assert.IsType <long>(first[0]);
            Assert.Equal(1, (long)first[0]);
            Assert.IsType <bool>(first[1]);
            Assert.False((bool)first[1]);

            Assert.True(dictionary.TryGetValue("second", out value));
            Assert.IsType <Dictionary <string, object> >(value);
            var second = (Dictionary <string, object>)value;

            Assert.True(second.TryGetValue("b", out value));
            Assert.IsType <string>(value);
            Assert.Equal("hi", (string)value);
        }
Ejemplo n.º 2
0
        public void TestNull()
        {
            var json      = @"{""a"": null}";
            var options   = new JsonSerializerOptions();
            var converter = new InferredTypeConverter();

            options.Converters.Add(converter);
            var    result = JsonSerializer.Deserialize <Dictionary <string, object> >(json, options);
            object value;

            Assert.True(result.TryGetValue("a", out value));
            Assert.Null(value);
        }
Ejemplo n.º 3
0
        public void TestArray()
        {
            var json      = @"{""a"": [1, ""two"", null, 4.1, true]}";
            var options   = new JsonSerializerOptions();
            var converter = new InferredTypeConverter();

            options.Converters.Add(converter);
            var    result = JsonSerializer.Deserialize <Dictionary <string, object> >(json, options);
            object value;

            Assert.True(result.TryGetValue("a", out value));
            Assert.IsType <object[]>(value);
            var array = (object[])value;

            Assert.Equal(5, array.Length);

            Assert.IsType <long>(array[0]);
            Assert.Equal(1, (long)array[0]);
            Assert.IsType <string>(array[1]);
            Assert.Equal("two", (string)array[1]);
            Assert.Null(array[2]);
            Assert.IsType <double>(array[3]);
            Assert.True(Math.Abs(4.1 - (double)array[3]) < 0.001);
            Assert.IsType <bool>(array[4]);
            Assert.True((bool)array[4]);

            json   = @"{""a"": []}";
            result = JsonSerializer.Deserialize <Dictionary <string, object> >(json, options);
            Assert.True(result.TryGetValue("a", out value));
            Assert.IsType <object[]>(value);
            array = (object[])value;
            Assert.Empty(array);

            json   = @"{""a"": [[1,true],{""b"": ""hi""}]}";
            result = JsonSerializer.Deserialize <Dictionary <string, object> >(json, options);
            Assert.True(result.TryGetValue("a", out value));
            Assert.IsType <object[]>(value);
            array = (object[])value;
            Assert.Equal(2, array.Length);

            Assert.IsType <object[]>(array[0]);
            Assert.Equal(2, ((object[])array[0]).Length);
            Assert.IsType <long>(((object[])array[0])[0]);
            Assert.Equal(1, (long)((object[])array[0])[0]);
            Assert.IsType <bool>(((object[])array[0])[1]);
            Assert.True((bool)((object[])array[0])[1]);
            Assert.IsType <Dictionary <string, object> >(array[1]);
            Assert.Equal("hi", ((Dictionary <string, object>)array[1])["b"]);
        }
Ejemplo n.º 4
0
        public void TestStrings()
        {
            var json      = @"{""a"": ""hello"", ""b"": """"}";
            var options   = new JsonSerializerOptions();
            var converter = new InferredTypeConverter();

            options.Converters.Add(converter);
            var    result = JsonSerializer.Deserialize <Dictionary <string, object> >(json, options);
            object value;

            Assert.True(result.TryGetValue("a", out value));
            Assert.IsType <string>(value);
            Assert.Equal("hello", (string)value);
            Assert.True(result.TryGetValue("b", out value));
            Assert.IsType <string>(value);
            Assert.Equal("", (string)value);
        }
Ejemplo n.º 5
0
        public void TestFloats()
        {
            var json      = @"{""a"": 12.5, ""b"": 0.1}";
            var options   = new JsonSerializerOptions();
            var converter = new InferredTypeConverter();

            options.Converters.Add(converter);
            var    result = JsonSerializer.Deserialize <Dictionary <string, object> >(json, options);
            object value;

            Assert.True(result.TryGetValue("a", out value));
            Assert.IsType <double>(value);
            Assert.True(Math.Abs(12.5 - (double)value) < 0.001);
            Assert.True(result.TryGetValue("b", out value));
            Assert.IsType <double>(value);
            Assert.True(Math.Abs(0.1 - (double)value) < 0.001);
        }
Ejemplo n.º 6
0
        public void TestBooleans()
        {
            var json      = @"{""a"": true, ""b"": false}";
            var options   = new JsonSerializerOptions();
            var converter = new InferredTypeConverter();

            options.Converters.Add(converter);
            var    result = JsonSerializer.Deserialize <Dictionary <string, object> >(json, options);
            object value;

            Assert.True(result.TryGetValue("a", out value));
            Assert.IsType <bool>(value);
            Assert.True((bool)value);
            Assert.True(result.TryGetValue("b", out value));
            Assert.IsType <bool>(value);
            Assert.False((bool)value);
        }