-Added ItemIsReference, ItemReferenceLoopHandling, ItemTypeNameHandling, ItemConverterType to JsonPropertyAttribute

This commit is contained in:
James Newton-King
2012-05-05 20:30:02 +12:00
parent 0e22f39e12
commit 2dfd0bc940
14 changed files with 1284 additions and 630 deletions
File diff suppressed because it is too large Load Diff
@@ -1006,6 +1006,74 @@ namespace Newtonsoft.Json.Tests.Serialization
Assert.IsTrue(ReferenceEquals(referenceObject.Component1, referenceObject.Component2));
}
[Test]
public void PropertyItemIsReferenceObject()
{
TestComponentSimple c1 = new TestComponentSimple();
PropertyItemIsReferenceObject o1 = new PropertyItemIsReferenceObject
{
Data = new PropertyItemIsReferenceBody
{
Prop1 = c1,
Prop2 = c1,
Data = new List<TestComponentSimple>
{
c1
}
}
};
string json = JsonConvert.SerializeObject(o1, Formatting.Indented);
Assert.AreEqual(@"{
""Data"": {
""Prop1"": {
""$id"": ""1"",
""MyProperty"": 0
},
""Prop2"": {
""$ref"": ""1""
},
""Data"": {
""$id"": ""2"",
""$values"": [
{
""MyProperty"": 0
}
]
}
}
}", json);
PropertyItemIsReferenceObject o2 = JsonConvert.DeserializeObject<PropertyItemIsReferenceObject>(json);
TestComponentSimple c2 = o2.Data.Prop1;
TestComponentSimple c3 = o2.Data.Prop2;
TestComponentSimple c4 = o2.Data.Data[0];
Assert.IsTrue(ReferenceEquals(c2, c3));
Assert.IsFalse(ReferenceEquals(c2, c4));
}
}
public class PropertyItemIsReferenceBody
{
public TestComponentSimple Prop1 { get; set; }
public TestComponentSimple Prop2 { get; set; }
public IList<TestComponentSimple> Data { get; set; }
}
public class PropertyItemIsReferenceObject
{
[JsonProperty(ItemIsReference = true)]
public PropertyItemIsReferenceBody Data { get; set; }
}
public class PropertyItemIsReferenceList
{
[JsonProperty(ItemIsReference = true)]
public IList<IList<object>> Data { get; set; }
}
[JsonArray(ItemIsReference = true)]
@@ -112,6 +112,62 @@ namespace Newtonsoft.Json.Tests.Serialization
});
Assert.AreEqual("{}", json);
}
[Test]
public void SerializePropertyItemReferenceLoopHandling()
{
PropertyItemReferenceLoopHandling c = new PropertyItemReferenceLoopHandling();
c.Text = "Text!";
c.SetData(new List<PropertyItemReferenceLoopHandling> { c });
string json = JsonConvert.SerializeObject(c, Formatting.Indented);
Assert.AreEqual(@"{
""Text"": ""Text!"",
""Data"": [
{
""Text"": ""Text!"",
""Data"": [
{
""Text"": ""Text!"",
""Data"": [
{
""Text"": ""Text!"",
""Data"": null
}
]
}
]
}
]
}", json);
}
}
public class PropertyItemReferenceLoopHandling
{
private IList<PropertyItemReferenceLoopHandling> _data;
private int _accessCount;
public string Text { get; set; }
[JsonProperty(ItemReferenceLoopHandling = ReferenceLoopHandling.Serialize)]
public IList<PropertyItemReferenceLoopHandling> Data
{
get
{
if (_accessCount >= 3)
return null;
_accessCount++;
return new List<PropertyItemReferenceLoopHandling>(_data);
}
}
public void SetData(IList<PropertyItemReferenceLoopHandling> data)
{
_data = data;
}
}
[JsonArray(ItemReferenceLoopHandling = ReferenceLoopHandling.Ignore)]
File diff suppressed because it is too large Load Diff