Support unlimited array dimensions (dotnet/corefx#37867)
authorSteve Harter <steveharter@users.noreply.github.com>
Thu, 23 May 2019 20:46:19 +0000 (13:46 -0700)
committerGitHub <noreply@github.com>
Thu, 23 May 2019 20:46:19 +0000 (13:46 -0700)
Commit migrated from https://github.com/dotnet/corefx/commit/025bb33e27b15d973e8be430a9329309ecac37d3

12 files changed:
src/libraries/System.Text.Json/src/System/Text/Json/Serialization/JsonSerializer.Read.HandleArray.cs
src/libraries/System.Text.Json/src/System/Text/Json/Serialization/JsonSerializer.Read.HandleDictionary.cs
src/libraries/System.Text.Json/src/System/Text/Json/Serialization/JsonSerializer.Read.HandlePropertyName.cs
src/libraries/System.Text.Json/src/System/Text/Json/Serialization/ReadStackFrame.cs
src/libraries/System.Text.Json/tests/Serialization/Array.ReadTests.cs
src/libraries/System.Text.Json/tests/Serialization/Array.WriteTests.cs
src/libraries/System.Text.Json/tests/Serialization/DictionaryTests.cs
src/libraries/System.Text.Json/tests/Serialization/Object.ReadTests.cs
src/libraries/System.Text.Json/tests/Serialization/TestClasses.SimpleTestClass.cs
src/libraries/System.Text.Json/tests/Serialization/Value.ReadTests.cs
src/libraries/System.Text.Json/tests/Serialization/Value.WriteTests.cs
src/libraries/System.Text.Json/tests/System.Text.Json.Tests.csproj

index 8ff408c..9d62812 100644 (file)
@@ -47,10 +47,11 @@ namespace System.Text.Json.Serialization
             if (state.Current.PropertyInitialized)
             {
                 // A nested json array so push a new stack frame.
-                Type elementType = state.Current.JsonClassInfo.ElementClassInfo.GetPolicyProperty().RuntimePropertyType;
+                Type elementType = jsonPropertyInfo.ElementClassInfo.Type;
 
                 state.Push();
                 state.Current.Initialize(elementType, options);
+                state.Current.PropertyInitialized = true;
             }
             else
             {
@@ -64,6 +65,8 @@ namespace System.Text.Json.Serialization
             {
                 // Create the enumerable.
                 object value = ReadStackFrame.CreateEnumerableValue(ref reader, ref state, options);
+
+                // If value is not null, then we don't have a converter so apply the value.
                 if (value != null)
                 {
                     if (state.Current.ReturnValue != null)
@@ -94,15 +97,20 @@ namespace System.Text.Json.Serialization
             }
 
             IEnumerable value = ReadStackFrame.GetEnumerableValue(state.Current);
-            bool setPropertyDirectly;
 
             if (state.Current.TempEnumerableValues != null)
             {
+                // We have a converter; possibilities:
+                // - Add value to current frame's current property or TempEnumerableValues.
+                // - Add value to previous frame's current property or TempEnumerableValues.
+                // - Set current property on current frame to value.
+                // - Set current property on previous frame to value.
+                // - Set ReturnValue if root frame and value is the actual return value.
                 JsonEnumerableConverter converter = state.Current.JsonPropertyInfo.EnumerableConverter;
                 Debug.Assert(converter != null);
 
                 value = converter.CreateFromList(ref state, (IList)value, options);
-                setPropertyDirectly = true;
+                state.Current.TempEnumerableValues = null;
             }
             else if (state.Current.IsEnumerableProperty)
             {
@@ -110,10 +118,6 @@ namespace System.Text.Json.Serialization
                 state.Current.ResetProperty();
                 return false;
             }
-            else
-            {
-                setPropertyDirectly = false;
-            }
 
             if (lastFrame)
             {
@@ -136,12 +140,7 @@ namespace System.Text.Json.Serialization
                 state.Pop();
             }
 
-            ApplyObjectToEnumerable(value, ref state, ref reader, setPropertyDirectly: setPropertyDirectly);
-
-            if (state.Current.IsEnumerableProperty)
-            {
-                state.Current.ResetProperty();
-            }
+            ApplyObjectToEnumerable(value, ref state, ref reader);
 
             return false;
         }
@@ -177,8 +176,14 @@ namespace System.Text.Json.Serialization
                 else
                 {
                     IList list = (IList)state.Current.JsonPropertyInfo.GetValueAsObject(state.Current.ReturnValue);
-                    Debug.Assert(list != null);
-                    list.Add(value);
+                    if (list == null)
+                    {
+                        state.Current.JsonPropertyInfo.SetValueAsObject(state.Current.ReturnValue, value);
+                    }
+                    else
+                    {
+                        list.Add(value);
+                    }
                 }
             }
             else if (state.Current.IsDictionary || (state.Current.IsDictionaryProperty && !setPropertyDirectly))
@@ -234,8 +239,14 @@ namespace System.Text.Json.Serialization
                 else
                 {
                     IList<TProperty> list = (IList<TProperty>)state.Current.JsonPropertyInfo.GetValueAsObject(state.Current.ReturnValue);
-                    Debug.Assert(list != null);
-                    list.Add(value);
+                    if (list == null)
+                    {
+                        state.Current.JsonPropertyInfo.SetValueAsObject(state.Current.ReturnValue, value);
+                    }
+                    else
+                    {
+                        list.Add(value);
+                    }
                 }
             }
             else if (state.Current.IsProcessingDictionary)
index 27f939b..17b9555 100644 (file)
@@ -26,10 +26,10 @@ namespace System.Text.Json.Serialization
             {
                 Debug.Assert(state.Current.IsDictionary);
 
-                JsonClassInfo classInfoTemp = state.Current.JsonClassInfo;
                 state.Push();
-                state.Current.JsonClassInfo = classInfoTemp.ElementClassInfo;
+                state.Current.JsonClassInfo = jsonPropertyInfo.ElementClassInfo;
                 state.Current.InitializeJsonPropertyInfo();
+                state.Current.PropertyInitialized = true;
 
                 ClassType classType = state.Current.JsonClassInfo.ClassType;
                 if (classType == ClassType.Value &&
index 3c0bde8..f32d08e 100644 (file)
@@ -60,6 +60,8 @@ namespace System.Text.Json.Serialization
             }
             else
             {
+                state.Current.ResetProperty();
+
                 ReadOnlySpan<byte> propertyName = reader.HasValueSequence ? reader.ValueSequence.ToArray() : reader.ValueSpan;
                 if (reader._stringHasEscaping)
                 {
index 6170ba1..f35bc44 100644 (file)
@@ -102,6 +102,7 @@ namespace System.Text.Json.Serialization
             PropertyInitialized = false;
             JsonPropertyInfo = null;
             TempEnumerableValues = null;
+            KeyName = null;
         }
 
         public void EndObject()
index b30e8de..05854c5 100644 (file)
@@ -3,8 +3,6 @@
 // See the LICENSE file in the project root for more information.
 
 using System.Collections.Generic;
-using System.Collections.Immutable;
-using System.Reflection;
 using Xunit;
 
 namespace System.Text.Json.Serialization.Tests
@@ -12,6 +10,68 @@ namespace System.Text.Json.Serialization.Tests
     public static partial class ArrayTests
     {
         [Fact]
+        public static void ReadObjectArray()
+        {
+            string data =
+                "[" +
+                SimpleTestClass.s_json +
+                "," +
+                SimpleTestClass.s_json +
+                "]";
+
+            SimpleTestClass[] i = JsonSerializer.Parse<SimpleTestClass[]>(Encoding.UTF8.GetBytes(data));
+
+            i[0].Verify();
+            i[1].Verify();
+        }
+
+        [Fact]
+        public static void ReadEmptyObjectArray()
+        {
+            SimpleTestClass[] data = JsonSerializer.Parse<SimpleTestClass[]>("[{}]");
+            Assert.Equal(1, data.Length);
+            Assert.NotNull(data[0]);
+        }
+
+        [Fact]
+        public static void ReadPrimitiveJagged2dArray()
+        {
+            int[][] i = JsonSerializer.Parse<int[][]>(Encoding.UTF8.GetBytes(@"[[1,2],[3,4]]"));
+            Assert.Equal(1, i[0][0]);
+            Assert.Equal(2, i[0][1]);
+            Assert.Equal(3, i[1][0]);
+            Assert.Equal(4, i[1][1]);
+        }
+
+        [Fact]
+        public static void ReadPrimitiveJagged3dArray()
+        {
+            int[][][] i = JsonSerializer.Parse<int[][][]>(Encoding.UTF8.GetBytes(@"[[[11,12],[13,14]], [[21,22],[23,24]]]"));
+            Assert.Equal(11, i[0][0][0]);
+            Assert.Equal(12, i[0][0][1]);
+            Assert.Equal(13, i[0][1][0]);
+            Assert.Equal(14, i[0][1][1]);
+
+            Assert.Equal(21, i[1][0][0]);
+            Assert.Equal(22, i[1][0][1]);
+            Assert.Equal(23, i[1][1][0]);
+            Assert.Equal(24, i[1][1][1]);
+        }
+
+        [Fact]
+        public static void ReadArrayWithInterleavedComments()
+        {
+            var options = new JsonSerializerOptions();
+            options.ReadCommentHandling = JsonCommentHandling.Skip;
+
+            int[][] i = JsonSerializer.Parse<int[][]>(Encoding.UTF8.GetBytes("[[1,2] // Inline [\n,[3, /* Multi\n]] Line*/4]]"), options);
+            Assert.Equal(1, i[0][0]);
+            Assert.Equal(2, i[0][1]);
+            Assert.Equal(3, i[1][0]);
+            Assert.Equal(4, i[1][1]);
+        }
+
+        [Fact]
         public static void ReadEmpty()
         {
             SimpleTestClass[] arr = JsonSerializer.Parse<SimpleTestClass[]>("[]");
@@ -21,6 +81,37 @@ namespace System.Text.Json.Serialization.Tests
             Assert.Equal(0, list.Count);
         }
 
+        [Fact]
+        public static void ReadPrimitiveArray()
+        {
+            int[] i = JsonSerializer.Parse<int[]>(Encoding.UTF8.GetBytes(@"[1,2]"));
+            Assert.Equal(1, i[0]);
+            Assert.Equal(2, i[1]);
+
+            i = JsonSerializer.Parse<int[]>(Encoding.UTF8.GetBytes(@"[]"));
+            Assert.Equal(0, i.Length);
+        }
+
+        [Fact]
+        public static void ReadArrayWithEnums()
+        {
+            SampleEnum[] i = JsonSerializer.Parse<SampleEnum[]>(Encoding.UTF8.GetBytes(@"[1,2]"));
+            Assert.Equal(SampleEnum.One, i[0]);
+            Assert.Equal(SampleEnum.Two, i[1]);
+        }
+
+        [Fact]
+        public static void ReadPrimitiveArrayFail()
+        {
+            // Invalid data
+            Assert.Throws<JsonException>(() => JsonSerializer.Parse<int[]>(Encoding.UTF8.GetBytes(@"[1,""a""]")));
+
+            // Invalid data
+            Assert.Throws<JsonException>(() => JsonSerializer.Parse<List<int?>>(Encoding.UTF8.GetBytes(@"[1,""a""]")));
+
+            // Multidimensional arrays currently not supported
+            Assert.Throws<JsonException>(() => JsonSerializer.Parse<int[,]>(Encoding.UTF8.GetBytes(@"[[1,2],[3,4]]")));
+        }
 
         public static IEnumerable<object[]> ReadNullJson
         {
index b1e7e77..65ef8bb 100644 (file)
@@ -10,6 +10,66 @@ namespace System.Text.Json.Serialization.Tests
     public static partial class ArrayTests
     {
         [Fact]
+        public static void WritePrimitiveArray()
+        {
+            var input = new int[] { 0, 1 };
+            string json = JsonSerializer.ToString(input);
+            Assert.Equal("[0,1]", json);
+        }
+
+        [Fact]
+        public static void WriteArrayWithEnums()
+        {
+            var input = new SampleEnum[] { SampleEnum.One, SampleEnum.Two };
+            string json = JsonSerializer.ToString(input);
+            Assert.Equal("[1,2]", json);
+        }
+
+        [Fact]
+        public static void WriteObjectArray()
+        {
+            string json;
+
+            {
+                SimpleTestClass[] input = new SimpleTestClass[] { new SimpleTestClass(), new SimpleTestClass() };
+                input[0].Initialize();
+                input[0].Verify();
+
+                input[1].Initialize();
+                input[1].Verify();
+
+                json = JsonSerializer.ToString(input);
+            }
+
+            {
+                SimpleTestClass[] output = JsonSerializer.Parse<SimpleTestClass[]>(json);
+                Assert.Equal(2, output.Length);
+                output[0].Verify();
+                output[1].Verify();
+            }
+        }
+
+        [Fact]
+        public static void WriteEmptyObjectArray()
+        {
+            object[] arr = new object[] { new object() };
+
+            string json = JsonSerializer.ToString(arr);
+            Assert.Equal("[{}]", json);
+        }
+
+        [Fact]
+        public static void WritePrimitiveJaggedArray()
+        {
+            var input = new int[2][];
+            input[0] = new int[] { 1, 2 };
+            input[1] = new int[] { 3, 4 };
+
+            string json = JsonSerializer.ToString(input);
+            Assert.Equal("[[1,2],[3,4]]", json);
+        }
+
+        [Fact]
         public static void WriteEmpty()
         {
             string json = JsonSerializer.ToString(new SimpleTestClass[] { });
index 4e80a3a..5d6d114 100644 (file)
@@ -208,6 +208,72 @@ namespace System.Text.Json.Serialization.Tests
         }
 
         [Fact]
+        public static void DictionaryOfDictionaryOfDictionary()
+        {
+            const string JsonString = @"{""Key1"":{""Key1"":{""Key1"":1,""Key2"":2},""Key2"":{""Key1"":3,""Key2"":4}},""Key2"":{""Key1"":{""Key1"":5,""Key2"":6},""Key2"":{""Key1"":7,""Key2"":8}}}";
+            Dictionary<string, Dictionary<string, Dictionary<string, int>>> obj = JsonSerializer.Parse<Dictionary<string, Dictionary<string, Dictionary<string, int>>>>(JsonString);
+
+            Assert.Equal(2, obj.Count);
+            Assert.Equal(2, obj["Key1"].Count);
+            Assert.Equal(2, obj["Key1"]["Key1"].Count);
+            Assert.Equal(2, obj["Key1"]["Key2"].Count);
+
+            Assert.Equal(1, obj["Key1"]["Key1"]["Key1"]);
+            Assert.Equal(2, obj["Key1"]["Key1"]["Key2"]);
+            Assert.Equal(3, obj["Key1"]["Key2"]["Key1"]);
+            Assert.Equal(4, obj["Key1"]["Key2"]["Key2"]);
+
+            Assert.Equal(2, obj["Key2"].Count);
+            Assert.Equal(2, obj["Key2"]["Key1"].Count);
+            Assert.Equal(2, obj["Key2"]["Key2"].Count);
+
+            Assert.Equal(5, obj["Key2"]["Key1"]["Key1"]);
+            Assert.Equal(6, obj["Key2"]["Key1"]["Key2"]);
+            Assert.Equal(7, obj["Key2"]["Key2"]["Key1"]);
+            Assert.Equal(8, obj["Key2"]["Key2"]["Key2"]);
+
+            string json = JsonSerializer.ToString(obj);
+            Assert.Equal(JsonString, json);
+
+            // Verify that typeof(object) doesn't interfere.
+            json = JsonSerializer.ToString<object>(obj);
+            Assert.Equal(JsonString, json);
+        }
+
+        [Fact]
+        public static void DictionaryOfArrayOfDictionary()
+        {
+            const string JsonString = @"{""Key1"":[{""Key1"":1,""Key2"":2},{""Key1"":3,""Key2"":4}],""Key2"":[{""Key1"":5,""Key2"":6},{""Key1"":7,""Key2"":8}]}";
+            Dictionary<string, Dictionary<string, int>[]> obj = JsonSerializer.Parse<Dictionary<string, Dictionary<string, int>[]>>(JsonString);
+
+            Assert.Equal(2, obj.Count);
+            Assert.Equal(2, obj["Key1"].Length);
+            Assert.Equal(2, obj["Key1"][0].Count);
+            Assert.Equal(2, obj["Key1"][1].Count);
+
+            Assert.Equal(1, obj["Key1"][0]["Key1"]);
+            Assert.Equal(2, obj["Key1"][0]["Key2"]);
+            Assert.Equal(3, obj["Key1"][1]["Key1"]);
+            Assert.Equal(4, obj["Key1"][1]["Key2"]);
+
+            Assert.Equal(2, obj["Key2"].Length);
+            Assert.Equal(2, obj["Key2"][0].Count);
+            Assert.Equal(2, obj["Key2"][1].Count);
+
+            Assert.Equal(5, obj["Key2"][0]["Key1"]);
+            Assert.Equal(6, obj["Key2"][0]["Key2"]);
+            Assert.Equal(7, obj["Key2"][1]["Key1"]);
+            Assert.Equal(8, obj["Key2"][1]["Key2"]);
+
+            string json = JsonSerializer.ToString(obj);
+            Assert.Equal(JsonString, json);
+
+            // Verify that typeof(object) doesn't interfere.
+            json = JsonSerializer.ToString<object>(obj);
+            Assert.Equal(JsonString, json);
+        }
+
+        [Fact]
         public static void DictionaryOfClasses()
         {
             Dictionary<string, SimpleTestClass> obj;
index ed4720c..7f7ed02 100644 (file)
@@ -174,5 +174,42 @@ namespace System.Text.Json.Serialization.Tests
             TestClassWithStringToPrimitiveDictionary obj = JsonSerializer.Parse<TestClassWithStringToPrimitiveDictionary>(TestClassWithStringToPrimitiveDictionary.s_data);
             obj.Verify();
         }
+
+        public class TestClassWithBadData
+        {
+            public TestChildClassWithBadData[] Children { get; set; }
+        }
+
+        public class TestChildClassWithBadData
+        {
+            public int MyProperty { get; set; }
+        }
+
+        [Fact]
+        public static void ReadConversionFails()
+        {
+            byte[] data = Encoding.UTF8.GetBytes(
+                @"{" +
+                    @"""Children"":[" +
+                        @"{""MyProperty"":""StringButShouldBeInt""}" +
+                    @"]" +
+                @"}");
+
+            bool exceptionThrown = false;
+
+            try
+            {
+                JsonSerializer.Parse<TestClassWithBadData>(data);
+            }
+            catch (JsonException exception)
+            {
+                exceptionThrown = true;
+
+                // Exception should contain property path.
+                Assert.True(exception.ToString().Contains("[System.Text.Json.Serialization.Tests.ObjectTests+TestClassWithBadData].Children.MyProperty"));
+            }
+
+            Assert.True(exceptionThrown);
+        }
     }
 }
index f5b090f..ba69ea6 100644 (file)
@@ -49,6 +49,10 @@ namespace System.Text.Json.Serialization.Tests
         public DateTime[] MyDateTimeArray { get; set; }
         public DateTimeOffset[] MyDateTimeOffsetArray { get; set; }
         public SampleEnum[] MyEnumArray { get; set; }
+        public int[][] MyInt16TwoDimensionArray { get; set; }
+        public List<List<int>> MyInt16TwoDimensionList { get; set; }
+        public int[][][] MyInt16ThreeDimensionArray { get; set; }
+        public List<List<List<int>>> MyInt16ThreeDimensionList { get; set; }
         public List<string> MyStringList { get; set; }
         public IEnumerable<string> MyStringIEnumerableT { get; set; }
         public IList<string> MyStringIListT { get; set; }
@@ -119,6 +123,10 @@ namespace System.Text.Json.Serialization.Tests
                 @"""MyDateTimeArray"" : [""2019-01-30T12:01:02.0000000Z""]," +
                 @"""MyDateTimeOffsetArray"" : [""2019-01-30T12:01:02.0000000+01:00""]," +
                 @"""MyEnumArray"" : [2]," + // int by default
+                @"""MyInt16TwoDimensionArray"" : [[10, 11],[20, 21]]," +
+                @"""MyInt16TwoDimensionList"" : [[10, 11],[20, 21]]," +
+                @"""MyInt16ThreeDimensionArray"" : [[[11, 12],[13, 14]],[[21,22],[23,24]]]," +
+                @"""MyInt16ThreeDimensionList"" : [[[11, 12],[13, 14]],[[21,22],[23,24]]]," +
                 @"""MyStringList"" : [""Hello""]," +
                 @"""MyStringIEnumerableT"" : [""Hello""]," +
                 @"""MyStringIListT"" : [""Hello""]," +
@@ -183,6 +191,32 @@ namespace System.Text.Json.Serialization.Tests
             MyDateTimeOffsetArray = new DateTimeOffset[] { new DateTimeOffset(2019, 1, 30, 12, 1, 2, new TimeSpan(1, 0, 0)) };
             MyEnumArray = new SampleEnum[] { SampleEnum.Two };
 
+            MyInt16TwoDimensionArray = new int[2][];
+            MyInt16TwoDimensionArray[0] = new int[] { 10, 11 };
+            MyInt16TwoDimensionArray[1] = new int[] { 20, 21 };
+
+            MyInt16TwoDimensionList = new List<List<int>>();
+            MyInt16TwoDimensionList.Add(new List<int> { 10, 11 });
+            MyInt16TwoDimensionList.Add(new List<int> { 20, 21 });
+
+            MyInt16ThreeDimensionArray = new int[2][][];
+            MyInt16ThreeDimensionArray[0] = new int[2][];
+            MyInt16ThreeDimensionArray[1] = new int[2][];
+            MyInt16ThreeDimensionArray[0][0] = new int[] { 11, 12 };
+            MyInt16ThreeDimensionArray[0][1] = new int[] { 13, 14 };
+            MyInt16ThreeDimensionArray[1][0] = new int[] { 21, 22 };
+            MyInt16ThreeDimensionArray[1][1] = new int[] { 23, 24 };
+
+            MyInt16ThreeDimensionList = new List<List<List<int>>>();
+            var list1 = new List<List<int>>();
+            MyInt16ThreeDimensionList.Add(list1);
+            list1.Add(new List<int> { 11, 12 });
+            list1.Add(new List<int> { 13, 14 });
+            var list2 = new List<List<int>>();
+            MyInt16ThreeDimensionList.Add(list2);
+            list2.Add(new List<int> { 21, 22 });
+            list2.Add(new List<int> { 23, 24 });
+
             MyStringList = new List<string>() { "Hello" };
             MyStringIEnumerableT = new string[] { "Hello" };
             MyStringIListT = new string[] { "Hello" };
@@ -253,6 +287,34 @@ namespace System.Text.Json.Serialization.Tests
             Assert.Equal(new DateTimeOffset(2019, 1, 30, 12, 1, 2, new TimeSpan(1, 0, 0)), MyDateTimeOffsetArray[0]);
             Assert.Equal(SampleEnum.Two, MyEnumArray[0]);
 
+            Assert.Equal(10, MyInt16TwoDimensionArray[0][0]);
+            Assert.Equal(11, MyInt16TwoDimensionArray[0][1]);
+            Assert.Equal(20, MyInt16TwoDimensionArray[1][0]);
+            Assert.Equal(21, MyInt16TwoDimensionArray[1][1]);
+
+            Assert.Equal(10, MyInt16TwoDimensionList[0][0]);
+            Assert.Equal(11, MyInt16TwoDimensionList[0][1]);
+            Assert.Equal(20, MyInt16TwoDimensionList[1][0]);
+            Assert.Equal(21, MyInt16TwoDimensionList[1][1]);
+
+            Assert.Equal(11, MyInt16ThreeDimensionArray[0][0][0]);
+            Assert.Equal(12, MyInt16ThreeDimensionArray[0][0][1]);
+            Assert.Equal(13, MyInt16ThreeDimensionArray[0][1][0]);
+            Assert.Equal(14, MyInt16ThreeDimensionArray[0][1][1]);
+            Assert.Equal(21, MyInt16ThreeDimensionArray[1][0][0]);
+            Assert.Equal(22, MyInt16ThreeDimensionArray[1][0][1]);
+            Assert.Equal(23, MyInt16ThreeDimensionArray[1][1][0]);
+            Assert.Equal(24, MyInt16ThreeDimensionArray[1][1][1]);
+
+            Assert.Equal(11, MyInt16ThreeDimensionList[0][0][0]);
+            Assert.Equal(12, MyInt16ThreeDimensionList[0][0][1]);
+            Assert.Equal(13, MyInt16ThreeDimensionList[0][1][0]);
+            Assert.Equal(14, MyInt16ThreeDimensionList[0][1][1]);
+            Assert.Equal(21, MyInt16ThreeDimensionList[1][0][0]);
+            Assert.Equal(22, MyInt16ThreeDimensionList[1][0][1]);
+            Assert.Equal(23, MyInt16ThreeDimensionList[1][1][0]);
+            Assert.Equal(24, MyInt16ThreeDimensionList[1][1][1]);
+
             Assert.Equal("Hello", MyStringList[0]);
             Assert.Equal("Hello", MyStringIEnumerableT.First());
             Assert.Equal("Hello", MyStringIListT[0]);
index a7fe869..5adca84 100644 (file)
@@ -2,7 +2,6 @@
 // The .NET Foundation licenses this file to you under the MIT license.
 // See the LICENSE file in the project root for more information.
 
-using System.Collections.Generic;
 using Xunit;
 
 namespace System.Text.Json.Serialization.Tests
@@ -101,25 +100,6 @@ namespace System.Text.Json.Serialization.Tests
         }
 
         [Fact]
-        public static void ReadPrimitiveArray()
-        {
-            int[] i = JsonSerializer.Parse<int[]>(Encoding.UTF8.GetBytes(@"[1,2]"));
-            Assert.Equal(1, i[0]);
-            Assert.Equal(2, i[1]);
-
-            i = JsonSerializer.Parse<int[]>(Encoding.UTF8.GetBytes(@"[]"));
-            Assert.Equal(0, i.Length);
-        }
-
-        [Fact]
-        public static void ReadArrayWithEnums()
-        {
-            SampleEnum[] i = JsonSerializer.Parse<SampleEnum[]>(Encoding.UTF8.GetBytes(@"[1,2]"));
-            Assert.Equal(SampleEnum.One, i[0]);
-            Assert.Equal(SampleEnum.Two, i[1]);
-        }
-
-        [Fact]
         public static void EmptyStringInput()
         {
             string obj = JsonSerializer.Parse<string>(@"""""");
@@ -127,19 +107,6 @@ namespace System.Text.Json.Serialization.Tests
         }
 
         [Fact]
-        public static void ReadPrimitiveArrayFail()
-        {
-            // Invalid data
-            Assert.Throws<JsonException>(() => JsonSerializer.Parse<int[]>(Encoding.UTF8.GetBytes(@"[1,""a""]")));
-
-            // Invalid data
-            Assert.Throws<JsonException>(() => JsonSerializer.Parse<List<int?>>(Encoding.UTF8.GetBytes(@"[1,""a""]")));
-
-            // Multidimensional arrays currently not supported
-            Assert.Throws<JsonException>(() => JsonSerializer.Parse<int[,]>(Encoding.UTF8.GetBytes(@"[[1,2],[3,4]]")));
-        }
-
-        [Fact]
         public static void ReadPrimitiveExtraBytesFail()
         {
             Assert.Throws<JsonException>(() => JsonSerializer.Parse<int[]>("[2] {3}"));
@@ -301,89 +268,5 @@ namespace System.Text.Json.Serialization.Tests
 
             Assert.Throws<JsonException>(() => JsonSerializer.Parse<Enum>(unexpectedString));
         }
-
-        [Fact]
-        public static void ReadObjectArray()
-        {
-            string data =
-                "[" +
-                SimpleTestClass.s_json +
-                "," +
-                SimpleTestClass.s_json +
-                "]";
-
-            SimpleTestClass[] i = JsonSerializer.Parse<SimpleTestClass[]>(Encoding.UTF8.GetBytes(data));
-
-            i[0].Verify();
-            i[1].Verify();
-        }
-
-        [Fact]
-        public static void ReadEmptyObjectArray()
-        {
-            SimpleTestClass[] data = JsonSerializer.Parse<SimpleTestClass[]>("[{}]");
-            Assert.Equal(1, data.Length);
-            Assert.NotNull(data[0]);
-        }
-
-        [Fact]
-        public static void ReadPrimitiveJaggedArray()
-        {
-            int[][] i = JsonSerializer.Parse<int[][]>(Encoding.UTF8.GetBytes(@"[[1,2],[3,4]]"));
-            Assert.Equal(1, i[0][0]);
-            Assert.Equal(2, i[0][1]);
-            Assert.Equal(3, i[1][0]);
-            Assert.Equal(4, i[1][1]);
-        }
-
-        [Fact]
-        public static void ReadArrayWithInterleavedComments()
-        {
-            var options = new JsonSerializerOptions();
-            options.ReadCommentHandling = JsonCommentHandling.Skip;
-
-            int[][] i = JsonSerializer.Parse<int[][]>(Encoding.UTF8.GetBytes("[[1,2] // Inline [\n,[3, /* Multi\n]] Line*/4]]"), options);
-            Assert.Equal(1, i[0][0]);
-            Assert.Equal(2, i[0][1]);
-            Assert.Equal(3, i[1][0]);
-            Assert.Equal(4, i[1][1]);
-        }
-
-        public class TestClassWithBadData
-        {
-            public TestChildClassWithBadData[] Children { get; set; }
-        }
-
-        public class TestChildClassWithBadData
-        {
-            public int MyProperty { get; set; }
-        }
-
-        [Fact]
-        public static void ReadConversionFails()
-        {
-            byte[] data = Encoding.UTF8.GetBytes(
-                @"{" +
-                    @"""Children"":[" +
-                        @"{""MyProperty"":""StringButShouldBeInt""}" +
-                    @"]" +
-                @"}");
-
-            bool exceptionThrown = false;
-
-            try
-            {
-                JsonSerializer.Parse<TestClassWithBadData>(data);
-            }
-            catch (JsonException exception)
-            {
-                exceptionThrown = true;
-
-                // Exception should contain property path.
-                Assert.True(exception.ToString().Contains("[System.Text.Json.Serialization.Tests.ValueTests+TestClassWithBadData].Children.MyProperty"));
-            }
-
-            Assert.True(exceptionThrown);
-        }
     }
 }
index 17b845b..55f0643 100644 (file)
@@ -2,8 +2,6 @@
 // The .NET Foundation licenses this file to you under the MIT license.
 // See the LICENSE file in the project root for more information.
 
-using System.Collections.Generic;
-using System.Collections.Immutable;
 using Xunit;
 
 namespace System.Text.Json.Serialization.Tests
@@ -55,65 +53,5 @@ namespace System.Text.Json.Serialization.Tests
                 Assert.Equal(Encoding.UTF8.GetBytes(@"""Hello"""), json.ToArray());
             }
         }
-
-        [Fact]
-        public static void WritePrimitiveArray()
-        {
-            var input = new int[] { 0, 1 };
-            string json = JsonSerializer.ToString(input);
-            Assert.Equal("[0,1]", json);
-        }
-
-        [Fact]
-        public static void WriteArrayWithEnums()
-        {
-            var input = new SampleEnum[] { SampleEnum.One, SampleEnum.Two };
-            string json = JsonSerializer.ToString(input);
-            Assert.Equal("[1,2]", json);
-        }
-
-        [Fact]
-        public static void WriteObjectArray()
-        {
-            string json;
-
-            {
-                SimpleTestClass[] input = new SimpleTestClass[] { new SimpleTestClass(), new SimpleTestClass() };
-                input[0].Initialize();
-                input[0].Verify();
-
-                input[1].Initialize();
-                input[1].Verify();
-
-                json = JsonSerializer.ToString(input);
-            }
-
-            {
-                SimpleTestClass[] output = JsonSerializer.Parse<SimpleTestClass[]>(json);
-                Assert.Equal(2, output.Length);
-                output[0].Verify();
-                output[1].Verify();
-            }
-        }
-
-        [Fact]
-        public static void WriteEmptyObjectArray()
-        {
-            object[] arr = new object[]{new object()};
-
-            string json = JsonSerializer.ToString(arr);
-            Assert.Equal("[{}]", json);
-        }
-
-        [Fact]
-        public static void WritePrimitiveJaggedArray()
-        {
-            var input = new int[2][];
-            input[0] = new int[] { 1, 2 };
-            input[1] = new int[] { 3, 4 };
-
-            string json = JsonSerializer.ToString(input);
-            Assert.Equal("[[1,2],[3,4]]", json);
-        }
     }
 }
index 137c99a..f9490f3 100644 (file)
@@ -29,9 +29,9 @@
     <Compile Include="Serialization\CyclicTests.cs" />
     <Compile Include="Serialization\DictionaryTests.cs" />
     <Compile Include="Serialization\EnumTests.cs" />
+    <Compile Include="Serialization\ExceptionTests.cs" />
     <Compile Include="Serialization\ExtensionDataTests.cs" />
     <Compile Include="Serialization\JsonElementTests.cs" />
-    <Compile Include="Serialization\ExceptionTests.cs" />
     <Compile Include="Serialization\Null.ReadTests.cs" />
     <Compile Include="Serialization\Null.WriteTests.cs" />
     <Compile Include="Serialization\Object.ReadTests.cs" />