Skip to content

Commit

Permalink
-Fixed serializing non-zero based multidimensional array
Browse files Browse the repository at this point in the history
  • Loading branch information
JamesNK committed Jul 24, 2015
1 parent 3613a89 commit d26d54b
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,62 @@ public void SerializeCustomConcurrentDictionary()
}
#endif

[Test]
public void NonZeroBasedArray()
{
var onebasedArray = Array.CreateInstance(typeof(string), new[] { 3 }, new[] { 2 });

for (var i = onebasedArray.GetLowerBound(0); i <= onebasedArray.GetUpperBound(0); i++)
{
onebasedArray.SetValue(i.ToString(CultureInfo.InvariantCulture), new[] { i, });
}

string output = JsonConvert.SerializeObject(onebasedArray, Formatting.Indented);

StringAssert.AreEqual(@"[
""2"",
""3"",
""4""
]", output);
}

[Test]
public void NonZeroBasedMultiArray()
{
// lets create a two dimensional array, each rank is 1-based of with a capacity of 4.
var onebasedArray = Array.CreateInstance(typeof(string), new[] { 3, 3 }, new[] { 1, 2 });

// Iterate of the array elements and assign a random double
for (var i = onebasedArray.GetLowerBound(0); i <= onebasedArray.GetUpperBound(0); i++)
{
for (var j = onebasedArray.GetLowerBound(1); j <= onebasedArray.GetUpperBound(1); j++)
{
onebasedArray.SetValue(i + "_" + j, new[] { i, j });
}
}

// Now lets try and serialize the Array
string output = JsonConvert.SerializeObject(onebasedArray, Formatting.Indented);

StringAssert.AreEqual(@"[
[
""1_2"",
""1_3"",
""1_4""
],
[
""2_2"",
""2_3"",
""2_4""
],
[
""3_2"",
""3_3"",
""3_4""
]
]", output);
}

[Test]
public void MultiDObjectArray()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -667,7 +667,7 @@ private void SerializeMultidimensionalArray(JsonWriter writer, Array values, Jso

writer.WriteStartArray();

for (int i = 0; i < values.GetLength(dimension); i++)
for (int i = values.GetLowerBound(dimension); i <= values.GetUpperBound(dimension); i++)
{
newIndices[dimension] = i;
bool isTopLevel = (newIndices.Length == values.Rank);
Expand Down

0 comments on commit d26d54b

Please sign in to comment.