-Fixed deserializing complex values inside multi-dimensional arrays

This commit is contained in:
James Newton-King
2014-11-03 16:10:08 -08:00
parent 3fa2949a38
commit 52d54d50e7
3 changed files with 54 additions and 3 deletions
@@ -144,7 +144,7 @@ namespace Newtonsoft.Json.Tests.Serialization
}
[Test]
public void Sdfsdf()
public void DataBagDoesNotInheritFromDictionaryClass()
{
Example e = new Example();
e.Data.Add("extensionData1", new int[] { 1, 2, 3 });
@@ -57,6 +57,52 @@ namespace Newtonsoft.Json.Tests.Serialization
[TestFixture]
public class JsonSerializerCollectionsTests : TestFixtureBase
{
[Test]
public void MultiDObjectArray()
{
object[,] myOtherArray =
{
{ new KeyValuePair<string, double>("my value", 0.8), "foobar" },
{ true, 0.4d },
{ 0.05f, 6 }
};
string myOtherArrayAsString = JsonConvert.SerializeObject(myOtherArray, Formatting.Indented);
Assert.AreEqual(@"[
[
{
""Key"": ""my value"",
""Value"": 0.8
},
""foobar""
],
[
true,
0.4
],
[
0.05,
6
]
]", myOtherArrayAsString);
JObject o = JObject.Parse(@"{
""Key"": ""my value"",
""Value"": 0.8
}");
object[,] myOtherResult = JsonConvert.DeserializeObject<object[,]>(myOtherArrayAsString);
Assert.IsTrue(JToken.DeepEquals(o, (JToken)myOtherResult[0, 0]));
Assert.AreEqual("foobar", myOtherResult[0, 1]);
Assert.AreEqual(true, myOtherResult[1, 0]);
Assert.AreEqual(0.4, myOtherResult[1, 1]);
Assert.AreEqual(0.05, myOtherResult[2, 0]);
Assert.AreEqual(6, myOtherResult[2, 1]);
}
public class EnumerableClass<T> : IEnumerable<T>
{
private readonly IList<T> _values;
@@ -212,7 +212,7 @@ namespace Newtonsoft.Json.Utilities
return -1;
}
private static IList<int> GetDimensions(IList values)
private static IList<int> GetDimensions(IList values, int dimensionsCount)
{
IList<int> dimensions = new List<int>();
@@ -220,6 +220,11 @@ namespace Newtonsoft.Json.Utilities
while (true)
{
dimensions.Add(currentArray.Count);
// don't keep calculating dimensions for arrays inside the value array
if (dimensions.Count == dimensionsCount)
break;
if (currentArray.Count == 0)
break;
@@ -277,7 +282,7 @@ namespace Newtonsoft.Json.Utilities
public static Array ToMultidimensionalArray(IList values, Type type, int rank)
{
IList<int> dimensions = GetDimensions(values);
IList<int> dimensions = GetDimensions(values, rank);
while (dimensions.Count < rank)
{