-Properties on dynamic objects with JsonProperty/DataMember will now be included in serialized JSON
-Serialize and deserialize multidimensional arrays
This commit is contained in:
@@ -192,6 +192,7 @@ namespace Newtonsoft.Json.Tests.Serialization
|
||||
});
|
||||
|
||||
Assert.AreEqual(@"{
|
||||
""explicit"": false,
|
||||
""text"": ""Text!"",
|
||||
""integer"": 2147483647,
|
||||
""int"": 0,
|
||||
|
||||
@@ -30,6 +30,7 @@ using System.Collections.Generic;
|
||||
using System.Dynamic;
|
||||
using System.Linq;
|
||||
using System.Linq.Expressions;
|
||||
using System.Reflection;
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Runtime.Serialization.Formatters;
|
||||
using System.Text;
|
||||
@@ -60,6 +61,8 @@ namespace Newtonsoft.Json.Tests.Serialization
|
||||
private readonly Dictionary<string, object> _members;
|
||||
|
||||
public int Int;
|
||||
[JsonProperty]
|
||||
public bool Explicit;
|
||||
public DynamicChildObject ChildObject { get; set; }
|
||||
|
||||
internal Dictionary<string, object> Members
|
||||
@@ -122,6 +125,7 @@ namespace Newtonsoft.Json.Tests.Serialization
|
||||
public void SerializeDynamicObject()
|
||||
{
|
||||
TestDynamicObject dynamicObject = new TestDynamicObject();
|
||||
dynamicObject.Explicit = true;
|
||||
|
||||
dynamic d = dynamicObject;
|
||||
d.Int = 1;
|
||||
@@ -144,6 +148,7 @@ namespace Newtonsoft.Json.Tests.Serialization
|
||||
|
||||
string json = JsonConvert.SerializeObject(dynamicObject, Formatting.Indented);
|
||||
Assert.AreEqual(@"{
|
||||
""Explicit"": true,
|
||||
""Decimal"": 99.9,
|
||||
""Int"": 1,
|
||||
""ChildObject"": {
|
||||
@@ -153,6 +158,8 @@ namespace Newtonsoft.Json.Tests.Serialization
|
||||
}", json);
|
||||
|
||||
TestDynamicObject newDynamicObject = JsonConvert.DeserializeObject<TestDynamicObject>(json);
|
||||
Assert.AreEqual(true, newDynamicObject.Explicit);
|
||||
|
||||
d = newDynamicObject;
|
||||
|
||||
Assert.AreEqual(99.9, d.Decimal);
|
||||
|
||||
@@ -6629,6 +6629,186 @@ Parameter name: value",
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
[Test]
|
||||
public void SerializeArray2D()
|
||||
{
|
||||
Array2D aa = new Array2D();
|
||||
aa.Before = "Before!";
|
||||
aa.After = "After!";
|
||||
aa.Coordinates = new[,] { { 1, 1 }, { 1, 2 }, { 2, 1 }, { 2, 2 } };
|
||||
|
||||
string json = JsonConvert.SerializeObject(aa);
|
||||
|
||||
Assert.AreEqual(@"{""Before"":""Before!"",""Coordinates"":[[1,1],[1,2],[2,1],[2,2]],""After"":""After!""}", json);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void SerializeArray3D()
|
||||
{
|
||||
Array3D aa = new Array3D();
|
||||
aa.Before = "Before!";
|
||||
aa.After = "After!";
|
||||
aa.Coordinates = new[, ,] { { { 1, 1, 1 }, { 1, 1, 2 } }, { { 1, 2, 1 }, { 1, 2, 2 } }, { { 2, 1, 1 }, { 2, 1, 2 } }, { { 2, 2, 1 }, { 2, 2, 2 } } };
|
||||
|
||||
string json = JsonConvert.SerializeObject(aa);
|
||||
|
||||
Assert.AreEqual(@"{""Before"":""Before!"",""Coordinates"":[[[1,1,1],[1,1,2]],[[1,2,1],[1,2,2]],[[2,1,1],[2,1,2]],[[2,2,1],[2,2,2]]],""After"":""After!""}", json);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void DeserializeArray2D()
|
||||
{
|
||||
string json = @"{""Before"":""Before!"",""Coordinates"":[[1,1],[1,2],[2,1],[2,2]],""After"":""After!""}";
|
||||
|
||||
Array2D aa = JsonConvert.DeserializeObject<Array2D>(json);
|
||||
|
||||
Assert.AreEqual("Before!", aa.Before);
|
||||
Assert.AreEqual("After!", aa.After);
|
||||
Assert.AreEqual(4, aa.Coordinates.GetLength(0));
|
||||
Assert.AreEqual(2, aa.Coordinates.GetLength(1));
|
||||
Assert.AreEqual(1, aa.Coordinates[0, 0]);
|
||||
Assert.AreEqual(2, aa.Coordinates[1, 1]);
|
||||
|
||||
string after = JsonConvert.SerializeObject(aa);
|
||||
|
||||
Assert.AreEqual(json, after);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void DeserializeArray2D_WithTooManyItems()
|
||||
{
|
||||
string json = @"{""Before"":""Before!"",""Coordinates"":[[1,1],[1,2,3],[2,1],[2,2]],""After"":""After!""}";
|
||||
|
||||
ExceptionAssert.Throws<Exception>(
|
||||
"Cannot deserialize non-cubical array as multidimensional array.",
|
||||
() => JsonConvert.DeserializeObject<Array2D>(json));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void DeserializeArray2D_WithTooFewItems()
|
||||
{
|
||||
string json = @"{""Before"":""Before!"",""Coordinates"":[[1,1],[1],[2,1],[2,2]],""After"":""After!""}";
|
||||
|
||||
ExceptionAssert.Throws<Exception>(
|
||||
"Cannot deserialize non-cubical array as multidimensional array.",
|
||||
() => JsonConvert.DeserializeObject<Array2D>(json));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void DeserializeArray3D()
|
||||
{
|
||||
string json = @"{""Before"":""Before!"",""Coordinates"":[[[1,1,1],[1,1,2]],[[1,2,1],[1,2,2]],[[2,1,1],[2,1,2]],[[2,2,1],[2,2,2]]],""After"":""After!""}";
|
||||
|
||||
Array3D aa = JsonConvert.DeserializeObject<Array3D>(json);
|
||||
|
||||
Assert.AreEqual("Before!", aa.Before);
|
||||
Assert.AreEqual("After!", aa.After);
|
||||
Assert.AreEqual(4, aa.Coordinates.GetLength(0));
|
||||
Assert.AreEqual(2, aa.Coordinates.GetLength(1));
|
||||
Assert.AreEqual(3, aa.Coordinates.GetLength(2));
|
||||
Assert.AreEqual(1, aa.Coordinates[0, 0, 0]);
|
||||
Assert.AreEqual(2, aa.Coordinates[1, 1, 1]);
|
||||
|
||||
string after = JsonConvert.SerializeObject(aa);
|
||||
|
||||
Assert.AreEqual(json, after);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void DeserializeArray3D_WithTooManyItems()
|
||||
{
|
||||
string json = @"{""Before"":""Before!"",""Coordinates"":[[[1,1,1],[1,1,2,1]],[[1,2,1],[1,2,2]],[[2,1,1],[2,1,2]],[[2,2,1],[2,2,2]]],""After"":""After!""}";
|
||||
|
||||
ExceptionAssert.Throws<Exception>(
|
||||
"Cannot deserialize non-cubical array as multidimensional array.",
|
||||
() => JsonConvert.DeserializeObject<Array3D>(json));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void DeserializeArray3D_WithBadItems()
|
||||
{
|
||||
string json = @"{""Before"":""Before!"",""Coordinates"":[[[1,1,1],[1,1,2]],[[1,2,1],[1,2,2]],[[2,1,1],[2,1,2]],[[2,2,1],{}]],""After"":""After!""}";
|
||||
|
||||
ExceptionAssert.Throws<JsonSerializationException>(
|
||||
"Unexpected token when deserializing multidimensional array: StartObject. Path 'Coordinates[3][1]', line 1, position 99.",
|
||||
() => JsonConvert.DeserializeObject<Array3D>(json));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void DeserializeArray3D_WithTooFewItems()
|
||||
{
|
||||
string json = @"{""Before"":""Before!"",""Coordinates"":[[[1,1,1],[1,1]],[[1,2,1],[1,2,2]],[[2,1,1],[2,1,2]],[[2,2,1],[2,2,2]]],""After"":""After!""}";
|
||||
|
||||
ExceptionAssert.Throws<Exception>(
|
||||
"Cannot deserialize non-cubical array as multidimensional array.",
|
||||
() => JsonConvert.DeserializeObject<Array3D>(json));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void SerializeEmpty3DArray()
|
||||
{
|
||||
Array3D aa = new Array3D();
|
||||
aa.Before = "Before!";
|
||||
aa.After = "After!";
|
||||
aa.Coordinates = new int[0, 0, 0];
|
||||
|
||||
string json = JsonConvert.SerializeObject(aa);
|
||||
|
||||
Assert.AreEqual(@"{""Before"":""Before!"",""Coordinates"":[],""After"":""After!""}", json);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void DeserializeEmpty3DArray()
|
||||
{
|
||||
string json = @"{""Before"":""Before!"",""Coordinates"":[],""After"":""After!""}";
|
||||
|
||||
Array3D aa = JsonConvert.DeserializeObject<Array3D>(json);
|
||||
|
||||
Assert.AreEqual("Before!", aa.Before);
|
||||
Assert.AreEqual("After!", aa.After);
|
||||
Assert.AreEqual(0, aa.Coordinates.GetLength(0));
|
||||
Assert.AreEqual(0, aa.Coordinates.GetLength(1));
|
||||
Assert.AreEqual(0, aa.Coordinates.GetLength(2));
|
||||
|
||||
string after = JsonConvert.SerializeObject(aa);
|
||||
|
||||
Assert.AreEqual(json, after);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void DeserializeNull3DArray()
|
||||
{
|
||||
string json = @"{""Before"":""Before!"",""Coordinates"":null,""After"":""After!""}";
|
||||
|
||||
Array3D aa = JsonConvert.DeserializeObject<Array3D>(json);
|
||||
|
||||
Assert.AreEqual("Before!", aa.Before);
|
||||
Assert.AreEqual("After!", aa.After);
|
||||
Assert.AreEqual(null, aa.Coordinates);
|
||||
|
||||
string after = JsonConvert.SerializeObject(aa);
|
||||
|
||||
Assert.AreEqual(json, after);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void DeserializeSemiEmpty3DArray()
|
||||
{
|
||||
string json = @"{""Before"":""Before!"",""Coordinates"":[[]],""After"":""After!""}";
|
||||
|
||||
Array3D aa = JsonConvert.DeserializeObject<Array3D>(json);
|
||||
|
||||
Assert.AreEqual("Before!", aa.Before);
|
||||
Assert.AreEqual("After!", aa.After);
|
||||
Assert.AreEqual(1, aa.Coordinates.GetLength(0));
|
||||
Assert.AreEqual(0, aa.Coordinates.GetLength(1));
|
||||
Assert.AreEqual(0, aa.Coordinates.GetLength(2));
|
||||
|
||||
string after = JsonConvert.SerializeObject(aa);
|
||||
|
||||
Assert.AreEqual(json, after);
|
||||
}
|
||||
}
|
||||
|
||||
#if !(SILVERLIGHT || NETFX_CORE || PORTABLE)
|
||||
@@ -6939,4 +7119,18 @@ Parameter name: value",
|
||||
public string IgnoreDataMemberAndDataMemberAttribute { get; set; }
|
||||
}
|
||||
#endif
|
||||
|
||||
public class Array2D
|
||||
{
|
||||
public string Before { get; set; }
|
||||
public int[,] Coordinates { get; set; }
|
||||
public string After { get; set; }
|
||||
}
|
||||
|
||||
public class Array3D
|
||||
{
|
||||
public string Before { get; set; }
|
||||
public int[,,] Coordinates { get; set; }
|
||||
public string After { get; set; }
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user