Refactor type name multidimensional array support and add tests

This commit is contained in:
James Newton-King
2021-11-14 18:04:58 +13:00
parent 233ed3a961
commit af6e7b9c13
3 changed files with 130 additions and 5 deletions
@@ -62,6 +62,125 @@ namespace Newtonsoft.Json.Tests.Serialization
[TestFixture]
public class TypeNameHandlingTests : TestFixtureBase
{
[Test]
public void SerializeMultidimensionalByteArrayWithTypeName()
{
string array2dRef = ReflectionUtils.GetTypeName(typeof(byte[,]), TypeNameAssemblyFormatHandling.Simple, null);
string array3dRef = ReflectionUtils.GetTypeName(typeof(byte[,,]), TypeNameAssemblyFormatHandling.Simple, null);
HasMultidimensionalByteArray o = new HasMultidimensionalByteArray
{
Array2D = new byte[,] { { 1, 2 }, { 2, 4 }, { 3, 6 } },
Array3D = new byte[,,] { { { 1, 2, 3}, { 4, 5, 6 } } }
};
string json = JsonConvert.SerializeObject(o, new JsonSerializerSettings
{
TypeNameHandling = TypeNameHandling.All,
Formatting = Formatting.Indented
});
string expectedJson = @"{
""$type"": ""Newtonsoft.Json.Tests.TestObjects.HasMultidimensionalByteArray, Newtonsoft.Json.Tests"",
""Array2D"": {
""$type"": """ + array2dRef + @""",
""$values"": [
[
1,
2
],
[
2,
4
],
[
3,
6
]
]
},
""Array3D"": {
""$type"": """ + array3dRef + @""",
""$values"": [
[
[
1,
2,
3
],
[
4,
5,
6
]
]
]
}
}";
StringAssert.AreEqual(expectedJson, json);
}
[Test]
public void DeserializeMultidimensionalByteArrayWithTypeName()
{
string json = @"{
""$type"": ""Newtonsoft.Json.Tests.TestObjects.HasMultidimensionalByteArray, Newtonsoft.Json.Tests"",
""Array2D"": {
""$type"": ""System.Byte[,], mscorlib"",
""$values"": [
[
1,
2
],
[
2,
4
],
[
3,
6
]
]
},
""Array3D"": {
""$type"": ""System.Byte[,,], mscorlib"",
""$values"": [
[
[
1,
2,
3
],
[
4,
5,
6
]
]
]
}
}";
HasMultidimensionalByteArray value = JsonConvert.DeserializeObject<HasMultidimensionalByteArray>(json, new JsonSerializerSettings
{
TypeNameHandling = TypeNameHandling.Objects
});
Assert.AreEqual(1, value.Array2D[0, 0]);
Assert.AreEqual(2, value.Array2D[0, 1]);
Assert.AreEqual(2, value.Array2D[1, 0]);
Assert.AreEqual(4, value.Array2D[1, 1]);
Assert.AreEqual(3, value.Array2D[2, 0]);
Assert.AreEqual(6, value.Array2D[2, 1]);
Assert.AreEqual(1, value.Array3D[0, 0, 0]);
Assert.AreEqual(2, value.Array3D[0, 0, 1]);
Assert.AreEqual(3, value.Array3D[0, 0, 2]);
Assert.AreEqual(4, value.Array3D[0, 1, 0]);
Assert.AreEqual(5, value.Array3D[0, 1, 1]);
Assert.AreEqual(6, value.Array3D[0, 1, 2]);
}
[Test]
public void DeserializeByteArrayWithTypeName()
@@ -29,4 +29,10 @@ namespace Newtonsoft.Json.Tests.TestObjects
{
public byte[] EncryptedPassword { get; set; }
}
internal class HasMultidimensionalByteArray
{
public byte[,] Array2D { get; set; }
public byte[,,] Array3D { get; set; }
}
}
@@ -187,7 +187,7 @@ namespace Newtonsoft.Json.Utilities
// loop through the type name and filter out qualified assembly details from nested type names
bool writingAssemblyName = false;
bool skippingAssemblyDetails = false;
bool DimensionArrayMaybe = false;
bool followBrackets = false;
for (int i = 0; i < fullyQualifiedTypeName.Length; i++)
{
char current = fullyQualifiedTypeName[i];
@@ -196,17 +196,17 @@ namespace Newtonsoft.Json.Utilities
case '[':
writingAssemblyName = false;
skippingAssemblyDetails = false;
DimensionArrayMaybe = true;
followBrackets = true;
builder.Append(current);
break;
case ']':
writingAssemblyName = false;
skippingAssemblyDetails = false;
DimensionArrayMaybe = false;
followBrackets = false;
builder.Append(current);
break;
case ',':
if (DimensionArrayMaybe)
if (followBrackets)
{
builder.Append(current);
}
@@ -221,7 +221,7 @@ namespace Newtonsoft.Json.Utilities
}
break;
default:
DimensionArrayMaybe = false;
followBrackets = false;
if (!skippingAssemblyDetails)
{
builder.Append(current);