-Fixed error when wrapping an exception in a JsonConverter

This commit is contained in:
James Newton-King
2013-12-23 22:42:38 +13:00
parent d529190b46
commit 40c24307de
8 changed files with 186 additions and 6 deletions
@@ -1,6 +1,10 @@
using System.Collections.Generic;
using System.Globalization;
#if NET20
using Newtonsoft.Json.Utilities.LinqBridge;
#else
using System.Linq;
#endif
using Newtonsoft.Json.Utilities;
namespace Newtonsoft.Json.Linq.JsonPath
@@ -30,7 +34,7 @@ namespace Newtonsoft.Json.Linq.JsonPath
else
{
if (errorWhenNoMatch)
throw new JsonException("Properties {0} not valid on {1}.".FormatWith(CultureInfo.InvariantCulture, string.Join(", ", Names.Select(n => "'" + n + "'")), t.GetType().Name));
throw new JsonException("Properties {0} not valid on {1}.".FormatWith(CultureInfo.InvariantCulture, string.Join(", ", Names.Select(n => "'" + n + "'").ToArray()), t.GetType().Name));
}
}
}
@@ -91,5 +91,5 @@ using System.Security;
// by using the '*' as shown below:
[assembly: AssemblyVersion("4.5.0.0")]
[assembly: AssemblyFileVersion("5.0.8.16815")]
[assembly: AssemblyFileVersion("5.0.8.16823")]
[assembly: CLSCompliant(true)]
@@ -43,7 +43,7 @@ namespace Newtonsoft.Json.Serialization
internal bool Traced { get; set; }
/// <summary>
/// Gets or sets the error.
/// Gets the error.
/// </summary>
/// <value>The error.</value>
public Exception Error { get; private set; }
@@ -174,6 +174,10 @@ namespace Newtonsoft.Json.Serialization
}
else
{
// clear context in case serializer is being used inside a converter
// if the converter wraps the error then not clearing the context will cause this error:
// "Current error context error is different to requested error."
ClearErrorContext();
throw;
}
}
@@ -65,7 +65,28 @@ namespace Newtonsoft.Json.Serialization
if (objectType != null)
_rootContract = Serializer._contractResolver.ResolveContract(objectType);
SerializeValue(jsonWriter, value, GetContractSafe(value), null, null, null);
JsonContract contract = GetContractSafe(value);
try
{
SerializeValue(jsonWriter, value, contract, null, null, null);
}
catch (Exception ex)
{
if (IsErrorHandled(null, contract, null, null, jsonWriter.Path, ex))
{
HandleError(jsonWriter, 0);
}
else
{
// clear context in case serializer is being used inside a converter
// if the converter wraps the error then not clearing the context will cause this error:
// "Current error context error is different to requested error."
ClearErrorContext();
throw;
}
}
}
private JsonSerializerProxy GetInternalSerializer()