-Added serialization constructors to all Exceptions

-Added support for serialization event attributes on base classes
-Changed WriteEndObject, WriteEndArray, WriteEndConstructor on JsonWriter to be virtual
This commit is contained in:
JamesNK
2011-07-04 09:16:23 +00:00
parent b607b40753
commit c1cafd078d
9 changed files with 141 additions and 21 deletions
@@ -41,5 +41,5 @@ using System.Runtime.InteropServices;
// by using the '*' as shown below:
[assembly: AssemblyVersion("4.0.2.0")]
#if !PocketPC
[assembly: AssemblyFileVersion("4.0.2.13825")]
[assembly: AssemblyFileVersion("4.0.2.13904")]
#endif
@@ -232,6 +232,32 @@ namespace Newtonsoft.Json.Tests.Serialization
// This value was set after deserialization.
}
public class SerializationEventBaseTestObject
{
public string TestMember { get; set; }
[OnSerializing]
internal void OnSerializingMethod(StreamingContext context)
{
TestMember = "Set!";
}
}
public class SerializationEventContextSubClassTestObject : SerializationEventBaseTestObject
{
}
[Test]
public void SerializationEventContextTestObjectSubClassTest()
{
SerializationEventContextSubClassTestObject obj = new SerializationEventContextSubClassTestObject();
string json = JsonConvert.SerializeObject(obj, Formatting.Indented);
Assert.AreEqual(@"{
""TestMember"": ""Set!""
}", json);
}
#if !SILVERLIGHT
public class SerializationEventContextTestObject
{
@@ -24,6 +24,7 @@
#endregion
using System;
using System.Runtime.Serialization;
namespace Newtonsoft.Json
{
@@ -76,6 +77,20 @@ namespace Newtonsoft.Json
{
}
#if !(WINDOWS_PHONE || SILVERLIGHT)
/// <summary>
/// Initializes a new instance of the <see cref="JsonReaderException"/> class.
/// </summary>
/// <param name="info">The <see cref="T:System.Runtime.Serialization.SerializationInfo"/> that holds the serialized object data about the exception being thrown.</param>
/// <param name="context">The <see cref="T:System.Runtime.Serialization.StreamingContext"/> that contains contextual information about the source or destination.</param>
/// <exception cref="T:System.ArgumentNullException">The <paramref name="info"/> parameter is null. </exception>
/// <exception cref="T:System.Runtime.Serialization.SerializationException">The class name is null or <see cref="P:System.Exception.HResult"/> is zero (0). </exception>
public JsonReaderException(SerializationInfo info, StreamingContext context)
: base(info, context)
{
}
#endif
internal JsonReaderException(string message, Exception innerException, int lineNumber, int linePosition)
: base(message, innerException)
{
@@ -25,6 +25,7 @@
using System;
using System.Collections.Generic;
using System.Runtime.Serialization;
using System.Text;
namespace Newtonsoft.Json
@@ -64,5 +65,19 @@ namespace Newtonsoft.Json
: base(message, innerException)
{
}
#if !(WINDOWS_PHONE || SILVERLIGHT)
/// <summary>
/// Initializes a new instance of the <see cref="JsonSerializationException"/> class.
/// </summary>
/// <param name="info">The <see cref="T:System.Runtime.Serialization.SerializationInfo"/> that holds the serialized object data about the exception being thrown.</param>
/// <param name="context">The <see cref="T:System.Runtime.Serialization.StreamingContext"/> that contains contextual information about the source or destination.</param>
/// <exception cref="T:System.ArgumentNullException">The <paramref name="info"/> parameter is null. </exception>
/// <exception cref="T:System.Runtime.Serialization.SerializationException">The class name is null or <see cref="P:System.Exception.HResult"/> is zero (0). </exception>
public JsonSerializationException(SerializationInfo info, StreamingContext context)
: base(info, context)
{
}
#endif
}
}
}
+4 -4
View File
@@ -246,7 +246,7 @@ namespace Newtonsoft.Json
/// <summary>
/// Writes the end of a Json object.
/// </summary>
public void WriteEndObject()
public virtual void WriteEndObject()
{
AutoCompleteClose(JsonToken.EndObject);
}
@@ -263,7 +263,7 @@ namespace Newtonsoft.Json
/// <summary>
/// Writes the end of an array.
/// </summary>
public void WriteEndArray()
public virtual void WriteEndArray()
{
AutoCompleteClose(JsonToken.EndArray);
}
@@ -281,7 +281,7 @@ namespace Newtonsoft.Json
/// <summary>
/// Writes the end constructor.
/// </summary>
public void WriteEndConstructor()
public virtual void WriteEndConstructor()
{
AutoCompleteClose(JsonToken.EndConstructor);
}
@@ -298,7 +298,7 @@ namespace Newtonsoft.Json
/// <summary>
/// Writes the end of the current Json object or array.
/// </summary>
public void WriteEnd()
public virtual void WriteEnd()
{
WriteEnd(Peek());
}
+16 -1
View File
@@ -25,6 +25,7 @@
using System;
using System.Collections.Generic;
using System.Runtime.Serialization;
using System.Text;
namespace Newtonsoft.Json
@@ -64,5 +65,19 @@ namespace Newtonsoft.Json
: base(message, innerException)
{
}
#if !(WINDOWS_PHONE || SILVERLIGHT)
/// <summary>
/// Initializes a new instance of the <see cref="JsonWriterException"/> class.
/// </summary>
/// <param name="info">The <see cref="T:System.Runtime.Serialization.SerializationInfo"/> that holds the serialized object data about the exception being thrown.</param>
/// <param name="context">The <see cref="T:System.Runtime.Serialization.StreamingContext"/> that contains contextual information about the source or destination.</param>
/// <exception cref="T:System.ArgumentNullException">The <paramref name="info"/> parameter is null. </exception>
/// <exception cref="T:System.Runtime.Serialization.SerializationException">The class name is null or <see cref="P:System.Exception.HResult"/> is zero (0). </exception>
public JsonWriterException(SerializationInfo info, StreamingContext context)
: base(info, context)
{
}
#endif
}
}
}
@@ -110,7 +110,7 @@ using System.Security;
// by using the '*' as shown below:
[assembly: AssemblyVersion("4.0.2.0")]
#if !PocketPC
[assembly: AssemblyFileVersion("4.0.2.13825")]
[assembly: AssemblyFileVersion("4.0.2.13904")]
#endif
[assembly: CLSCompliant(true)]
@@ -24,6 +24,7 @@
#endregion
using System;
using System.Runtime.Serialization;
namespace Newtonsoft.Json.Schema
{
@@ -76,6 +77,20 @@ namespace Newtonsoft.Json.Schema
{
}
#if !(WINDOWS_PHONE || SILVERLIGHT)
/// <summary>
/// Initializes a new instance of the <see cref="JsonSchemaException"/> class.
/// </summary>
/// <param name="info">The <see cref="T:System.Runtime.Serialization.SerializationInfo"/> that holds the serialized object data about the exception being thrown.</param>
/// <param name="context">The <see cref="T:System.Runtime.Serialization.StreamingContext"/> that contains contextual information about the source or destination.</param>
/// <exception cref="T:System.ArgumentNullException">The <paramref name="info"/> parameter is null. </exception>
/// <exception cref="T:System.Runtime.Serialization.SerializationException">The class name is null or <see cref="P:System.Exception.HResult"/> is zero (0). </exception>
public JsonSchemaException(SerializationInfo info, StreamingContext context)
: base(info, context)
{
}
#endif
internal JsonSchemaException(string message, Exception innerException, int lineNumber, int linePosition)
: base(message, innerException)
{
@@ -440,7 +440,43 @@ namespace Newtonsoft.Json.Serialization
ReflectionUtils.GetDefaultConstructor(contract.CreatedType) == null);
}
foreach (MethodInfo method in contract.UnderlyingType.GetMethods(BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly))
ResolveCallbackMethods(contract, contract.UnderlyingType);
}
private void ResolveCallbackMethods(JsonContract contract, Type t)
{
if (t.BaseType != null)
ResolveCallbackMethods(contract, t.BaseType);
MethodInfo onSerializing;
MethodInfo onSerialized;
MethodInfo onDeserializing;
MethodInfo onDeserialized;
MethodInfo onError;
GetCallbackMethodsForType(t, out onSerializing, out onSerialized, out onDeserializing, out onDeserialized, out onError);
if (onSerializing != null)
contract.OnSerializing = onSerializing;
if (onSerialized != null)
contract.OnSerialized = onSerialized;
if (onDeserializing != null)
contract.OnDeserializing = onDeserializing;
if (onDeserialized != null)
contract.OnDeserialized = onDeserialized;
if (onError != null)
contract.OnError = onError;
}
private void GetCallbackMethodsForType(Type type, out MethodInfo onSerializing, out MethodInfo onSerialized, out MethodInfo onDeserializing, out MethodInfo onDeserialized, out MethodInfo onError)
{
onSerializing = null;
onSerialized = null;
onDeserializing = null;
onDeserialized = null;
onError = null;
foreach (MethodInfo method in type.GetMethods(BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly))
{
// compact framework errors when getting parameters for a generic method
// lame, but generic methods should not be callbacks anyway
@@ -450,27 +486,25 @@ namespace Newtonsoft.Json.Serialization
Type prevAttributeType = null;
ParameterInfo[] parameters = method.GetParameters();
#if !PocketPC
if (IsValidCallback(method, parameters, typeof(OnSerializingAttribute), contract.OnSerializing, ref prevAttributeType))
if (IsValidCallback(method, parameters, typeof(OnSerializingAttribute), onSerializing, ref prevAttributeType))
{
contract.OnSerializing = method;
onSerializing = method;
}
if (IsValidCallback(method, parameters, typeof(OnSerializedAttribute), contract.OnSerialized, ref prevAttributeType))
if (IsValidCallback(method, parameters, typeof(OnSerializedAttribute), onSerialized, ref prevAttributeType))
{
contract.OnSerialized = method;
onSerialized = method;
}
if (IsValidCallback(method, parameters, typeof(OnDeserializingAttribute), contract.OnDeserializing, ref prevAttributeType))
if (IsValidCallback(method, parameters, typeof(OnDeserializingAttribute), onDeserializing, ref prevAttributeType))
{
contract.OnDeserializing = method;
onDeserializing = method;
}
if (IsValidCallback(method, parameters, typeof(OnDeserializedAttribute), contract.OnDeserialized, ref prevAttributeType))
if (IsValidCallback(method, parameters, typeof(OnDeserializedAttribute), onDeserialized, ref prevAttributeType))
{
contract.OnDeserialized = method;
onDeserialized = method;
}
#endif
if (IsValidCallback(method, parameters, typeof(OnErrorAttribute), contract.OnError, ref prevAttributeType))
if (IsValidCallback(method, parameters, typeof(OnErrorAttribute), onError, ref prevAttributeType))
{
contract.OnError = method;
onError = method;
}
}
}