-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
@@ -114,6 +114,35 @@ namespace Newtonsoft.Json.Tests.Converters
return node;
}
#if !NET20
[Test]
public void SerializeEmptyDocument()
{
XmlDocument doc = new XmlDocument();
doc.LoadXml("<root />");
string json = JsonConvert.SerializeXmlNode(doc, Formatting.Indented, true);
Assert.AreEqual("null", json);
doc = new XmlDocument();
doc.LoadXml("<root></root>");
json = JsonConvert.SerializeXmlNode(doc, Formatting.Indented, true);
Assert.AreEqual("null", json);
XDocument doc1 = XDocument.Parse("<root />");
json = JsonConvert.SerializeXNode(doc1, Formatting.Indented, true);
Assert.AreEqual("null", json);
doc1 = XDocument.Parse("<root></root>");
json = JsonConvert.SerializeXNode(doc1, Formatting.Indented, true);
Assert.AreEqual("null", json);
}
#endif
[Test]
public void DocumentSerializeIndented()
{
+17 -17
View File
@@ -712,27 +712,27 @@ namespace Newtonsoft.Json.Tests.Linq
""StockValue"": 22050.0
}", json);
}
}
public class DynamicDictionary : DynamicObject
public class DynamicDictionary : DynamicObject
{
private readonly IDictionary<string, object> _values = new Dictionary<string, object>();
public override IEnumerable<string> GetDynamicMemberNames()
{
private readonly IDictionary<string, object> _values = new Dictionary<string, object>();
return _values.Keys;
}
public override IEnumerable<string> GetDynamicMemberNames()
{
return _values.Keys;
}
public override bool TryGetMember(GetMemberBinder binder, out object result)
{
result = _values[binder.Name];
return true;
}
public override bool TryGetMember(GetMemberBinder binder, out object result)
{
result = _values[binder.Name];
return true;
}
public override bool TrySetMember(SetMemberBinder binder, object value)
{
_values[binder.Name] = value;
return true;
}
public override bool TrySetMember(SetMemberBinder binder, object value)
{
_values[binder.Name] = value;
return true;
}
}
}
@@ -72,5 +72,5 @@ using System.Security;
// by using the '*' as shown below:
[assembly: AssemblyVersion("4.5.0.0")]
#if !PocketPC
[assembly: AssemblyFileVersion("4.5.4.14902")]
[assembly: AssemblyFileVersion("4.5.4.14905")]
#endif
@@ -751,6 +751,33 @@ namespace Newtonsoft.Json.Tests.Schema
}
}", json);
}
[Test]
public void GenerateForNullableInt32()
{
JsonSchemaGenerator jsonSchemaGenerator = new JsonSchemaGenerator();
JsonSchema jsonSchema = jsonSchemaGenerator.Generate(typeof(NullableInt32TestClass));
string json = jsonSchema.ToString();
Assert.AreEqual(@"{
""type"": ""object"",
""properties"": {
""Value"": {
""required"": true,
""type"": [
""integer"",
""null""
]
}
}
}", json);
}
}
public class NullableInt32TestClass
{
public int? Value { get; set; }
}
public class DMDSLBase
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