-Changed callback collection properties to be readonly

This commit is contained in:
James Newton-King
2013-02-14 22:38:37 +13:00
parent a03d827537
commit 159c5ee8cd
6 changed files with 44 additions and 47 deletions
@@ -73,4 +73,4 @@ using System.Security;
// You can specify all the values or you can default the Revision and Build Numbers // You can specify all the values or you can default the Revision and Build Numbers
// by using the '*' as shown below: // by using the '*' as shown below:
[assembly: AssemblyVersion("4.5.0.0")] [assembly: AssemblyVersion("4.5.0.0")]
[assembly: AssemblyFileVersion("4.5.11.15810")] [assembly: AssemblyFileVersion("4.5.11.15814")]
@@ -48,9 +48,9 @@ namespace Newtonsoft.Json.Tests.Serialization
[Test] [Test]
public void ObjectEvents() 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;
@@ -65,7 +65,7 @@ namespace Newtonsoft.Json.Tests.Serialization
""Member1"": 11, ""Member1"": 11,
""Member2"": ""This value went into the data file during serialization."", ""Member2"": ""This value went into the data file during serialization."",
""Member4"": null ""Member4"": null
}", json); }", json);
Assert.AreEqual(11, obj.Member1); Assert.AreEqual(11, obj.Member1);
Assert.AreEqual("This value was reset after serialization.", obj.Member2); Assert.AreEqual("This value was reset after serialization.", obj.Member2);
@@ -79,7 +79,7 @@ namespace Newtonsoft.Json.Tests.Serialization
""Member1"": 11, ""Member1"": 11,
""Member2"": ""This value went into the data file during serialization."", ""Member2"": ""This value went into the data file during serialization."",
""Member4"": null ""Member4"": null
}"); }");
o["Member6"] = "Dummy text for error"; o["Member6"] = "Dummy text for error";
obj = (SerializationEventTestObject) JsonConvert.DeserializeObject(o.ToString(), obj.GetType()); obj = (SerializationEventTestObject) JsonConvert.DeserializeObject(o.ToString(), obj.GetType());
@@ -87,6 +87,6 @@ using System.Security;
// You can specify all the values or you can default the Revision and Build Numbers // You can specify all the values or you can default the Revision and Build Numbers
// by using the '*' as shown below: // by using the '*' as shown below:
[assembly: AssemblyVersion("4.5.0.0")] [assembly: AssemblyVersion("4.5.0.0")]
[assembly: AssemblyFileVersion("4.5.11.15810")] [assembly: AssemblyFileVersion("4.5.11.15814")]
[assembly: CLSCompliant(true)] [assembly: CLSCompliant(true)]
@@ -564,29 +564,29 @@ namespace Newtonsoft.Json.Serialization
if (!t.IsGenericType() || (t.GetGenericTypeDefinition() != typeof(ConcurrentDictionary<,>))) if (!t.IsGenericType() || (t.GetGenericTypeDefinition() != typeof(ConcurrentDictionary<,>)))
contract.OnSerializingMethods = onSerializing; contract.OnSerializingMethods = onSerializing;
#else #else
contract.OnSerializingMethods = onSerializing; contract.OnSerializingMethods.AddRange(onSerializing);
#endif #endif
} }
if (onSerialized != null) if (onSerialized != null)
contract.OnSerializedMethods = onSerialized; contract.OnSerializedMethods.AddRange(onSerialized);
if (onDeserializing != null) if (onDeserializing != null)
contract.OnDeserializingMethods = onDeserializing; contract.OnDeserializingMethods.AddRange(onDeserializing);
if (onDeserialized != null) if (onDeserialized != null)
{ {
// ConcurrentDictionary throws an error here so don't use its OnDeserialized - http://json.codeplex.com/discussions/257093 // 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 !(NET35 || NET20 || SILVERLIGHT || WINDOWS_PHONE || PORTABLE)
if (!t.IsGenericType() || (t.GetGenericTypeDefinition() != typeof(ConcurrentDictionary<,>))) if (!t.IsGenericType() || (t.GetGenericTypeDefinition() != typeof(ConcurrentDictionary<,>)))
contract.OnDeserializedMethods = onDeserialized; contract.OnDeserializedMethods.AddRange(onDeserialized);
#else #else
contract.OnDeserializedMethods = onDeserialized; contract.OnDeserializedMethods.AddRange(onDeserialized);
#endif #endif
} }
if (onError != null) if (onError != null)
contract.OnErrorMethods = onError; contract.OnErrorMethods.AddRange(onError);
} }
private void GetCallbackMethodsForType(Type type, out List<MethodInfo> onSerializing, out List<MethodInfo> onSerialized, out List<MethodInfo> onDeserializing, out List<MethodInfo> onDeserialized, out List<MethodInfo> onError) private void GetCallbackMethodsForType(Type type, out List<MethodInfo> onSerializing, out List<MethodInfo> onSerialized, out List<MethodInfo> onDeserializing, out List<MethodInfo> onDeserialized, out List<MethodInfo> onError)
@@ -94,25 +94,25 @@ namespace Newtonsoft.Json.Serialization
/// Gets or sets all methods called immediately after deserialization of the object. /// Gets or sets all methods called immediately after deserialization of the object.
/// </summary> /// </summary>
/// <value>The methods called immediately after deserialization of the object.</value> /// <value>The methods called immediately after deserialization of the object.</value>
public List<MethodInfo> OnDeserializedMethods { get; set; } public IList<MethodInfo> OnDeserializedMethods { get; private set; }
/// <summary> /// <summary>
/// Gets or sets all methods called during deserialization of the object. /// Gets or sets all methods called during deserialization of the object.
/// </summary> /// </summary>
/// <value>The methods called during deserialization of the object.</value> /// <value>The methods called during deserialization of the object.</value>
public List<MethodInfo> OnDeserializingMethods { get; set; } public IList<MethodInfo> OnDeserializingMethods { get; private set; }
/// <summary> /// <summary>
/// Gets or sets all methods called after serialization of the object graph. /// Gets or sets all methods called after serialization of the object graph.
/// </summary> /// </summary>
/// <value>The methods called after serialization of the object graph.</value> /// <value>The methods called after serialization of the object graph.</value>
public List<MethodInfo> OnSerializedMethods { get; set; } public IList<MethodInfo> OnSerializedMethods { get; private set; }
/// <summary> /// <summary>
/// Gets or sets all methods called before serialization of the object. /// Gets or sets all methods called before serialization of the object.
/// </summary> /// </summary>
/// <value>The methods called before serialization of the object.</value> /// <value>The methods called before serialization of the object.</value>
public List<MethodInfo> OnSerializingMethods { get; set; } public IList<MethodInfo> OnSerializingMethods { get; private set; }
/// <summary> /// <summary>
/// Gets or sets the method called immediately after deserialization of the object. /// Gets or sets the method called immediately after deserialization of the object.
@@ -120,10 +120,10 @@ namespace Newtonsoft.Json.Serialization
/// <value>The method called immediately after deserialization of the object.</value> /// <value>The method called immediately after deserialization of the object.</value>
public MethodInfo OnDeserialized public MethodInfo OnDeserialized
{ {
get { return (OnDeserializedMethods != null && OnDeserializedMethods.Count > 0 ? OnDeserializedMethods[0] : null); } get { return (OnDeserializedMethods.Count > 0) ? OnDeserializedMethods[0] : null; }
set set
{ {
OnDeserializedMethods = new List<MethodInfo>(); OnDeserializedMethods.Clear();
OnDeserializedMethods.Add(value); OnDeserializedMethods.Add(value);
} }
} }
@@ -134,10 +134,10 @@ namespace Newtonsoft.Json.Serialization
/// <value>The method called during deserialization of the object.</value> /// <value>The method called during deserialization of the object.</value>
public MethodInfo OnDeserializing public MethodInfo OnDeserializing
{ {
get { return (OnDeserializingMethods != null && OnDeserializingMethods.Count > 0 ? OnDeserializingMethods[0] : null); } get { return (OnDeserializingMethods.Count > 0) ? OnDeserializingMethods[0] : null; }
set set
{ {
OnDeserializingMethods = new List<MethodInfo>(); OnDeserializingMethods.Clear();
OnDeserializingMethods.Add(value); OnDeserializingMethods.Add(value);
} }
} }
@@ -148,10 +148,10 @@ namespace Newtonsoft.Json.Serialization
/// <value>The method called after serialization of the object graph.</value> /// <value>The method called after serialization of the object graph.</value>
public MethodInfo OnSerialized public MethodInfo OnSerialized
{ {
get { return (OnSerializedMethods != null && OnSerializedMethods.Count > 0 ? OnSerializedMethods[0] : null); } get { return (OnSerializedMethods.Count > 0) ? OnSerializedMethods[0] : null; }
set set
{ {
OnSerializedMethods = new List<MethodInfo>(); OnSerializedMethods.Clear();
OnSerializedMethods.Add(value); OnSerializedMethods.Add(value);
} }
} }
@@ -162,10 +162,10 @@ namespace Newtonsoft.Json.Serialization
/// <value>The method called before serialization of the object.</value> /// <value>The method called before serialization of the object.</value>
public MethodInfo OnSerializing public MethodInfo OnSerializing
{ {
get { return (OnSerializingMethods != null && OnSerializingMethods.Count > 0 ? OnSerializingMethods[0] : null); } get { return (OnSerializingMethods.Count > 0) ? OnSerializingMethods[0] : null; }
set set
{ {
OnSerializingMethods = new List<MethodInfo>(); OnSerializingMethods.Clear();
OnSerializingMethods.Add(value); 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. /// Gets or sets all method called when an error is thrown during the serialization of the object.
/// </summary> /// </summary>
/// <value>The methods called when an error is thrown during the serialization of the object.</value> /// <value>The methods called when an error is thrown during the serialization of the object.</value>
public List<MethodInfo> OnErrorMethods { get; set; } public IList<MethodInfo> OnErrorMethods { get; private set; }
/// <summary> /// <summary>
/// Gets or sets the method called when an error is thrown during the serialization of the object. /// 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
/// <value>The method called when an error is thrown during the serialization of the object.</value> /// <value>The method called when an error is thrown during the serialization of the object.</value>
public MethodInfo OnError public MethodInfo OnError
{ {
get { return (OnErrorMethods != null && OnErrorMethods.Count > 0 ? OnErrorMethods[0] : null); } get { return (OnErrorMethods.Count > 0) ? OnErrorMethods[0] : null; }
set set
{ {
OnErrorMethods = new List<MethodInfo>(); OnErrorMethods.Clear();
OnErrorMethods.Add(value); OnErrorMethods.Add(value);
} }
} }
internal void InvokeOnSerializing(object o, StreamingContext context) internal void InvokeOnSerializing(object o, StreamingContext context)
{ {
if (OnSerializingMethods == null) return;
foreach (MethodInfo mi in OnSerializingMethods) foreach (MethodInfo mi in OnSerializingMethods)
{ {
mi.Invoke(o, new object[] {context}); mi.Invoke(o, new object[] {context});
@@ -214,8 +212,6 @@ namespace Newtonsoft.Json.Serialization
internal void InvokeOnSerialized(object o, StreamingContext context) internal void InvokeOnSerialized(object o, StreamingContext context)
{ {
if (OnSerializedMethods == null) return;
foreach (MethodInfo mi in OnSerializedMethods) foreach (MethodInfo mi in OnSerializedMethods)
{ {
mi.Invoke(o, new object[] {context}); mi.Invoke(o, new object[] {context});
@@ -224,8 +220,6 @@ namespace Newtonsoft.Json.Serialization
internal void InvokeOnDeserializing(object o, StreamingContext context) internal void InvokeOnDeserializing(object o, StreamingContext context)
{ {
if (OnDeserializingMethods == null) return;
foreach (MethodInfo mi in OnDeserializingMethods) foreach (MethodInfo mi in OnDeserializingMethods)
{ {
mi.Invoke(o, new object[] {context}); mi.Invoke(o, new object[] {context});
@@ -234,8 +228,6 @@ namespace Newtonsoft.Json.Serialization
internal void InvokeOnDeserialized(object o, StreamingContext context) internal void InvokeOnDeserialized(object o, StreamingContext context)
{ {
if (OnDeserializedMethods == null) return;
foreach (MethodInfo mi in OnDeserializedMethods) foreach (MethodInfo mi in OnDeserializedMethods)
{ {
mi.Invoke(o, new object[] {context}); mi.Invoke(o, new object[] {context});
@@ -244,7 +236,6 @@ namespace Newtonsoft.Json.Serialization
internal void InvokeOnError(object o, StreamingContext context, ErrorContext errorContext) internal void InvokeOnError(object o, StreamingContext context, ErrorContext errorContext)
{ {
if (OnErrorMethods == null) return;
foreach (MethodInfo mi in OnErrorMethods) foreach (MethodInfo mi in OnErrorMethods)
{ {
mi.Invoke(o, new object[] {context, errorContext}); mi.Invoke(o, new object[] {context, errorContext});
@@ -294,6 +285,12 @@ namespace Newtonsoft.Json.Serialization
{ {
InternalReadType = ReadType.Read; InternalReadType = ReadType.Read;
} }
OnErrorMethods = new List<MethodInfo>();
OnSerializedMethods = new List<MethodInfo>();
OnSerializingMethods = new List<MethodInfo>();
OnDeserializedMethods = new List<MethodInfo>();
OnDeserializingMethods = new List<MethodInfo>();
} }
} }
} }
@@ -561,10 +561,10 @@ To fix this error either change the JSON to a {1} or change the deserialized typ
if (id != null && isTemporaryListReference) if (id != null && isTemporaryListReference)
throw JsonSerializationException.Create(reader, "Cannot preserve reference to array or readonly list: {0}.".FormatWith(CultureInfo.InvariantCulture, contract.UnderlyingType)); 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)); 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)); throw JsonSerializationException.Create(reader, "Cannot call OnError on an array or readonly list: {0}.".FormatWith(CultureInfo.InvariantCulture, contract.UnderlyingType));
if (!arrayContract.IsMultidimensionalArray) if (!arrayContract.IsMultidimensionalArray)