-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
// by using the '*' as shown below:
[assembly: AssemblyVersion("4.5.0.0")]
[assembly: AssemblyFileVersion("4.5.11.15810")]
[assembly: AssemblyFileVersion("4.5.11.15814")]
@@ -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());
@@ -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)]
@@ -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<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.
/// </summary>
/// <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>
/// Gets or sets all methods called during deserialization of the object.
/// </summary>
/// <value>The methods called during deserialization of the object.</value>
public List<MethodInfo> OnDeserializingMethods { get; set; }
public IList<MethodInfo> OnDeserializingMethods { get; private set; }
/// <summary>
/// Gets or sets all methods called after serialization of the object graph.
/// </summary>
/// <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>
/// Gets or sets all methods called before serialization of the object.
/// </summary>
/// <value>The methods called before serialization of the object.</value>
public List<MethodInfo> OnSerializingMethods { get; set; }
public IList<MethodInfo> OnSerializingMethods { get; private set; }
/// <summary>
/// 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>
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<MethodInfo>();
OnDeserializedMethods.Clear();
OnDeserializedMethods.Add(value);
}
}
@@ -134,10 +134,10 @@ namespace Newtonsoft.Json.Serialization
/// <value>The method called during deserialization of the object.</value>
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<MethodInfo>();
OnDeserializingMethods.Clear();
OnDeserializingMethods.Add(value);
}
}
@@ -148,10 +148,10 @@ namespace Newtonsoft.Json.Serialization
/// <value>The method called after serialization of the object graph.</value>
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<MethodInfo>();
OnSerializedMethods.Clear();
OnSerializedMethods.Add(value);
}
}
@@ -162,10 +162,10 @@ namespace Newtonsoft.Json.Serialization
/// <value>The method called before serialization of the object.</value>
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<MethodInfo>();
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.
/// </summary>
/// <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>
/// 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>
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<MethodInfo>();
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<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)
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)