-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()
{
@@ -712,6 +712,7 @@ namespace Newtonsoft.Json.Tests.Linq
""StockValue"": 22050.0
}", json);
}
}
public class DynamicDictionary : DynamicObject
{
@@ -734,6 +735,5 @@ namespace Newtonsoft.Json.Tests.Linq
return true;
}
}
}
}
#endif
@@ -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)]
@@ -24,6 +24,10 @@
#endregion
#if !PORTABLE
#if !(NET35 || NET20 || WINDOWS_PHONE || PORTABLE)
using System.Dynamic;
#endif
using Newtonsoft.Json.Tests.Linq;
using global::System;
using global::System.Collections;
using global::System.Collections.Generic;
@@ -65,7 +69,7 @@ namespace Newtonsoft.Json.Tests.Serialization
};
wrapper.Dictionary = new Dictionary<string, EmployeeReference>
{
{ "First", new EmployeeReference() }
{"First", new EmployeeReference()}
};
string json = JsonConvert.SerializeObject(wrapper, Formatting.Indented, new JsonSerializerSettings
@@ -91,14 +95,14 @@ namespace Newtonsoft.Json.Tests.Serialization
}", json);
Wrapper w2 = JsonConvert.DeserializeObject<Wrapper>(json);
CustomAssert.IsInstanceOfType(typeof(List<EmployeeReference>), w2.Array);
CustomAssert.IsInstanceOfType(typeof(Dictionary<string, EmployeeReference>), w2.Dictionary);
CustomAssert.IsInstanceOfType(typeof (List<EmployeeReference>), w2.Array);
CustomAssert.IsInstanceOfType(typeof (Dictionary<string, EmployeeReference>), w2.Dictionary);
}
[Test]
public void WriteTypeNameForObjects()
{
string employeeRef = ReflectionUtils.GetTypeName(typeof(EmployeeReference), FormatterAssemblyStyle.Simple);
string employeeRef = ReflectionUtils.GetTypeName(typeof (EmployeeReference), FormatterAssemblyStyle.Simple);
EmployeeReference employee = new EmployeeReference();
@@ -118,7 +122,7 @@ namespace Newtonsoft.Json.Tests.Serialization
[Test]
public void DeserializeTypeName()
{
string employeeRef = ReflectionUtils.GetTypeName(typeof(EmployeeReference), FormatterAssemblyStyle.Simple);
string employeeRef = ReflectionUtils.GetTypeName(typeof (EmployeeReference), FormatterAssemblyStyle.Simple);
string json = @"{
""$id"": ""1"",
@@ -132,15 +136,15 @@ namespace Newtonsoft.Json.Tests.Serialization
TypeNameHandling = TypeNameHandling.Objects
});
CustomAssert.IsInstanceOfType(typeof(EmployeeReference), employee);
Assert.AreEqual("Name!", ((EmployeeReference)employee).Name);
CustomAssert.IsInstanceOfType(typeof (EmployeeReference), employee);
Assert.AreEqual("Name!", ((EmployeeReference) employee).Name);
}
#if !(SILVERLIGHT || NETFX_CORE || PORTABLE)
[Test]
public void DeserializeTypeNameFromGacAssembly()
{
string cookieRef = ReflectionUtils.GetTypeName(typeof(Cookie), FormatterAssemblyStyle.Simple);
string cookieRef = ReflectionUtils.GetTypeName(typeof (Cookie), FormatterAssemblyStyle.Simple);
string json = @"{
""$id"": ""1"",
@@ -152,15 +156,15 @@ namespace Newtonsoft.Json.Tests.Serialization
TypeNameHandling = TypeNameHandling.Objects
});
CustomAssert.IsInstanceOfType(typeof(Cookie), cookie);
CustomAssert.IsInstanceOfType(typeof (Cookie), cookie);
}
#endif
[Test]
public void SerializeGenericObjectListWithTypeName()
{
string employeeRef = typeof(EmployeeReference).AssemblyQualifiedName;
string personRef = typeof(Person).AssemblyQualifiedName;
string employeeRef = typeof (EmployeeReference).AssemblyQualifiedName;
string personRef = typeof (Person).AssemblyQualifiedName;
List<object> values = new List<object>
{
@@ -211,8 +215,8 @@ namespace Newtonsoft.Json.Tests.Serialization
[Test]
public void DeserializeGenericObjectListWithTypeName()
{
string employeeRef = typeof(EmployeeReference).AssemblyQualifiedName;
string personRef = typeof(Person).AssemblyQualifiedName;
string employeeRef = typeof (EmployeeReference).AssemblyQualifiedName;
string personRef = typeof (Person).AssemblyQualifiedName;
string json = @"[
{
@@ -236,7 +240,7 @@ namespace Newtonsoft.Json.Tests.Serialization
-2147483648
]";
List<object> values = (List<object>)JsonConvert.DeserializeObject(json, typeof(List<object>), new JsonSerializerSettings
List<object> values = (List<object>) JsonConvert.DeserializeObject(json, typeof (List<object>), new JsonSerializerSettings
{
TypeNameHandling = TypeNameHandling.Objects,
TypeNameAssemblyFormat = FormatterAssemblyStyle.Full
@@ -244,8 +248,8 @@ namespace Newtonsoft.Json.Tests.Serialization
Assert.AreEqual(4, values.Count);
EmployeeReference e = (EmployeeReference)values[0];
Person p = (Person)values[1];
EmployeeReference e = (EmployeeReference) values[0];
Person p = (Person) values[1];
Assert.AreEqual("Bob", e.Name);
Assert.AreEqual("Frank", e.Manager.Name);
@@ -255,14 +259,14 @@ namespace Newtonsoft.Json.Tests.Serialization
Assert.AreEqual(new DateTime(2000, 12, 30, 0, 0, 0, DateTimeKind.Utc), p.LastModified);
Assert.AreEqual("String!", values[2]);
Assert.AreEqual((long)int.MinValue, values[3]);
Assert.AreEqual((long) int.MinValue, values[3]);
}
[Test]
public void DeserializeWithBadTypeName()
{
string employeeRef = typeof(EmployeeReference).AssemblyQualifiedName;
string personRef = typeof(Person).AssemblyQualifiedName;
string employeeRef = typeof (EmployeeReference).AssemblyQualifiedName;
string personRef = typeof (Person).AssemblyQualifiedName;
string json = @"{
""$id"": ""1"",
@@ -273,7 +277,7 @@ namespace Newtonsoft.Json.Tests.Serialization
try
{
JsonConvert.DeserializeObject(json, typeof(Person), new JsonSerializerSettings
JsonConvert.DeserializeObject(json, typeof (Person), new JsonSerializerSettings
{
TypeNameHandling = TypeNameHandling.Objects,
TypeNameAssemblyFormat = FormatterAssemblyStyle.Full
@@ -288,7 +292,7 @@ namespace Newtonsoft.Json.Tests.Serialization
[Test]
public void DeserializeTypeNameWithNoTypeNameHandling()
{
string employeeRef = typeof(EmployeeReference).AssemblyQualifiedName;
string employeeRef = typeof (EmployeeReference).AssemblyQualifiedName;
string json = @"{
""$id"": ""1"",
@@ -297,7 +301,7 @@ namespace Newtonsoft.Json.Tests.Serialization
""Manager"": null
}";
JObject o = (JObject)JsonConvert.DeserializeObject(json);
JObject o = (JObject) JsonConvert.DeserializeObject(json);
Assert.AreEqual(@"{
""Name"": ""Name!"",
@@ -338,6 +342,7 @@ namespace Newtonsoft.Json.Tests.Serialization
RequestEncoding = "UTF-8";
Method = "GET";
}
public string Method { get; set; }
public Dictionary<string, string> Headers { get; set; }
public string Url { get; set; }
@@ -352,7 +357,7 @@ namespace Newtonsoft.Json.Tests.Serialization
[Test]
public void DeserializeGenericTypeName()
{
string typeName = typeof(SendHttpRequest).AssemblyQualifiedName;
string typeName = typeof (SendHttpRequest).AssemblyQualifiedName;
string json = @"{
""$type"": """ + typeName + @""",
@@ -373,9 +378,9 @@ namespace Newtonsoft.Json.Tests.Serialization
TypeNameAssemblyFormat = FormatterAssemblyStyle.Full
});
CustomAssert.IsInstanceOfType(typeof(SendHttpRequest), message);
CustomAssert.IsInstanceOfType(typeof (SendHttpRequest), message);
SendHttpRequest request = (SendHttpRequest)message;
SendHttpRequest request = (SendHttpRequest) message;
Assert.AreEqual("xyz", request.CorrelationId);
Assert.AreEqual(2, request.RequestData.Count);
Assert.AreEqual("siedemnaście", request.RequestData["Id"]);
@@ -384,8 +389,8 @@ namespace Newtonsoft.Json.Tests.Serialization
[Test]
public void SerializeObjectWithMultipleGenericLists()
{
string containerTypeName = typeof(Container).AssemblyQualifiedName;
string productListTypeName = typeof(List<Product>).AssemblyQualifiedName;
string containerTypeName = typeof (Container).AssemblyQualifiedName;
string productListTypeName = typeof (List<Product>).AssemblyQualifiedName;
Container container = new Container
{
@@ -417,6 +422,7 @@ namespace Newtonsoft.Json.Tests.Serialization
public class TypeNameProperty
{
public string Name { get; set; }
[JsonProperty(TypeNameHandling = TypeNameHandling.All)]
public object Value { get; set; }
}
@@ -424,7 +430,7 @@ namespace Newtonsoft.Json.Tests.Serialization
[Test]
public void WriteObjectTypeNameForProperty()
{
string typeNamePropertyRef = ReflectionUtils.GetTypeName(typeof(TypeNameProperty), FormatterAssemblyStyle.Simple);
string typeNamePropertyRef = ReflectionUtils.GetTypeName(typeof (TypeNameProperty), FormatterAssemblyStyle.Simple);
TypeNameProperty typeNameProperty = new TypeNameProperty
{
@@ -448,9 +454,9 @@ namespace Newtonsoft.Json.Tests.Serialization
TypeNameProperty deserialized = JsonConvert.DeserializeObject<TypeNameProperty>(json);
Assert.AreEqual("Name!", deserialized.Name);
CustomAssert.IsInstanceOfType(typeof(TypeNameProperty), deserialized.Value);
CustomAssert.IsInstanceOfType(typeof (TypeNameProperty), deserialized.Value);
TypeNameProperty nested = (TypeNameProperty)deserialized.Value;
TypeNameProperty nested = (TypeNameProperty) deserialized.Value;
Assert.AreEqual("Nested!", nested.Name);
Assert.AreEqual(null, nested.Value);
}
@@ -458,12 +464,12 @@ namespace Newtonsoft.Json.Tests.Serialization
[Test]
public void WriteListTypeNameForProperty()
{
string listRef = ReflectionUtils.GetTypeName(typeof(List<int>), FormatterAssemblyStyle.Simple);
string listRef = ReflectionUtils.GetTypeName(typeof (List<int>), FormatterAssemblyStyle.Simple);
TypeNameProperty typeNameProperty = new TypeNameProperty
{
Name = "Name!",
Value = new List<int> { 1, 2, 3, 4, 5 }
Value = new List<int> {1, 2, 3, 4, 5}
};
string json = JsonConvert.SerializeObject(typeNameProperty, Formatting.Indented);
@@ -484,9 +490,9 @@ namespace Newtonsoft.Json.Tests.Serialization
TypeNameProperty deserialized = JsonConvert.DeserializeObject<TypeNameProperty>(json);
Assert.AreEqual("Name!", deserialized.Name);
CustomAssert.IsInstanceOfType(typeof(List<int>), deserialized.Value);
CustomAssert.IsInstanceOfType(typeof (List<int>), deserialized.Value);
List<int> nested = (List<int>)deserialized.Value;
List<int> nested = (List<int>) deserialized.Value;
Assert.AreEqual(5, nested.Count);
Assert.AreEqual(1, nested[0]);
Assert.AreEqual(2, nested[1]);
@@ -510,9 +516,9 @@ namespace Newtonsoft.Json.Tests.Serialization
Binder = new CustomSerializationBinder()
});
CustomAssert.IsInstanceOfType(typeof(Person), p);
CustomAssert.IsInstanceOfType(typeof (Person), p);
Person person = (Person)p;
Person person = (Person) p;
Assert.AreEqual("Name!", person.Name);
}
@@ -521,7 +527,7 @@ namespace Newtonsoft.Json.Tests.Serialization
{
public override Type BindToType(string assemblyName, string typeName)
{
return typeof(Person);
return typeof (Person);
}
}
@@ -584,12 +590,12 @@ namespace Newtonsoft.Json.Tests.Serialization
Binder = new TypeNameSerializationBinder("Newtonsoft.Json.Tests.Serialization.{0}, Newtonsoft.Json.Tests")
});
CustomAssert.IsInstanceOfType(typeof(Customer), newValues[0]);
Customer customer = (Customer)newValues[0];
CustomAssert.IsInstanceOfType(typeof (Customer), newValues[0]);
Customer customer = (Customer) newValues[0];
Assert.AreEqual("Caroline Customer", customer.Name);
CustomAssert.IsInstanceOfType(typeof(Purchase), newValues[1]);
Purchase purchase = (Purchase)newValues[1];
CustomAssert.IsInstanceOfType(typeof (Purchase), newValues[1]);
Purchase purchase = (Purchase) newValues[1];
Assert.AreEqual("Elbow Grease", purchase.ProductName);
}
@@ -640,9 +646,9 @@ namespace Newtonsoft.Json.Tests.Serialization
string json = sw.ToString();
string contentSubClassRef = ReflectionUtils.GetTypeName(typeof(ContentSubClass), FormatterAssemblyStyle.Simple);
string dictionaryRef = ReflectionUtils.GetTypeName(typeof(Dictionary<int, IList<ContentBaseClass>>), FormatterAssemblyStyle.Simple);
string listRef = ReflectionUtils.GetTypeName(typeof(List<ContentBaseClass>), FormatterAssemblyStyle.Simple);
string contentSubClassRef = ReflectionUtils.GetTypeName(typeof (ContentSubClass), FormatterAssemblyStyle.Simple);
string dictionaryRef = ReflectionUtils.GetTypeName(typeof (Dictionary<int, IList<ContentBaseClass>>), FormatterAssemblyStyle.Simple);
string listRef = ReflectionUtils.GetTypeName(typeof (List<ContentBaseClass>), FormatterAssemblyStyle.Simple);
string expected = @"{
""TestMember"": {
@@ -680,15 +686,15 @@ namespace Newtonsoft.Json.Tests.Serialization
}
Assert.IsNotNull(anotherTestObject);
CustomAssert.IsInstanceOfType(typeof(ContentSubClass), anotherTestObject.TestMember);
CustomAssert.IsInstanceOfType(typeof(Dictionary<int, IList<ContentBaseClass>>), anotherTestObject.AnotherTestMember);
CustomAssert.IsInstanceOfType(typeof (ContentSubClass), anotherTestObject.TestMember);
CustomAssert.IsInstanceOfType(typeof (Dictionary<int, IList<ContentBaseClass>>), anotherTestObject.AnotherTestMember);
Assert.AreEqual(1, anotherTestObject.AnotherTestMember.Count);
IList<ContentBaseClass> list = anotherTestObject.AnotherTestMember[1];
CustomAssert.IsInstanceOfType(typeof(List<ContentBaseClass>), list);
CustomAssert.IsInstanceOfType(typeof (List<ContentBaseClass>), list);
Assert.AreEqual(1, list.Count);
CustomAssert.IsInstanceOfType(typeof(ContentSubClass), list[0]);
CustomAssert.IsInstanceOfType(typeof (ContentSubClass), list[0]);
}
[Test]
@@ -714,7 +720,7 @@ namespace Newtonsoft.Json.Tests.Serialization
Message deserialized = JsonConvert.DeserializeObject<Message>(json);
SearchDetails searchDetails = (SearchDetails)deserialized.Body;
SearchDetails searchDetails = (SearchDetails) deserialized.Body;
// Json.NET
}
@@ -730,9 +736,10 @@ namespace Newtonsoft.Json.Tests.Serialization
{
Dictionary<string, object> collection = new Dictionary<string, object>()
{
{"First", new UrlStatus{ Status = 404, Url = @"http://www.bing.com"}},
{"Second", new UrlStatus{Status = 400, Url = @"http://www.google.com"}},
{"List", new List<UrlStatus>
{"First", new UrlStatus {Status = 404, Url = @"http://www.bing.com"}},
{"Second", new UrlStatus {Status = 400, Url = @"http://www.google.com"}},
{
"List", new List<UrlStatus>
{
new UrlStatus {Status = 300, Url = @"http://www.yahoo.com"},
new UrlStatus {Status = 200, Url = @"http://www.askjeeves.com"}
@@ -746,7 +753,7 @@ namespace Newtonsoft.Json.Tests.Serialization
TypeNameAssemblyFormat = FormatterAssemblyStyle.Simple
});
string urlStatusTypeName = ReflectionUtils.GetTypeName(typeof(UrlStatus), FormatterAssemblyStyle.Simple);
string urlStatusTypeName = ReflectionUtils.GetTypeName(typeof (UrlStatus), FormatterAssemblyStyle.Simple);
Assert.AreEqual(@"{
""$type"": ""System.Collections.Generic.Dictionary`2[[System.String, mscorlib],[System.Object, mscorlib]], mscorlib"",
@@ -783,13 +790,13 @@ namespace Newtonsoft.Json.Tests.Serialization
TypeNameAssemblyFormat = FormatterAssemblyStyle.Simple
});
CustomAssert.IsInstanceOfType(typeof(Dictionary<string, object>), c);
CustomAssert.IsInstanceOfType(typeof (Dictionary<string, object>), c);
Dictionary<string, object> newCollection = (Dictionary<string, object>)c;
Dictionary<string, object> newCollection = (Dictionary<string, object>) c;
Assert.AreEqual(3, newCollection.Count);
Assert.AreEqual(@"http://www.bing.com", ((UrlStatus)newCollection["First"]).Url);
Assert.AreEqual(@"http://www.bing.com", ((UrlStatus) newCollection["First"]).Url);
List<UrlStatus> statues = (List<UrlStatus>)newCollection["List"];
List<UrlStatus> statues = (List<UrlStatus>) newCollection["List"];
Assert.AreEqual(2, statues.Count);
}
@@ -797,11 +804,11 @@ namespace Newtonsoft.Json.Tests.Serialization
[Test]
public void SerializingIEnumerableOfTShouldRetainGenericTypeInfo()
{
string productClassRef = ReflectionUtils.GetTypeName(typeof(Product[]), FormatterAssemblyStyle.Simple);
string productClassRef = ReflectionUtils.GetTypeName(typeof (Product[]), FormatterAssemblyStyle.Simple);
CustomEnumerable<Product> products = new CustomEnumerable<Product>();
string json = JsonConvert.SerializeObject(products, Formatting.Indented, new JsonSerializerSettings { TypeNameHandling = TypeNameHandling.All });
string json = JsonConvert.SerializeObject(products, Formatting.Indented, new JsonSerializerSettings {TypeNameHandling = TypeNameHandling.All});
Assert.AreEqual(@"{
""$type"": """ + productClassRef + @""",
@@ -872,8 +879,8 @@ namespace Newtonsoft.Json.Tests.Serialization
{
Car testerObject = new Car();
testerObject.Year = new DateTime(2000, 10, 5, 1, 1, 1, DateTimeKind.Utc);
byte[] data = new byte[] { 75, 65, 82, 73, 82, 65 };
testerObject.Objects = new object[] { data, "prueba" };
byte[] data = new byte[] {75, 65, 82, 73, 82, 65};
testerObject.Objects = new object[] {data, "prueba"};
JsonSerializerSettings jsonSettings = new JsonSerializerSettings();
jsonSettings.NullValueHandling = NullValueHandling.Ignore;
@@ -881,7 +888,7 @@ namespace Newtonsoft.Json.Tests.Serialization
string output = JsonConvert.SerializeObject(testerObject, Formatting.Indented, jsonSettings);
string carClassRef = ReflectionUtils.GetTypeName(typeof(Car), FormatterAssemblyStyle.Simple);
string carClassRef = ReflectionUtils.GetTypeName(typeof (Car), FormatterAssemblyStyle.Simple);
Assert.AreEqual(output, @"{
""$type"": """ + carClassRef + @""",
@@ -903,7 +910,7 @@ namespace Newtonsoft.Json.Tests.Serialization
Assert.IsTrue(obj.Objects[0] is byte[]);
byte[] d = (byte[])obj.Objects[0];
byte[] d = (byte[]) obj.Objects[0];
CollectionAssert.AreEquivalent(data, d);
}
@@ -1065,17 +1072,17 @@ namespace Newtonsoft.Json.Tests.Serialization
TypeNameList<object> l2 = JsonConvert.DeserializeObject<TypeNameList<object>>(json);
Assert.AreEqual(4, l2.Count);
CustomAssert.IsInstanceOfType(typeof(TestComponentSimple), l2[0]);
CustomAssert.IsInstanceOfType(typeof(Employee), l2[1]);
CustomAssert.IsInstanceOfType(typeof(string), l2[2]);
CustomAssert.IsInstanceOfType(typeof(long), l2[3]);
CustomAssert.IsInstanceOfType(typeof (TestComponentSimple), l2[0]);
CustomAssert.IsInstanceOfType(typeof (Employee), l2[1]);
CustomAssert.IsInstanceOfType(typeof (string), l2[2]);
CustomAssert.IsInstanceOfType(typeof (long), l2[3]);
}
[Test]
public void TypeNameDictionary()
{
TypeNameDictionary<object> l = new TypeNameDictionary<object>();
l.Add("First", new TestComponentSimple { MyProperty = 1 });
l.Add("First", new TestComponentSimple {MyProperty = 1});
l.Add("Second", "String!");
l.Add("Third", long.MaxValue);
@@ -1092,10 +1099,10 @@ namespace Newtonsoft.Json.Tests.Serialization
TypeNameDictionary<object> l2 = JsonConvert.DeserializeObject<TypeNameDictionary<object>>(json);
Assert.AreEqual(3, l2.Count);
CustomAssert.IsInstanceOfType(typeof(TestComponentSimple), l2["First"]);
Assert.AreEqual(1, ((TestComponentSimple)l2["First"]).MyProperty);
CustomAssert.IsInstanceOfType(typeof(string), l2["Second"]);
CustomAssert.IsInstanceOfType(typeof(long), l2["Third"]);
CustomAssert.IsInstanceOfType(typeof (TestComponentSimple), l2["First"]);
Assert.AreEqual(1, ((TestComponentSimple) l2["First"]).MyProperty);
CustomAssert.IsInstanceOfType(typeof (string), l2["Second"]);
CustomAssert.IsInstanceOfType(typeof (long), l2["Third"]);
}
[Test]
@@ -1103,9 +1110,9 @@ namespace Newtonsoft.Json.Tests.Serialization
{
TypeNameObject o1 = new TypeNameObject();
o1.Object1 = new TestComponentSimple { MyProperty = 1 };
o1.Object1 = new TestComponentSimple {MyProperty = 1};
o1.Object2 = 123;
o1.ObjectNotHandled = new TestComponentSimple { MyProperty = int.MaxValue };
o1.ObjectNotHandled = new TestComponentSimple {MyProperty = int.MaxValue};
o1.String = "String!";
o1.Integer = int.MaxValue;
@@ -1127,14 +1134,327 @@ namespace Newtonsoft.Json.Tests.Serialization
TypeNameObject o2 = JsonConvert.DeserializeObject<TypeNameObject>(json);
Assert.IsNotNull(o2);
CustomAssert.IsInstanceOfType(typeof(TestComponentSimple), o2.Object1);
Assert.AreEqual(1, ((TestComponentSimple)o2.Object1).MyProperty);
CustomAssert.IsInstanceOfType(typeof(long), o2.Object2);
CustomAssert.IsInstanceOfType(typeof(JObject), o2.ObjectNotHandled);
CustomAssert.IsInstanceOfType(typeof (TestComponentSimple), o2.Object1);
Assert.AreEqual(1, ((TestComponentSimple) o2.Object1).MyProperty);
CustomAssert.IsInstanceOfType(typeof (long), o2.Object2);
CustomAssert.IsInstanceOfType(typeof (JObject), o2.ObjectNotHandled);
Assert.AreEqual(@"{
""MyProperty"": 2147483647
}", o2.ObjectNotHandled.ToString());
}
[Test]
public void PropertyItemTypeNameHandling()
{
PropertyItemTypeNameHandling c1 = new PropertyItemTypeNameHandling();
c1.Data = new List<object>
{
1,
"two",
new TestComponentSimple {MyProperty = 1}
};
string json = JsonConvert.SerializeObject(c1, Formatting.Indented);
Assert.AreEqual(@"{
""Data"": [
1,
""two"",
{
""$type"": ""Newtonsoft.Json.Tests.Serialization.TestComponentSimple, Newtonsoft.Json.Tests"",
""MyProperty"": 1
}
]
}", json);
PropertyItemTypeNameHandling c2 = JsonConvert.DeserializeObject<PropertyItemTypeNameHandling>(json);
Assert.AreEqual(3, c2.Data.Count);
CustomAssert.IsInstanceOfType(typeof (long), c2.Data[0]);
CustomAssert.IsInstanceOfType(typeof (string), c2.Data[1]);
CustomAssert.IsInstanceOfType(typeof (TestComponentSimple), c2.Data[2]);
TestComponentSimple c = (TestComponentSimple) c2.Data[2];
Assert.AreEqual(1, c.MyProperty);
}
[Test]
public void PropertyItemTypeNameHandlingNestedCollections()
{
PropertyItemTypeNameHandling c1 = new PropertyItemTypeNameHandling
{
Data = new List<object>
{
new TestComponentSimple {MyProperty = 1},
new List<object>
{
new List<object>
{
new List<object>()
}
}
}
};
string json = JsonConvert.SerializeObject(c1, Formatting.Indented);
Assert.AreEqual(@"{
""Data"": [
{
""$type"": ""Newtonsoft.Json.Tests.Serialization.TestComponentSimple, Newtonsoft.Json.Tests"",
""MyProperty"": 1
},
{
""$type"": ""System.Collections.Generic.List`1[[System.Object, mscorlib]], mscorlib"",
""$values"": [
[
[]
]
]
}
]
}", json);
PropertyItemTypeNameHandling c2 = JsonConvert.DeserializeObject<PropertyItemTypeNameHandling>(json);
Assert.AreEqual(2, c2.Data.Count);
CustomAssert.IsInstanceOfType(typeof (TestComponentSimple), c2.Data[0]);
CustomAssert.IsInstanceOfType(typeof (List<object>), c2.Data[1]);
List<object> c = (List<object>) c2.Data[1];
CustomAssert.IsInstanceOfType(typeof (JArray), c[0]);
json = @"{
""Data"": [
{
""$type"": ""Newtonsoft.Json.Tests.Serialization.TestComponentSimple, Newtonsoft.Json.Tests"",
""MyProperty"": 1
},
{
""$type"": ""System.Collections.Generic.List`1[[System.Object, mscorlib]], mscorlib"",
""$values"": [
{
""$type"": ""Newtonsoft.Json.Tests.Serialization.TestComponentSimple, Newtonsoft.Json.Tests"",
""MyProperty"": 1
}
]
}
]
}";
c2 = JsonConvert.DeserializeObject<PropertyItemTypeNameHandling>(json);
Assert.AreEqual(2, c2.Data.Count);
CustomAssert.IsInstanceOfType(typeof (TestComponentSimple), c2.Data[0]);
CustomAssert.IsInstanceOfType(typeof (List<object>), c2.Data[1]);
c = (List<object>) c2.Data[1];
CustomAssert.IsInstanceOfType(typeof (JObject), c[0]);
JObject o = (JObject) c[0];
Assert.AreEqual(1, (int) o["MyProperty"]);
}
[Test]
public void PropertyItemTypeNameHandlingNestedDictionaries()
{
PropertyItemTypeNameHandlingDictionary c1 = new PropertyItemTypeNameHandlingDictionary()
{
Data = new Dictionary<string, object>
{
{
"one", new TestComponentSimple {MyProperty = 1}
},
{
"two", new Dictionary<string, object>
{
{
"one", new Dictionary<string, object>
{
{"one", 1}
}
}
}
}
}
};
string json = JsonConvert.SerializeObject(c1, Formatting.Indented);
Assert.AreEqual(@"{
""Data"": {
""one"": {
""$type"": ""Newtonsoft.Json.Tests.Serialization.TestComponentSimple, Newtonsoft.Json.Tests"",
""MyProperty"": 1
},
""two"": {
""$type"": ""System.Collections.Generic.Dictionary`2[[System.String, mscorlib],[System.Object, mscorlib]], mscorlib"",
""one"": {
""one"": 1
}
}
}
}", json);
PropertyItemTypeNameHandlingDictionary c2 = JsonConvert.DeserializeObject<PropertyItemTypeNameHandlingDictionary>(json);
Assert.AreEqual(2, c2.Data.Count);
CustomAssert.IsInstanceOfType(typeof (TestComponentSimple), c2.Data["one"]);
CustomAssert.IsInstanceOfType(typeof(Dictionary<string, object>), c2.Data["two"]);
Dictionary<string, object> c = (Dictionary<string, object>)c2.Data["two"];
CustomAssert.IsInstanceOfType(typeof (JObject), c["one"]);
json = @"{
""Data"": {
""one"": {
""$type"": ""Newtonsoft.Json.Tests.Serialization.TestComponentSimple, Newtonsoft.Json.Tests"",
""MyProperty"": 1
},
""two"": {
""$type"": ""System.Collections.Generic.Dictionary`2[[System.String, mscorlib],[System.Object, mscorlib]], mscorlib"",
""one"": {
""$type"": ""Newtonsoft.Json.Tests.Serialization.TestComponentSimple, Newtonsoft.Json.Tests"",
""MyProperty"": 1
}
}
}
}";
c2 = JsonConvert.DeserializeObject<PropertyItemTypeNameHandlingDictionary>(json);
Assert.AreEqual(2, c2.Data.Count);
CustomAssert.IsInstanceOfType(typeof(TestComponentSimple), c2.Data["one"]);
CustomAssert.IsInstanceOfType(typeof(Dictionary<string, object>), c2.Data["two"]);
c = (Dictionary<string, object>)c2.Data["two"];
CustomAssert.IsInstanceOfType(typeof(JObject), c["one"]);
JObject o = (JObject) c["one"];
Assert.AreEqual(1, (int) o["MyProperty"]);
}
[Test]
public void PropertyItemTypeNameHandlingObject()
{
PropertyItemTypeNameHandlingObject o1 = new PropertyItemTypeNameHandlingObject
{
Data = new TypeNameHandlingTestObject
{
Prop1 = new List<object>
{
new TestComponentSimple
{
MyProperty = 1
}
},
Prop2 = new TestComponentSimple
{
MyProperty = 1
},
Prop3 = 3,
Prop4 = new JObject()
}
};
string json = JsonConvert.SerializeObject(o1, Formatting.Indented);
Assert.AreEqual(@"{
""Data"": {
""Prop1"": {
""$type"": ""System.Collections.Generic.List`1[[System.Object, mscorlib]], mscorlib"",
""$values"": [
{
""MyProperty"": 1
}
]
},
""Prop2"": {
""$type"": ""Newtonsoft.Json.Tests.Serialization.TestComponentSimple, Newtonsoft.Json.Tests"",
""MyProperty"": 1
},
""Prop3"": 3,
""Prop4"": {}
}
}", json);
PropertyItemTypeNameHandlingObject o2 = JsonConvert.DeserializeObject<PropertyItemTypeNameHandlingObject>(json);
Assert.IsNotNull(o2);
Assert.IsNotNull(o2.Data);
CustomAssert.IsInstanceOfType(typeof(List<object>), o2.Data.Prop1);
CustomAssert.IsInstanceOfType(typeof(TestComponentSimple), o2.Data.Prop2);
CustomAssert.IsInstanceOfType(typeof(long), o2.Data.Prop3);
CustomAssert.IsInstanceOfType(typeof(JObject), o2.Data.Prop4);
List<object> o = (List<object>)o2.Data.Prop1;
JObject j = (JObject)o[0];
Assert.AreEqual(1, (int)j["MyProperty"]);
}
#if !(NET35 || NET20 || WINDOWS_PHONE || PORTABLE)
[Test]
public void PropertyItemTypeNameHandlingDynamic()
{
PropertyItemTypeNameHandlingDynamic d1 = new PropertyItemTypeNameHandlingDynamic();
dynamic data = new DynamicDictionary();
data.one = new TestComponentSimple
{
MyProperty = 1
};
dynamic data2 = new DynamicDictionary();
data2.one = new TestComponentSimple
{
MyProperty = 2
};
data.two = data2;
d1.Data = (DynamicDictionary)data;
string json = JsonConvert.SerializeObject(d1, Formatting.Indented);
Assert.AreEqual(@"{
""Data"": {
""one"": {
""$type"": ""Newtonsoft.Json.Tests.Serialization.TestComponentSimple, Newtonsoft.Json.Tests"",
""MyProperty"": 1
},
""two"": {
""$type"": ""Newtonsoft.Json.Tests.Linq.DynamicDictionary, Newtonsoft.Json.Tests"",
""one"": {
""MyProperty"": 2
}
}
}
}", json);
PropertyItemTypeNameHandlingDynamic d2 = JsonConvert.DeserializeObject<PropertyItemTypeNameHandlingDynamic>(json);
Assert.IsNotNull(d2);
Assert.IsNotNull(d2.Data);
dynamic data3 = d2.Data;
TestComponentSimple c = (TestComponentSimple)data3.one;
Assert.AreEqual(1, c.MyProperty);
dynamic data4 = data3.two;
JObject o = (JObject)data4.one;
Assert.AreEqual(2, (int)o["MyProperty"]);
json = @"{
""Data"": {
""one"": {
""$type"": ""Newtonsoft.Json.Tests.Serialization.TestComponentSimple, Newtonsoft.Json.Tests"",
""MyProperty"": 1
},
""two"": {
""$type"": ""Newtonsoft.Json.Tests.Linq.DynamicDictionary, Newtonsoft.Json.Tests"",
""one"": {
""$type"": ""Newtonsoft.Json.Tests.Serialization.TestComponentSimple, Newtonsoft.Json.Tests"",
""MyProperty"": 2
}
}
}
}";
d2 = JsonConvert.DeserializeObject<PropertyItemTypeNameHandlingDynamic>(json);
data3 = d2.Data;
data4 = data3.two;
o = (JObject)data4.one;
Assert.AreEqual(2, (int)o["MyProperty"]);
}
#endif
}
public class Message
@@ -1225,6 +1545,40 @@ namespace Newtonsoft.Json.Tests.Serialization
}
#endif
public class PropertyItemTypeNameHandlingObject
{
[JsonProperty(ItemTypeNameHandling = TypeNameHandling.All)]
public TypeNameHandlingTestObject Data { get; set; }
}
#if !(NET35 || NET20 || WINDOWS_PHONE || PORTABLE)
public class PropertyItemTypeNameHandlingDynamic
{
[JsonProperty(ItemTypeNameHandling = TypeNameHandling.All)]
public DynamicDictionary Data { get; set; }
}
#endif
public class TypeNameHandlingTestObject
{
public object Prop1 { get; set; }
public object Prop2 { get; set; }
public object Prop3 { get; set; }
public object Prop4 { get; set; }
}
public class PropertyItemTypeNameHandlingDictionary
{
[JsonProperty(ItemTypeNameHandling = TypeNameHandling.All)]
public IDictionary<string, object> Data { get; set; }
}
public class PropertyItemTypeNameHandling
{
[JsonProperty(ItemTypeNameHandling = TypeNameHandling.All)]
public IList<object> Data { get; set; }
}
[JsonArray(ItemTypeNameHandling = TypeNameHandling.All)]
public class TypeNameList<T> : List<T>
{
+26 -5
View File
@@ -43,12 +43,15 @@ namespace Newtonsoft.Json
internal bool? _isReference;
internal int? _order;
internal Required? _required;
internal bool? _itemIsReference;
internal ReferenceLoopHandling? _itemReferenceLoopHandling;
internal TypeNameHandling? _itemTypeNameHandling;
///// <summary>
///// Gets the collection's items converter.
///// </summary>
///// <value>The collection's items converter.</value>
//public Type ItemConverterType { get; set; }
/// <summary>
/// Gets the collection's items converter.
/// </summary>
/// <value>The collection's items converter.</value>
public Type ItemConverterType { get; set; }
/// <summary>
/// Gets or sets the null value handling used when serializing this property.
@@ -138,6 +141,24 @@ namespace Newtonsoft.Json
/// <value>The name of the property.</value>
public string PropertyName { get; set; }
public ReferenceLoopHandling ItemReferenceLoopHandling
{
get { return _itemReferenceLoopHandling ?? default(ReferenceLoopHandling); }
set { _itemReferenceLoopHandling = value; }
}
public TypeNameHandling ItemTypeNameHandling
{
get { return _itemTypeNameHandling ?? default(TypeNameHandling); }
set { _itemTypeNameHandling = value; }
}
public bool ItemIsReference
{
get { return _itemIsReference ?? default(bool); }
set { _itemIsReference = value; }
}
/// <summary>
/// Initializes a new instance of the <see cref="JsonPropertyAttribute"/> class.
/// </summary>
@@ -90,7 +90,7 @@ 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
[assembly: CLSCompliant(true)]
@@ -997,6 +997,14 @@ namespace Newtonsoft.Json.Serialization
property.TypeNameHandling = (propertyAttribute != null) ? propertyAttribute._typeNameHandling : null;
property.IsReference = (propertyAttribute != null) ? propertyAttribute._isReference : null;
property.ItemIsReference = (propertyAttribute != null) ? propertyAttribute._itemIsReference : null;
property.ItemConverter =
(propertyAttribute != null && propertyAttribute.ItemConverterType != null)
? JsonConverterAttribute.CreateJsonConverterInstance(propertyAttribute.ItemConverterType)
: null;
property.ItemReferenceLoopHandling = (propertyAttribute != null) ? propertyAttribute._itemReferenceLoopHandling : null;
property.ItemTypeNameHandling = (propertyAttribute != null) ? propertyAttribute._itemTypeNameHandling : null;
allowNonPublicAccess = false;
if ((DefaultMembersSearchFlags & BindingFlags.NonPublic) == BindingFlags.NonPublic)
allowNonPublicAccess = true;
@@ -35,6 +35,8 @@ namespace Newtonsoft.Json.Serialization
/// </summary>
public class JsonProperty
{
internal Required? _required;
// use to cache contract during deserialization
internal JsonContract PropertyContract { get; set; }
@@ -111,8 +113,6 @@ namespace Newtonsoft.Json.Serialization
/// <value>The default value.</value>
public object DefaultValue { get; set; }
internal Required? _required;
/// <summary>
/// Gets a value indicating whether this <see cref="JsonProperty"/> is required.
/// </summary>
@@ -189,5 +189,10 @@ namespace Newtonsoft.Json.Serialization
{
return PropertyName;
}
public JsonConverter ItemConverter { get; set; }
public bool? ItemIsReference { get; set; }
public TypeNameHandling? ItemTypeNameHandling { get; set; }
public ReferenceLoopHandling? ItemReferenceLoopHandling { get; set; }
}
}
@@ -69,7 +69,7 @@ namespace Newtonsoft.Json.Serialization
if (reader.TokenType == JsonToken.StartArray)
{
if (contract.ContractType == JsonContractType.Array)
PopulateList(CollectionUtils.CreateCollectionWrapper(target), reader, null, (JsonArrayContract) contract);
PopulateList(CollectionUtils.CreateCollectionWrapper(target), reader, null, (JsonArrayContract) contract, null);
else
throw JsonSerializationException.Create(reader, "Cannot populate JSON array onto type '{0}'.".FormatWith(CultureInfo.InvariantCulture, objectType));
}
@@ -86,9 +86,9 @@ namespace Newtonsoft.Json.Serialization
}
if (contract.ContractType == JsonContractType.Dictionary)
PopulateDictionary(CollectionUtils.CreateDictionaryWrapper(target), reader, (JsonDictionaryContract) contract, id);
PopulateDictionary(CollectionUtils.CreateDictionaryWrapper(target), reader, (JsonDictionaryContract) contract, null, null, id);
else if (contract.ContractType == JsonContractType.Object)
PopulateObject(target, reader, (JsonObjectContract) contract, id);
PopulateObject(target, reader, (JsonObjectContract) contract, null, null, id);
else
throw JsonSerializationException.Create(reader, "Cannot populate JSON object onto type '{0}'.".FormatWith(CultureInfo.InvariantCulture, objectType));
}
@@ -115,7 +115,7 @@ namespace Newtonsoft.Json.Serialization
try
{
JsonConverter converter = GetConverter(contract, null, null);
JsonConverter converter = GetConverter(contract, null, null, null);
if (reader.TokenType == JsonToken.None && !ReadForType(reader, contract, converter != null, false))
{
@@ -125,7 +125,7 @@ namespace Newtonsoft.Json.Serialization
return null;
}
return CreateValueNonProperty(reader, objectType, contract, converter, null);
return CreateValueNonProperty(reader, objectType, contract, converter, null, null);
}
catch (Exception ex)
{
@@ -201,7 +201,7 @@ namespace Newtonsoft.Json.Serialization
return token;
}
private object CreateValueProperty(JsonReader reader, JsonProperty property, JsonConverter propertyConverter, JsonContainerContract containerContract, object target, bool gottenCurrentValue, object currentValue)
private object CreateValueProperty(JsonReader reader, JsonProperty property, JsonConverter propertyConverter, JsonContainerContract containerContract, JsonProperty containerProperty, object target, bool gottenCurrentValue, object currentValue)
{
JsonContract contract;
JsonConverter converter;
@@ -219,7 +219,7 @@ namespace Newtonsoft.Json.Serialization
contract = GetContractSafe(currentValue.GetType());
if (contract != property.PropertyContract)
converter = GetConverter(contract, property.MemberConverter, containerContract);
converter = GetConverter(contract, property.MemberConverter, containerContract, containerProperty);
else
converter = propertyConverter;
}
@@ -234,18 +234,18 @@ namespace Newtonsoft.Json.Serialization
return converter.ReadJson(reader, objectType, currentValue, GetInternalSerializer());
}
return CreateValueInternal(reader, objectType, contract, property, containerContract, currentValue);
return CreateValueInternal(reader, objectType, contract, property, containerContract, containerProperty, currentValue);
}
private object CreateValueNonProperty(JsonReader reader, Type objectType, JsonContract contract, JsonConverter converter, JsonContainerContract containerContract)
private object CreateValueNonProperty(JsonReader reader, Type objectType, JsonContract contract, JsonConverter converter, JsonContainerContract containerContract, JsonProperty containerProperty)
{
if (converter != null && converter.CanRead)
return converter.ReadJson(reader, objectType, null, GetInternalSerializer());
return CreateValueInternal(reader, objectType, contract, null, containerContract, null);
return CreateValueInternal(reader, objectType, contract, null, containerContract, containerProperty, null);
}
private object CreateValueInternal(JsonReader reader, Type objectType, JsonContract contract, JsonProperty member, JsonContainerContract containerContract, object existingValue)
private object CreateValueInternal(JsonReader reader, Type objectType, JsonContract contract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerMember, object existingValue)
{
if (contract != null && contract.ContractType == JsonContractType.Linq)
return CreateJToken(reader, contract);
@@ -257,9 +257,9 @@ namespace Newtonsoft.Json.Serialization
// populate a typed object or generic dictionary/array
// depending upon whether an objectType was supplied
case JsonToken.StartObject:
return CreateObject(reader, objectType, contract, member, containerContract, existingValue);
return CreateObject(reader, objectType, contract, member, containerContract, containerMember, existingValue);
case JsonToken.StartArray:
return CreateList(reader, objectType, contract, member, existingValue, null);
return CreateList(reader, objectType, contract, member, containerMember, existingValue, null);
case JsonToken.Integer:
case JsonToken.Float:
case JsonToken.Boolean:
@@ -268,7 +268,7 @@ namespace Newtonsoft.Json.Serialization
return EnsureType(reader, reader.Value, CultureInfo.InvariantCulture, contract, objectType);
case JsonToken.String:
// convert empty string to null automatically for nullable types
if (string.IsNullOrEmpty((string)reader.Value) && objectType != typeof(string) && objectType != typeof(object) && contract.IsNullable)
if (string.IsNullOrEmpty((string)reader.Value) && objectType != typeof(string) && objectType != typeof(object) && contract != null && contract.IsNullable)
return null;
// string that needs to be returned as a byte array should be base 64 decoded
@@ -301,7 +301,7 @@ namespace Newtonsoft.Json.Serialization
throw JsonSerializationException.Create(reader, "Unexpected end when deserializing object.");
}
private JsonConverter GetConverter(JsonContract contract, JsonConverter memberConverter, JsonContainerContract containerContract)
private JsonConverter GetConverter(JsonContract contract, JsonConverter memberConverter, JsonContainerContract containerContract, JsonProperty containerProperty)
{
JsonConverter converter = null;
if (memberConverter != null)
@@ -309,6 +309,10 @@ namespace Newtonsoft.Json.Serialization
// member attribute converter
converter = memberConverter;
}
else if (containerProperty != null && containerProperty.ItemConverter != null)
{
converter = containerProperty.ItemConverter;
}
else if (containerContract != null && containerContract.ItemConverter != null)
{
converter = containerContract.ItemConverter;
@@ -329,7 +333,7 @@ namespace Newtonsoft.Json.Serialization
return converter;
}
private object CreateObject(JsonReader reader, Type objectType, JsonContract contract, JsonProperty member, JsonContainerContract containerContract, object existingValue)
private object CreateObject(JsonReader reader, Type objectType, JsonContract contract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerMember, object existingValue)
{
CheckedRead(reader);
@@ -378,6 +382,7 @@ namespace Newtonsoft.Json.Serialization
TypeNameHandling resolvedTypeNameHandling = ((member != null) ? member.TypeNameHandling : null)
?? ((containerContract != null) ? containerContract.ItemTypeNameHandling : null)
?? ((containerMember != null) ? containerMember.ItemTypeNameHandling : null)
?? Serializer.TypeNameHandling;
if (resolvedTypeNameHandling != TypeNameHandling.None)
@@ -399,7 +404,11 @@ namespace Newtonsoft.Json.Serialization
if (specifiedType == null)
throw JsonSerializationException.Create(reader, "Type specified in JSON '{0}' was not resolved.".FormatWith(CultureInfo.InvariantCulture, qualifiedTypeName));
if (objectType != null && !objectType.IsAssignableFrom(specifiedType))
if (objectType != null
#if !(NET35 || NET20 || WINDOWS_PHONE || PORTABLE)
&& objectType != typeof(IDynamicMetaObjectProvider)
#endif
&& !objectType.IsAssignableFrom(specifiedType))
throw JsonSerializationException.Create(reader, "Type specified in JSON '{0}' is not compatible with '{1}'.".FormatWith(CultureInfo.InvariantCulture, specifiedType.AssemblyQualifiedName, objectType.AssemblyQualifiedName));
objectType = specifiedType;
@@ -422,7 +431,7 @@ namespace Newtonsoft.Json.Serialization
else if (string.Equals(propertyName, JsonTypeReflector.ArrayValuesPropertyName, StringComparison.Ordinal))
{
CheckedRead(reader);
object list = CreateList(reader, objectType, contract, member, existingValue, id);
object list = CreateList(reader, objectType, contract, member, containerMember, existingValue, id);
CheckedRead(reader);
return list;
}
@@ -446,16 +455,16 @@ namespace Newtonsoft.Json.Serialization
case JsonContractType.Object:
JsonObjectContract objectContract = (JsonObjectContract) contract;
if (existingValue == null)
return CreateAndPopulateObject(reader, objectContract, id);
return CreateAndPopulateObject(reader, objectContract, member, containerMember, id);
return PopulateObject(existingValue, reader, objectContract, id);
return PopulateObject(existingValue, reader, objectContract, member, containerMember, id);
case JsonContractType.Primitive:
JsonPrimitiveContract primitiveContract = (JsonPrimitiveContract) contract;
// if the content is inside $value then read past it
if (reader.TokenType == JsonToken.PropertyName && string.Equals(reader.Value.ToString(), JsonTypeReflector.ValuePropertyName, StringComparison.Ordinal))
{
CheckedRead(reader);
object value = CreateValueInternal(reader, objectType, primitiveContract, member, null, existingValue);
object value = CreateValueInternal(reader, objectType, primitiveContract, member, null, null, existingValue);
CheckedRead(reader);
return value;
@@ -464,13 +473,13 @@ namespace Newtonsoft.Json.Serialization
case JsonContractType.Dictionary:
JsonDictionaryContract dictionaryContract = (JsonDictionaryContract) contract;
if (existingValue == null)
return CreateAndPopulateDictionary(reader, dictionaryContract, id);
return CreateAndPopulateDictionary(reader, dictionaryContract, member, containerMember, id);
return PopulateDictionary(dictionaryContract.CreateWrapper(existingValue), reader, dictionaryContract, id);
return PopulateDictionary(dictionaryContract.CreateWrapper(existingValue), reader, dictionaryContract, member, containerMember, id);
#if !(NET35 || NET20 || WINDOWS_PHONE || PORTABLE)
case JsonContractType.Dynamic:
JsonDynamicContract dynamicContract = (JsonDynamicContract) contract;
return CreateDynamic(reader, dynamicContract, id);
return CreateDynamic(reader, dynamicContract, member, id);
#endif
#if !(SILVERLIGHT || NETFX_CORE || PORTABLE)
case JsonContractType.Serializable:
@@ -504,7 +513,7 @@ To force JSON arrays to deserialize add the JsonArrayAttribute to the type.".For
throw JsonSerializationException.Create(reader, "Unexpected end when deserializing object.");
}
private object CreateList(JsonReader reader, Type objectType, JsonContract contract, JsonProperty member, object existingValue, string reference)
private object CreateList(JsonReader reader, Type objectType, JsonContract contract, JsonProperty member, JsonProperty containerProperty, object existingValue, string reference)
{
object value;
if (HasDefinedType(objectType))
@@ -512,9 +521,9 @@ To force JSON arrays to deserialize add the JsonArrayAttribute to the type.".For
JsonArrayContract arrayContract = EnsureArrayContract(reader, objectType, contract);
if (existingValue == null)
value = CreateAndPopulateList(reader, reference, arrayContract);
value = CreateAndPopulateList(reader, reference, arrayContract, member, containerProperty);
else
value = PopulateList(arrayContract.CreateWrapper(existingValue), reader, reference, arrayContract);
value = PopulateList(arrayContract.CreateWrapper(existingValue), reader, reference, arrayContract, member);
}
else
{
@@ -583,7 +592,7 @@ To force JSON arrays to deserialize add the JsonArrayAttribute to the type.".For
return value.ToString();
}
private void SetPropertyValue(JsonProperty property, JsonConverter propertyConverter, JsonContainerContract containerContract, JsonReader reader, object target)
private void SetPropertyValue(JsonProperty property, JsonConverter propertyConverter, JsonContainerContract containerContract, JsonProperty containerProperty, JsonReader reader, object target)
{
if (property.Ignored)
{
@@ -634,7 +643,7 @@ To force JSON arrays to deserialize add the JsonArrayAttribute to the type.".For
}
object existingValue = (useExistingValue) ? currentValue : null;
object value = CreateValueProperty(reader, property, propertyConverter, containerContract, target, gottenCurrentValue, existingValue);
object value = CreateValueProperty(reader, property, propertyConverter, containerContract, containerProperty, target, gottenCurrentValue, existingValue);
// always set the value if useExistingValue is false,
// otherwise also set it if CreateValue returns a new value compared to the currentValue
@@ -669,7 +678,7 @@ To force JSON arrays to deserialize add the JsonArrayAttribute to the type.".For
return true;
}
private object CreateAndPopulateDictionary(JsonReader reader, JsonDictionaryContract contract, string id)
private object CreateAndPopulateDictionary(JsonReader reader, JsonDictionaryContract contract, JsonProperty member, JsonProperty containerProperty, string id)
{
object dictionary;
@@ -681,12 +690,12 @@ To force JSON arrays to deserialize add the JsonArrayAttribute to the type.".For
IWrappedDictionary dictionaryWrapper = contract.CreateWrapper(dictionary);
PopulateDictionary(dictionaryWrapper, reader, contract, id);
PopulateDictionary(dictionaryWrapper, reader, contract, member, containerProperty, id);
return dictionaryWrapper.UnderlyingDictionary;
}
private object PopulateDictionary(IWrappedDictionary dictionary, JsonReader reader, JsonDictionaryContract contract, string id)
private object PopulateDictionary(IWrappedDictionary dictionary, JsonReader reader, JsonDictionaryContract contract, JsonProperty member, JsonProperty containerProperty, string id)
{
if (id != null)
Serializer.ReferenceResolver.AddReference(this, id, dictionary.UnderlyingDictionary);
@@ -718,12 +727,12 @@ To force JSON arrays to deserialize add the JsonArrayAttribute to the type.".For
if (contract.ItemContract == null)
contract.ItemContract = GetContractSafe(contract.DictionaryValueType);
JsonConverter dictionaryValueConverter = contract.ItemConverter ?? GetConverter(contract.ItemContract, null, contract);
JsonConverter dictionaryValueConverter = contract.ItemConverter ?? GetConverter(contract.ItemContract, null, contract, containerProperty);
if (!ReadForType(reader, contract.ItemContract, dictionaryValueConverter != null, false))
throw JsonSerializationException.Create(reader, "Unexpected end when deserializing object.");
dictionary[keyValue] = CreateValueNonProperty(reader, contract.DictionaryValueType, contract.ItemContract, dictionaryValueConverter, contract);
dictionary[keyValue] = CreateValueNonProperty(reader, contract.DictionaryValueType, contract.ItemContract, dictionaryValueConverter, contract, member);
}
catch (Exception ex)
{
@@ -747,7 +756,7 @@ To force JSON arrays to deserialize add the JsonArrayAttribute to the type.".For
throw JsonSerializationException.Create(reader, "Unexpected end when deserializing object.");
}
private object CreateAndPopulateList(JsonReader reader, string reference, JsonArrayContract contract)
private object CreateAndPopulateList(JsonReader reader, string reference, JsonArrayContract contract, JsonProperty member, JsonProperty containerProperty)
{
return CollectionUtils.CreateAndPopulateList(contract.CreatedType, (l, isTemporaryListReference) =>
{
@@ -761,11 +770,11 @@ To force JSON arrays to deserialize add the JsonArrayAttribute to the type.".For
if (contract.OnError != null && isTemporaryListReference)
throw JsonSerializationException.Create(reader, "Cannot call OnError on an array or readonly list: {0}.".FormatWith(CultureInfo.InvariantCulture, contract.UnderlyingType));
PopulateList(contract.CreateWrapper(l), reader, reference, contract);
PopulateList(contract.CreateWrapper(l), reader, reference, contract, member);
});
}
private object PopulateList(IWrappedCollection wrappedList, JsonReader reader, string reference, JsonArrayContract contract)
private object PopulateList(IWrappedCollection wrappedList, JsonReader reader, string reference, JsonArrayContract contract, JsonProperty containerProperty)
{
object list = wrappedList.UnderlyingCollection;
@@ -785,7 +794,7 @@ To force JSON arrays to deserialize add the JsonArrayAttribute to the type.".For
int index = 0;
JsonContract collectionItemContract = GetContractSafe(contract.CollectionItemType);
JsonConverter collectionItemConverter = GetConverter(collectionItemContract, null, contract);
JsonConverter collectionItemConverter = GetConverter(collectionItemContract, null, contract, containerProperty);
while (true)
{
@@ -802,7 +811,7 @@ To force JSON arrays to deserialize add the JsonArrayAttribute to the type.".For
case JsonToken.Comment:
break;
default:
object value = CreateValueNonProperty(reader, contract.CollectionItemType, collectionItemContract, collectionItemConverter, contract);
object value = CreateValueNonProperty(reader, contract.CollectionItemType, collectionItemContract, collectionItemConverter, contract, containerProperty);
wrappedList.Add(value);
break;
@@ -881,7 +890,7 @@ To fix this error either change the environment to be fully trusted, change the
#endif
#if !(NET35 || NET20 || WINDOWS_PHONE || PORTABLE)
private object CreateDynamic(JsonReader reader, JsonDynamicContract contract, string id)
private object CreateDynamic(JsonReader reader, JsonDynamicContract contract, JsonProperty member, string id)
{
IDynamicMetaObjectProvider newObject = null;
@@ -922,18 +931,18 @@ To fix this error either change the environment to be fully trusted, change the
if (property.PropertyContract == null)
property.PropertyContract = GetContractSafe(property.PropertyType);
JsonConverter propertyConverter = GetConverter(property.PropertyContract, property.MemberConverter, null);
JsonConverter propertyConverter = GetConverter(property.PropertyContract, property.MemberConverter, null, null);
SetPropertyValue(property, propertyConverter, null, reader, newObject);
SetPropertyValue(property, propertyConverter, null, member, reader, newObject);
}
else
{
Type t = (JsonReader.IsPrimitiveToken(reader.TokenType)) ? reader.ValueType : typeof (IDynamicMetaObjectProvider);
JsonContract dynamicMemberContract = GetContractSafe(t);
JsonConverter dynamicMemberConverter = GetConverter(dynamicMemberContract, null, null);
JsonConverter dynamicMemberConverter = GetConverter(dynamicMemberContract, null, null, member);
object value = CreateValueNonProperty(reader, t, dynamicMemberContract, dynamicMemberConverter, null);
object value = CreateValueNonProperty(reader, t, dynamicMemberContract, dynamicMemberConverter, null, member);
newObject.TrySetMember(memberName, value);
}
@@ -960,7 +969,7 @@ To fix this error either change the environment to be fully trusted, change the
}
#endif
private object CreateAndPopulateObject(JsonReader reader, JsonObjectContract contract, string id)
private object CreateAndPopulateObject(JsonReader reader, JsonObjectContract contract, JsonProperty member, JsonProperty containerProperty, string id)
{
object newObject = null;
@@ -970,7 +979,7 @@ To fix this error either change the environment to be fully trusted, change the
if (contract.OverrideConstructor != null)
{
if (contract.OverrideConstructor.GetParameters().Length > 0)
return CreateObjectFromNonDefaultConstructor(reader, contract, contract.OverrideConstructor, id);
return CreateObjectFromNonDefaultConstructor(reader, contract, containerProperty, contract.OverrideConstructor, id);
newObject = contract.OverrideConstructor.Invoke(null);
}
@@ -985,23 +994,23 @@ To fix this error either change the environment to be fully trusted, change the
}
else if (contract.ParametrizedConstructor != null)
{
return CreateObjectFromNonDefaultConstructor(reader, contract, contract.ParametrizedConstructor, id);
return CreateObjectFromNonDefaultConstructor(reader, contract, containerProperty, contract.ParametrizedConstructor, id);
}
if (newObject == null)
throw JsonSerializationException.Create(reader, "Unable to find a constructor to use for type {0}. A class should either have a default constructor, one constructor with arguments or a constructor marked with the JsonConstructor attribute.".FormatWith(CultureInfo.InvariantCulture, contract.UnderlyingType));
PopulateObject(newObject, reader, contract, id);
PopulateObject(newObject, reader, contract, member, containerProperty, id);
return newObject;
}
private object CreateObjectFromNonDefaultConstructor(JsonReader reader, JsonObjectContract contract, ConstructorInfo constructorInfo, string id)
private object CreateObjectFromNonDefaultConstructor(JsonReader reader, JsonObjectContract contract, JsonProperty containerProperty, ConstructorInfo constructorInfo, string id)
{
ValidationUtils.ArgumentNotNull(constructorInfo, "constructorInfo");
Type objectType = contract.UnderlyingType;
IDictionary<JsonProperty, object> propertyValues = ResolvePropertyAndConstructorValues(contract, reader, objectType);
IDictionary<JsonProperty, object> propertyValues = ResolvePropertyAndConstructorValues(contract, containerProperty, reader, objectType);
IDictionary<ParameterInfo, object> constructorParameters = constructorInfo.GetParameters().ToDictionary(p => p, p => (object) null);
IDictionary<JsonProperty, object> remainingPropertyValues = new Dictionary<JsonProperty, object>();
@@ -1039,7 +1048,7 @@ To fix this error either change the environment to be fully trusted, change the
if (propertyContract.ContractType == JsonContractType.Array)
{
JsonArrayContract propertyArrayContract = propertyContract as JsonArrayContract;
JsonArrayContract propertyArrayContract = (JsonArrayContract)propertyContract;
object createdObjectCollection = property.ValueProvider.GetValue(createdObject);
if (createdObjectCollection != null)
@@ -1055,7 +1064,7 @@ To fix this error either change the environment to be fully trusted, change the
}
else if (propertyContract.ContractType == JsonContractType.Dictionary)
{
JsonDictionaryContract jsonDictionaryContract = propertyContract as JsonDictionaryContract;
JsonDictionaryContract jsonDictionaryContract = (JsonDictionaryContract)propertyContract;
object createdObjectDictionary = property.ValueProvider.GetValue(createdObject);
if (createdObjectDictionary != null)
@@ -1076,7 +1085,7 @@ To fix this error either change the environment to be fully trusted, change the
return createdObject;
}
private IDictionary<JsonProperty, object> ResolvePropertyAndConstructorValues(JsonObjectContract contract, JsonReader reader, Type objectType)
private IDictionary<JsonProperty, object> ResolvePropertyAndConstructorValues(JsonObjectContract contract, JsonProperty containerProperty, JsonReader reader, Type objectType)
{
IDictionary<JsonProperty, object> propertyValues = new Dictionary<JsonProperty, object>();
bool exit = false;
@@ -1097,13 +1106,13 @@ To fix this error either change the environment to be fully trusted, change the
if (property.PropertyContract == null)
property.PropertyContract = GetContractSafe(property.PropertyType);
JsonConverter propertyConverter = GetConverter(property.PropertyContract, property.MemberConverter, contract);
JsonConverter propertyConverter = GetConverter(property.PropertyContract, property.MemberConverter, contract, containerProperty);
if (!ReadForType(reader, property.PropertyContract, propertyConverter != null, false))
throw JsonSerializationException.Create(reader, "Unexpected end when setting {0}'s value.".FormatWith(CultureInfo.InvariantCulture, memberName));
if (!property.Ignored)
propertyValues[property] = CreateValueProperty(reader, property, propertyConverter, contract, null, true, null);
propertyValues[property] = CreateValueProperty(reader, property, propertyConverter, contract, containerProperty, null, true, null);
else
reader.Skip();
}
@@ -1177,7 +1186,7 @@ To fix this error either change the environment to be fully trusted, change the
return (reader.TokenType != JsonToken.None);
}
private object PopulateObject(object newObject, JsonReader reader, JsonObjectContract contract, string id)
private object PopulateObject(object newObject, JsonReader reader, JsonObjectContract contract, JsonProperty member, JsonProperty containerProperty, string id)
{
contract.InvokeOnDeserializing(newObject, Serializer.Context);
@@ -1215,14 +1224,14 @@ To fix this error either change the environment to be fully trusted, change the
if (property.PropertyContract == null)
property.PropertyContract = GetContractSafe(property.PropertyType);
JsonConverter propertyConverter = GetConverter(property.PropertyContract, property.MemberConverter, contract);
JsonConverter propertyConverter = GetConverter(property.PropertyContract, property.MemberConverter, contract, member);
if (!ReadForType(reader, property.PropertyContract, propertyConverter != null, false))
throw JsonSerializationException.Create(reader, "Unexpected end when setting {0}'s value.".FormatWith(CultureInfo.InvariantCulture, memberName));
SetPropertyPresence(reader, property, propertiesPresence);
SetPropertyValue(property, propertyConverter, contract, reader, newObject);
SetPropertyValue(property, propertyConverter, contract, member, reader, newObject);
}
catch (Exception ex)
{
@@ -58,7 +58,7 @@ namespace Newtonsoft.Json.Serialization
if (jsonWriter == null)
throw new ArgumentNullException("jsonWriter");
SerializeValue(jsonWriter, value, GetContractSafe(value), null, null);
SerializeValue(jsonWriter, value, GetContractSafe(value), null, null, null);
}
private JsonSerializerProxy GetInternalSerializer()
@@ -77,11 +77,11 @@ namespace Newtonsoft.Json.Serialization
return Serializer.ContractResolver.ResolveContract(value.GetType());
}
private void SerializePrimitive(JsonWriter writer, object value, JsonPrimitiveContract contract, JsonProperty member, JsonContainerContract collectionContract)
private void SerializePrimitive(JsonWriter writer, object value, JsonPrimitiveContract contract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerProperty)
{
if (contract.UnderlyingType == typeof (byte[]))
{
bool includeTypeDetails = ShouldWriteType(TypeNameHandling.Objects, contract, member, collectionContract);
bool includeTypeDetails = ShouldWriteType(TypeNameHandling.Objects, contract, member, containerContract, containerProperty);
if (includeTypeDetails)
{
writer.WriteStartObject();
@@ -96,7 +96,7 @@ namespace Newtonsoft.Json.Serialization
writer.WriteValue(value);
}
private void SerializeValue(JsonWriter writer, object value, JsonContract valueContract, JsonProperty member, JsonContainerContract collectionContract)
private void SerializeValue(JsonWriter writer, object value, JsonContract valueContract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerProperty)
{
if (value == null)
{
@@ -106,43 +106,44 @@ namespace Newtonsoft.Json.Serialization
JsonConverter converter;
if ((((converter = (member != null) ? member.Converter : null) != null)
|| ((converter = (collectionContract != null) ? collectionContract.ItemConverter : null) != null)
|| ((converter = (containerProperty != null) ? containerProperty.ItemConverter : null) != null)
|| ((converter = (containerContract != null) ? containerContract.ItemConverter : null) != null)
|| ((converter = valueContract.Converter) != null)
|| ((converter = Serializer.GetMatchingConverter(valueContract.UnderlyingType)) != null)
|| ((converter = valueContract.InternalConverter) != null))
&& converter.CanWrite)
{
SerializeConvertable(writer, converter, value, valueContract, collectionContract);
SerializeConvertable(writer, converter, value, valueContract, containerContract, containerProperty);
return;
}
switch (valueContract.ContractType)
{
case JsonContractType.Object:
SerializeObject(writer, value, (JsonObjectContract) valueContract, member, collectionContract);
SerializeObject(writer, value, (JsonObjectContract)valueContract, member, containerContract, containerProperty);
break;
case JsonContractType.Array:
JsonArrayContract arrayContract = (JsonArrayContract) valueContract;
SerializeList(writer, arrayContract.CreateWrapper(value), arrayContract, member, collectionContract);
SerializeList(writer, arrayContract.CreateWrapper(value), arrayContract, member, containerContract, containerProperty);
break;
case JsonContractType.Primitive:
SerializePrimitive(writer, value, (JsonPrimitiveContract)valueContract, member, collectionContract);
SerializePrimitive(writer, value, (JsonPrimitiveContract)valueContract, member, containerContract, containerProperty);
break;
case JsonContractType.String:
SerializeString(writer, value, (JsonStringContract) valueContract);
SerializeString(writer, value, (JsonStringContract)valueContract);
break;
case JsonContractType.Dictionary:
JsonDictionaryContract dictionaryContract = (JsonDictionaryContract) valueContract;
SerializeDictionary(writer, dictionaryContract.CreateWrapper(value), dictionaryContract, member, collectionContract);
SerializeDictionary(writer, dictionaryContract.CreateWrapper(value), dictionaryContract, member, containerContract, containerProperty);
break;
#if !(NET35 || NET20 || WINDOWS_PHONE || PORTABLE)
case JsonContractType.Dynamic:
SerializeDynamic(writer, (IDynamicMetaObjectProvider)value, (JsonDynamicContract)valueContract, member, collectionContract);
SerializeDynamic(writer, (IDynamicMetaObjectProvider)value, (JsonDynamicContract)valueContract, member, containerContract, containerProperty);
break;
#endif
#if !(SILVERLIGHT || NETFX_CORE || PORTABLE)
case JsonContractType.Serializable:
SerializeISerializable(writer, (ISerializable)value, (JsonISerializableContract)valueContract, member, collectionContract);
SerializeISerializable(writer, (ISerializable)value, (JsonISerializableContract)valueContract, member, containerContract, containerProperty);
break;
#endif
case JsonContractType.Linq:
@@ -151,7 +152,7 @@ namespace Newtonsoft.Json.Serialization
}
}
private bool? ResolveIsReference(JsonContract contract, JsonProperty property, JsonContainerContract collectionContract)
private bool? ResolveIsReference(JsonContract contract, JsonProperty property, JsonContainerContract collectionContract, JsonProperty containerProperty)
{
bool? isReference = null;
@@ -159,6 +160,9 @@ namespace Newtonsoft.Json.Serialization
if (property != null)
isReference = property.IsReference;
if (isReference == null && containerProperty != null)
isReference = containerProperty.ItemIsReference;
if (isReference == null && collectionContract != null)
isReference = collectionContract.ItemIsReference;
@@ -168,14 +172,14 @@ namespace Newtonsoft.Json.Serialization
return isReference;
}
private bool ShouldWriteReference(object value, JsonProperty property, JsonContract valueContract, JsonContainerContract collectionContract)
private bool ShouldWriteReference(object value, JsonProperty property, JsonContract valueContract, JsonContainerContract collectionContract, JsonProperty containerProperty)
{
if (value == null)
return false;
if (valueContract.ContractType == JsonContractType.Primitive || valueContract.ContractType == JsonContractType.String)
return false;
bool? isReference = ResolveIsReference(valueContract, property, collectionContract);
bool? isReference = ResolveIsReference(valueContract, property, collectionContract, containerProperty);
if (isReference == null)
{
@@ -191,31 +195,6 @@ namespace Newtonsoft.Json.Serialization
return Serializer.ReferenceResolver.IsReferenced(this, value);
}
private void WriteMemberInfoProperty(JsonWriter writer, object memberValue, JsonProperty property, JsonContract contract, JsonObjectContract collectionContract)
{
string propertyName = property.PropertyName;
if (ShouldWriteReference(memberValue, property, contract, collectionContract))
{
writer.WritePropertyName(propertyName);
WriteReference(writer, memberValue);
return;
}
if (!CheckForCircularReference(memberValue, property, contract, collectionContract))
return;
if (memberValue == null)
{
Required resolvedRequired = property._required ?? collectionContract.ItemRequired ?? Required.Default;
if (resolvedRequired == Required.Always)
throw new JsonSerializationException("Cannot write a null value for property '{0}'. Property requires a value.".FormatWith(CultureInfo.InvariantCulture, property.PropertyName));
}
writer.WritePropertyName(propertyName);
SerializeValue(writer, memberValue, contract, property, collectionContract);
}
private bool ShouldWriteProperty(object memberValue, JsonProperty property)
{
if (property.NullValueHandling.GetValueOrDefault(Serializer.NullValueHandling) == NullValueHandling.Ignore &&
@@ -229,7 +208,7 @@ namespace Newtonsoft.Json.Serialization
return true;
}
private bool CheckForCircularReference(object value, JsonProperty property, JsonContract contract, JsonContainerContract collectionContract)
private bool CheckForCircularReference(object value, JsonProperty property, JsonContract contract, JsonContainerContract containerContract, JsonProperty containerProperty)
{
if (value == null || contract.ContractType == JsonContractType.Primitive || contract.ContractType == JsonContractType.String)
return true;
@@ -239,8 +218,11 @@ namespace Newtonsoft.Json.Serialization
if (property != null)
referenceLoopHandling = property.ReferenceLoopHandling;
if (referenceLoopHandling == null && collectionContract != null)
referenceLoopHandling = collectionContract.ItemReferenceLoopHandling;
if (referenceLoopHandling == null && containerProperty != null)
referenceLoopHandling = containerProperty.ItemReferenceLoopHandling;
if (referenceLoopHandling == null && containerContract != null)
referenceLoopHandling = containerContract.ItemReferenceLoopHandling;
if (_serializeStack.IndexOf(value) != -1)
{
@@ -321,13 +303,13 @@ namespace Newtonsoft.Json.Serialization
contract.InvokeOnSerialized(value, Serializer.Context);
}
private void SerializeObject(JsonWriter writer, object value, JsonObjectContract contract, JsonProperty member, JsonContainerContract collectionContract)
private void SerializeObject(JsonWriter writer, object value, JsonObjectContract contract, JsonProperty member, JsonContainerContract collectionContract, JsonProperty containerProperty)
{
contract.InvokeOnSerializing(value, Serializer.Context);
_serializeStack.Add(value);
WriteObjectStart(writer, value, contract, member, collectionContract);
WriteObjectStart(writer, value, contract, member, collectionContract, containerProperty);
int initialDepth = writer.Top;
@@ -345,7 +327,27 @@ namespace Newtonsoft.Json.Serialization
if (ShouldWriteProperty(memberValue, property))
{
WriteMemberInfoProperty(writer, memberValue, property, memberContract, contract);
string propertyName = property.PropertyName;
if (ShouldWriteReference(memberValue, property, memberContract, contract, member))
{
writer.WritePropertyName(propertyName);
WriteReference(writer, memberValue);
continue;
}
if (!CheckForCircularReference(memberValue, property, memberContract, contract, member))
continue;
if (memberValue == null)
{
Required resolvedRequired = property._required ?? contract.ItemRequired ?? Required.Default;
if (resolvedRequired == Required.Always)
throw new JsonSerializationException("Cannot write a null value for property '{0}'. Property requires a value.".FormatWith(CultureInfo.InvariantCulture, property.PropertyName));
}
writer.WritePropertyName(propertyName);
SerializeValue(writer, memberValue, memberContract, property, contract, member);
}
}
}
@@ -365,17 +367,17 @@ namespace Newtonsoft.Json.Serialization
contract.InvokeOnSerialized(value, Serializer.Context);
}
private void WriteObjectStart(JsonWriter writer, object value, JsonContract contract, JsonProperty member, JsonContainerContract collectionContract)
private void WriteObjectStart(JsonWriter writer, object value, JsonContract contract, JsonProperty member, JsonContainerContract collectionContract, JsonProperty containerProperty)
{
writer.WriteStartObject();
bool isReference = ResolveIsReference(contract, member, collectionContract) ?? HasFlag(Serializer.PreserveReferencesHandling, PreserveReferencesHandling.Objects);
bool isReference = ResolveIsReference(contract, member, collectionContract, containerProperty) ?? HasFlag(Serializer.PreserveReferencesHandling, PreserveReferencesHandling.Objects);
if (isReference)
{
writer.WritePropertyName(JsonTypeReflector.IdPropertyName);
writer.WriteValue(Serializer.ReferenceResolver.GetReference(this, value));
}
if (ShouldWriteType(TypeNameHandling.Objects, contract, member, collectionContract))
if (ShouldWriteType(TypeNameHandling.Objects, contract, member, collectionContract, containerProperty))
{
WriteTypeProperty(writer, contract.UnderlyingType);
}
@@ -402,15 +404,15 @@ namespace Newtonsoft.Json.Serialization
return ((value & flag) == flag);
}
private void SerializeConvertable(JsonWriter writer, JsonConverter converter, object value, JsonContract contract, JsonContainerContract collectionContract)
private void SerializeConvertable(JsonWriter writer, JsonConverter converter, object value, JsonContract contract, JsonContainerContract collectionContract, JsonProperty containerProperty)
{
if (ShouldWriteReference(value, null, contract, collectionContract))
if (ShouldWriteReference(value, null, contract, collectionContract, containerProperty))
{
WriteReference(writer, value);
}
else
{
if (!CheckForCircularReference(value, null, contract, collectionContract))
if (!CheckForCircularReference(value, null, contract, collectionContract, containerProperty))
return;
_serializeStack.Add(value);
@@ -421,13 +423,13 @@ namespace Newtonsoft.Json.Serialization
}
}
private void SerializeList(JsonWriter writer, IWrappedCollection values, JsonArrayContract contract, JsonProperty member, JsonContainerContract collectionContract)
private void SerializeList(JsonWriter writer, IWrappedCollection values, JsonArrayContract contract, JsonProperty member, JsonContainerContract collectionContract, JsonProperty containerProperty)
{
contract.InvokeOnSerializing(values.UnderlyingCollection, Serializer.Context);
_serializeStack.Add(values.UnderlyingCollection);
bool hasWrittenMetadataObject = WriteStartArray(writer, values, contract, member, collectionContract);
bool hasWrittenMetadataObject = WriteStartArray(writer, values, contract, member, collectionContract, containerProperty);
writer.WriteStartArray();
@@ -441,15 +443,15 @@ namespace Newtonsoft.Json.Serialization
{
JsonContract valueContract = contract.FinalItemContract ?? GetContractSafe(value);
if (ShouldWriteReference(value, null, valueContract, contract))
if (ShouldWriteReference(value, null, valueContract, contract, member))
{
WriteReference(writer, value);
}
else
{
if (CheckForCircularReference(value, null, valueContract, contract))
if (CheckForCircularReference(value, null, valueContract, contract, member))
{
SerializeValue(writer, value, valueContract, null, contract);
SerializeValue(writer, value, valueContract, null, contract, member);
}
}
}
@@ -476,10 +478,10 @@ namespace Newtonsoft.Json.Serialization
contract.InvokeOnSerialized(values.UnderlyingCollection, Serializer.Context);
}
private bool WriteStartArray(JsonWriter writer, IWrappedCollection values, JsonArrayContract contract, JsonProperty member, JsonContainerContract collectionContract)
private bool WriteStartArray(JsonWriter writer, IWrappedCollection values, JsonArrayContract contract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerProperty)
{
bool isReference = ResolveIsReference(contract, member, collectionContract) ?? HasFlag(Serializer.PreserveReferencesHandling, PreserveReferencesHandling.Arrays);
bool includeTypeDetails = ShouldWriteType(TypeNameHandling.Arrays, contract, member, collectionContract);
bool isReference = ResolveIsReference(contract, member, containerContract, containerProperty) ?? HasFlag(Serializer.PreserveReferencesHandling, PreserveReferencesHandling.Arrays);
bool includeTypeDetails = ShouldWriteType(TypeNameHandling.Arrays, contract, member, containerContract, containerProperty);
bool writeMetadataObject = isReference || includeTypeDetails;
if (writeMetadataObject)
@@ -509,7 +511,7 @@ namespace Newtonsoft.Json.Serialization
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Portability", "CA1903:UseOnlyApiFromTargetedFramework", MessageId = "System.Security.SecuritySafeCriticalAttribute")]
[SecuritySafeCritical]
#endif
private void SerializeISerializable(JsonWriter writer, ISerializable value, JsonISerializableContract contract, JsonProperty member, JsonContainerContract collectionContract)
private void SerializeISerializable(JsonWriter writer, ISerializable value, JsonISerializableContract contract, JsonProperty member, JsonContainerContract collectionContract, JsonProperty containerProperty)
{
if (!JsonTypeReflector.FullyTrusted)
{
@@ -520,7 +522,7 @@ To fix this error either change the environment to be fully trusted, change the
contract.InvokeOnSerializing(value, Serializer.Context);
_serializeStack.Add(value);
WriteObjectStart(writer, value, contract, member, collectionContract);
WriteObjectStart(writer, value, contract, member, collectionContract, containerProperty);
SerializationInfo serializationInfo = new SerializationInfo(contract.UnderlyingType, new FormatterConverter());
value.GetObjectData(serializationInfo, Serializer.Context);
@@ -528,7 +530,7 @@ To fix this error either change the environment to be fully trusted, change the
foreach (SerializationEntry serializationEntry in serializationInfo)
{
writer.WritePropertyName(serializationEntry.Name);
SerializeValue(writer, serializationEntry.Value, GetContractSafe(serializationEntry.Value), null, null);
SerializeValue(writer, serializationEntry.Value, GetContractSafe(serializationEntry.Value), null, null, member);
}
writer.WriteEndObject();
@@ -539,12 +541,12 @@ To fix this error either change the environment to be fully trusted, change the
#endif
#if !(NET35 || NET20 || WINDOWS_PHONE || PORTABLE)
private void SerializeDynamic(JsonWriter writer, IDynamicMetaObjectProvider value, JsonDynamicContract contract, JsonProperty member, JsonContainerContract collectionContract)
private void SerializeDynamic(JsonWriter writer, IDynamicMetaObjectProvider value, JsonDynamicContract contract, JsonProperty member, JsonContainerContract collectionContract, JsonProperty containerProperty)
{
contract.InvokeOnSerializing(value, Serializer.Context);
_serializeStack.Add(value);
WriteObjectStart(writer, value, contract, member, collectionContract);
WriteObjectStart(writer, value, contract, member, collectionContract, containerProperty);
foreach (string memberName in value.GetDynamicMemberNames())
{
@@ -556,7 +558,7 @@ To fix this error either change the environment to be fully trusted, change the
: memberName;
writer.WritePropertyName(resolvedPropertyName);
SerializeValue(writer, memberValue, GetContractSafe(memberValue), null, null);
SerializeValue(writer, memberValue, GetContractSafe(memberValue), null, null, member);
}
}
@@ -567,43 +569,42 @@ To fix this error either change the environment to be fully trusted, change the
}
#endif
private bool ShouldWriteType(TypeNameHandling typeNameHandlingFlag, JsonContract contract, JsonProperty member, JsonContainerContract collectionContract)
private bool ShouldWriteType(TypeNameHandling typeNameHandlingFlag, JsonContract contract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerProperty)
{
TypeNameHandling resolvedTypeNameHandling = ((member != null) ? member.TypeNameHandling : null)
?? ((collectionContract != null) ? collectionContract.ItemTypeNameHandling : null)
TypeNameHandling resolvedTypeNameHandling =
((member != null) ? member.TypeNameHandling : null)
?? ((containerProperty != null) ? containerProperty.ItemTypeNameHandling : null)
?? ((containerContract != null) ? containerContract.ItemTypeNameHandling : null)
?? Serializer.TypeNameHandling;
if (HasFlag(resolvedTypeNameHandling, typeNameHandlingFlag))
return true;
// instance type and the property's type's contract default type are different (no need to put the type in JSON because the type will be created by default)
if (HasFlag(resolvedTypeNameHandling, TypeNameHandling.Auto))
{
if (member != null)
{
if ((member.TypeNameHandling ?? Serializer.TypeNameHandling) == TypeNameHandling.Auto
// instance and property type are different
&& contract.UnderlyingType != member.PropertyType)
{
JsonContract memberTypeContract = Serializer.ContractResolver.ResolveContract(member.PropertyType);
// instance type and the property's type's contract default type are different (no need to put the type in JSON because the type will be created by default)
if (contract.UnderlyingType != memberTypeContract.CreatedType)
if (contract.UnderlyingType != member.PropertyContract.CreatedType)
return true;
}
}
else if (collectionContract != null && collectionContract.ItemContract != null)
else if (containerContract != null && containerContract.ItemContract != null)
{
if (Serializer.TypeNameHandling == TypeNameHandling.Auto && contract.UnderlyingType != collectionContract.ItemContract.CreatedType)
if (contract.UnderlyingType != containerContract.ItemContract.CreatedType)
return true;
}
}
return false;
}
private void SerializeDictionary(JsonWriter writer, IWrappedDictionary values, JsonDictionaryContract contract, JsonProperty member, JsonContainerContract collectionContract)
private void SerializeDictionary(JsonWriter writer, IWrappedDictionary values, JsonDictionaryContract contract, JsonProperty member, JsonContainerContract collectionContract, JsonProperty containerProperty)
{
contract.InvokeOnSerializing(values.UnderlyingDictionary, Serializer.Context);
_serializeStack.Add(values.UnderlyingDictionary);
WriteObjectStart(writer, values.UnderlyingDictionary, contract, member, collectionContract);
WriteObjectStart(writer, values.UnderlyingDictionary, contract, member, collectionContract, containerProperty);
if (contract.ItemContract == null)
contract.ItemContract = Serializer.ContractResolver.ResolveContract(contract.DictionaryValueType ?? typeof(object));
@@ -626,19 +627,19 @@ To fix this error either change the environment to be fully trusted, change the
object value = entry.Value;
JsonContract valueContract = contract.FinalItemContract ?? GetContractSafe(value);
if (ShouldWriteReference(value, null, valueContract, contract))
if (ShouldWriteReference(value, null, valueContract, contract, member))
{
writer.WritePropertyName(propertyName);
WriteReference(writer, value);
}
else
{
if (!CheckForCircularReference(value, null, valueContract, contract))
if (!CheckForCircularReference(value, null, valueContract, contract, member))
continue;
writer.WritePropertyName(propertyName);
SerializeValue(writer, value, valueContract, null, contract);
SerializeValue(writer, value, valueContract, null, contract, member);
}
}
catch (Exception ex)