diff --git a/Src/Newtonsoft.Json.Tests/Properties/AssemblyInfo.cs b/Src/Newtonsoft.Json.Tests/Properties/AssemblyInfo.cs index 02c8b3f1..33c4eed9 100644 --- a/Src/Newtonsoft.Json.Tests/Properties/AssemblyInfo.cs +++ b/Src/Newtonsoft.Json.Tests/Properties/AssemblyInfo.cs @@ -73,4 +73,4 @@ using System.Security; // You can specify all the values or you can default the Revision and Build Numbers // by using the '*' as shown below: [assembly: AssemblyVersion("4.5.0.0")] -[assembly: AssemblyFileVersion("4.5.11.15810")] +[assembly: AssemblyFileVersion("4.5.11.15814")] diff --git a/Src/Newtonsoft.Json.Tests/Serialization/SerializationEventAttributeTests.cs b/Src/Newtonsoft.Json.Tests/Serialization/SerializationEventAttributeTests.cs index 3c98b99f..105f6072 100644 --- a/Src/Newtonsoft.Json.Tests/Serialization/SerializationEventAttributeTests.cs +++ b/Src/Newtonsoft.Json.Tests/Serialization/SerializationEventAttributeTests.cs @@ -48,11 +48,11 @@ namespace Newtonsoft.Json.Tests.Serialization [Test] public void ObjectEvents() { - SerializationEventTestObject[] objs = new[] { new SerializationEventTestObject(), new DerivedSerializationEventTestObject() }; + SerializationEventTestObject[] objs = new[] {new SerializationEventTestObject(), new DerivedSerializationEventTestObject()}; - foreach(SerializationEventTestObject current in objs) + foreach (SerializationEventTestObject current in objs) { - SerializationEventTestObject obj = current; + SerializationEventTestObject obj = current; Assert.AreEqual(11, obj.Member1); Assert.AreEqual("Hello World!", obj.Member2); @@ -62,24 +62,24 @@ namespace Newtonsoft.Json.Tests.Serialization string json = JsonConvert.SerializeObject(obj, Formatting.Indented); Assert.AreEqual(@"{ - ""Member1"": 11, - ""Member2"": ""This value went into the data file during serialization."", - ""Member4"": null - }", json); + ""Member1"": 11, + ""Member2"": ""This value went into the data file during serialization."", + ""Member4"": null +}", json); Assert.AreEqual(11, obj.Member1); Assert.AreEqual("This value was reset after serialization.", obj.Member2); Assert.AreEqual("This is a nonserialized value", obj.Member3); Assert.AreEqual(null, obj.Member4); - string expectedError = String.Format("Error message for member Member6 = Error getting value from 'Member6' on '{0}'.", obj.GetType().FullName); + string expectedError = String.Format("Error message for member Member6 = Error getting value from 'Member6' on '{0}'.", obj.GetType().FullName); Assert.AreEqual(expectedError, obj.Member5); JObject o = JObject.Parse(@"{ - ""Member1"": 11, - ""Member2"": ""This value went into the data file during serialization."", - ""Member4"": null - }"); + ""Member1"": 11, + ""Member2"": ""This value went into the data file during serialization."", + ""Member4"": null +}"); o["Member6"] = "Dummy text for error"; obj = (SerializationEventTestObject) JsonConvert.DeserializeObject(o.ToString(), obj.GetType()); diff --git a/Src/Newtonsoft.Json/Properties/AssemblyInfo.cs b/Src/Newtonsoft.Json/Properties/AssemblyInfo.cs index b67176ff..9878a2f8 100644 --- a/Src/Newtonsoft.Json/Properties/AssemblyInfo.cs +++ b/Src/Newtonsoft.Json/Properties/AssemblyInfo.cs @@ -87,6 +87,6 @@ using System.Security; // You can specify all the values or you can default the Revision and Build Numbers // by using the '*' as shown below: [assembly: AssemblyVersion("4.5.0.0")] -[assembly: AssemblyFileVersion("4.5.11.15810")] +[assembly: AssemblyFileVersion("4.5.11.15814")] [assembly: CLSCompliant(true)] diff --git a/Src/Newtonsoft.Json/Serialization/DefaultContractResolver.cs b/Src/Newtonsoft.Json/Serialization/DefaultContractResolver.cs index 49e03762..97e160f5 100644 --- a/Src/Newtonsoft.Json/Serialization/DefaultContractResolver.cs +++ b/Src/Newtonsoft.Json/Serialization/DefaultContractResolver.cs @@ -564,29 +564,29 @@ namespace Newtonsoft.Json.Serialization if (!t.IsGenericType() || (t.GetGenericTypeDefinition() != typeof(ConcurrentDictionary<,>))) contract.OnSerializingMethods = onSerializing; #else - contract.OnSerializingMethods = onSerializing; + contract.OnSerializingMethods.AddRange(onSerializing); #endif } if (onSerialized != null) - contract.OnSerializedMethods = onSerialized; + contract.OnSerializedMethods.AddRange(onSerialized); if (onDeserializing != null) - contract.OnDeserializingMethods = onDeserializing; + contract.OnDeserializingMethods.AddRange(onDeserializing); if (onDeserialized != null) { // ConcurrentDictionary throws an error here so don't use its OnDeserialized - http://json.codeplex.com/discussions/257093 #if !(NET35 || NET20 || SILVERLIGHT || WINDOWS_PHONE || PORTABLE) if (!t.IsGenericType() || (t.GetGenericTypeDefinition() != typeof(ConcurrentDictionary<,>))) - contract.OnDeserializedMethods = onDeserialized; + contract.OnDeserializedMethods.AddRange(onDeserialized); #else - contract.OnDeserializedMethods = onDeserialized; + contract.OnDeserializedMethods.AddRange(onDeserialized); #endif } if (onError != null) - contract.OnErrorMethods = onError; + contract.OnErrorMethods.AddRange(onError); } private void GetCallbackMethodsForType(Type type, out List onSerializing, out List onSerialized, out List onDeserializing, out List onDeserialized, out List onError) diff --git a/Src/Newtonsoft.Json/Serialization/JsonContract.cs b/Src/Newtonsoft.Json/Serialization/JsonContract.cs index 82f12dfe..81175d34 100644 --- a/Src/Newtonsoft.Json/Serialization/JsonContract.cs +++ b/Src/Newtonsoft.Json/Serialization/JsonContract.cs @@ -94,25 +94,25 @@ namespace Newtonsoft.Json.Serialization /// Gets or sets all methods called immediately after deserialization of the object. /// /// The methods called immediately after deserialization of the object. - public List OnDeserializedMethods { get; set; } + public IList OnDeserializedMethods { get; private set; } /// /// Gets or sets all methods called during deserialization of the object. /// /// The methods called during deserialization of the object. - public List OnDeserializingMethods { get; set; } + public IList OnDeserializingMethods { get; private set; } /// /// Gets or sets all methods called after serialization of the object graph. /// /// The methods called after serialization of the object graph. - public List OnSerializedMethods { get; set; } + public IList OnSerializedMethods { get; private set; } /// /// Gets or sets all methods called before serialization of the object. /// /// The methods called before serialization of the object. - public List OnSerializingMethods { get; set; } + public IList OnSerializingMethods { get; private set; } /// /// Gets or sets the method called immediately after deserialization of the object. @@ -120,10 +120,10 @@ namespace Newtonsoft.Json.Serialization /// The method called immediately after deserialization of the object. public MethodInfo OnDeserialized { - get { return (OnDeserializedMethods != null && OnDeserializedMethods.Count > 0 ? OnDeserializedMethods[0] : null); } + get { return (OnDeserializedMethods.Count > 0) ? OnDeserializedMethods[0] : null; } set { - OnDeserializedMethods = new List(); + OnDeserializedMethods.Clear(); OnDeserializedMethods.Add(value); } } @@ -134,10 +134,10 @@ namespace Newtonsoft.Json.Serialization /// The method called during deserialization of the object. public MethodInfo OnDeserializing { - get { return (OnDeserializingMethods != null && OnDeserializingMethods.Count > 0 ? OnDeserializingMethods[0] : null); } + get { return (OnDeserializingMethods.Count > 0) ? OnDeserializingMethods[0] : null; } set { - OnDeserializingMethods = new List(); + OnDeserializingMethods.Clear(); OnDeserializingMethods.Add(value); } } @@ -148,10 +148,10 @@ namespace Newtonsoft.Json.Serialization /// The method called after serialization of the object graph. public MethodInfo OnSerialized { - get { return (OnSerializedMethods != null && OnSerializedMethods.Count > 0 ? OnSerializedMethods[0] : null); } + get { return (OnSerializedMethods.Count > 0) ? OnSerializedMethods[0] : null; } set { - OnSerializedMethods = new List(); + OnSerializedMethods.Clear(); OnSerializedMethods.Add(value); } } @@ -162,10 +162,10 @@ namespace Newtonsoft.Json.Serialization /// The method called before serialization of the object. public MethodInfo OnSerializing { - get { return (OnSerializingMethods != null && OnSerializingMethods.Count > 0 ? OnSerializingMethods[0] : null); } + get { return (OnSerializingMethods.Count > 0) ? OnSerializingMethods[0] : null; } set { - OnSerializingMethods = new List(); + OnSerializingMethods.Clear(); OnSerializingMethods.Add(value); } } @@ -186,7 +186,7 @@ namespace Newtonsoft.Json.Serialization /// Gets or sets all method called when an error is thrown during the serialization of the object. /// /// The methods called when an error is thrown during the serialization of the object. - public List OnErrorMethods { get; set; } + public IList OnErrorMethods { get; private set; } /// /// Gets or sets the method called when an error is thrown during the serialization of the object. @@ -194,18 +194,16 @@ namespace Newtonsoft.Json.Serialization /// The method called when an error is thrown during the serialization of the object. public MethodInfo OnError { - get { return (OnErrorMethods != null && OnErrorMethods.Count > 0 ? OnErrorMethods[0] : null); } + get { return (OnErrorMethods.Count > 0) ? OnErrorMethods[0] : null; } set { - OnErrorMethods = new List(); + OnErrorMethods.Clear(); OnErrorMethods.Add(value); } } internal void InvokeOnSerializing(object o, StreamingContext context) { - if (OnSerializingMethods == null) return; - foreach (MethodInfo mi in OnSerializingMethods) { mi.Invoke(o, new object[] {context}); @@ -214,8 +212,6 @@ namespace Newtonsoft.Json.Serialization internal void InvokeOnSerialized(object o, StreamingContext context) { - if (OnSerializedMethods == null) return; - foreach (MethodInfo mi in OnSerializedMethods) { mi.Invoke(o, new object[] {context}); @@ -224,8 +220,6 @@ namespace Newtonsoft.Json.Serialization internal void InvokeOnDeserializing(object o, StreamingContext context) { - if (OnDeserializingMethods == null) return; - foreach (MethodInfo mi in OnDeserializingMethods) { mi.Invoke(o, new object[] {context}); @@ -234,8 +228,6 @@ namespace Newtonsoft.Json.Serialization internal void InvokeOnDeserialized(object o, StreamingContext context) { - if (OnDeserializedMethods == null) return; - foreach (MethodInfo mi in OnDeserializedMethods) { mi.Invoke(o, new object[] {context}); @@ -244,7 +236,6 @@ namespace Newtonsoft.Json.Serialization internal void InvokeOnError(object o, StreamingContext context, ErrorContext errorContext) { - if (OnErrorMethods == null) return; foreach (MethodInfo mi in OnErrorMethods) { mi.Invoke(o, new object[] {context, errorContext}); @@ -294,6 +285,12 @@ namespace Newtonsoft.Json.Serialization { InternalReadType = ReadType.Read; } + + OnErrorMethods = new List(); + OnSerializedMethods = new List(); + OnSerializingMethods = new List(); + OnDeserializedMethods = new List(); + OnDeserializingMethods = new List(); } } -} +} \ No newline at end of file diff --git a/Src/Newtonsoft.Json/Serialization/JsonSerializerInternalReader.cs b/Src/Newtonsoft.Json/Serialization/JsonSerializerInternalReader.cs index 307b5b2e..c718d0bd 100644 --- a/Src/Newtonsoft.Json/Serialization/JsonSerializerInternalReader.cs +++ b/Src/Newtonsoft.Json/Serialization/JsonSerializerInternalReader.cs @@ -561,10 +561,10 @@ To fix this error either change the JSON to a {1} or change the deserialized typ if (id != null && isTemporaryListReference) throw JsonSerializationException.Create(reader, "Cannot preserve reference to array or readonly list: {0}.".FormatWith(CultureInfo.InvariantCulture, contract.UnderlyingType)); - if (contract.OnSerializingMethods != null && isTemporaryListReference) + if (contract.OnSerializingMethods.Count > 0 && isTemporaryListReference) throw JsonSerializationException.Create(reader, "Cannot call OnSerializing on an array or readonly list: {0}.".FormatWith(CultureInfo.InvariantCulture, contract.UnderlyingType)); - if (contract.OnErrorMethods != null && isTemporaryListReference) + if (contract.OnErrorMethods.Count > 0 && isTemporaryListReference) throw JsonSerializationException.Create(reader, "Cannot call OnError on an array or readonly list: {0}.".FormatWith(CultureInfo.InvariantCulture, contract.UnderlyingType)); if (!arrayContract.IsMultidimensionalArray)