-Added support for missing JSON errors to error handling
This commit is contained in:
@@ -25,11 +25,13 @@
|
||||
|
||||
#if !PocketPC
|
||||
using System;
|
||||
using System.Diagnostics;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Reflection;
|
||||
using System.Runtime.Serialization;
|
||||
using System.Text;
|
||||
using Newtonsoft.Json.Serialization;
|
||||
using Newtonsoft.Json.Tests;
|
||||
using Newtonsoft.Json.Tests.TestObjects;
|
||||
using NUnit.Framework;
|
||||
@@ -288,6 +290,47 @@ namespace Newtonsoft.Json.Tests.Serialization
|
||||
}", json);
|
||||
}
|
||||
#endif
|
||||
|
||||
public void WhenSerializationErrorDetectedBySerializer_ThenCallbackIsCalled()
|
||||
{
|
||||
// Verify contract is properly finding our callback
|
||||
var resolver = new DefaultContractResolver().ResolveContract(typeof(FooEvent));
|
||||
|
||||
Debug.Assert(resolver.OnError != null);
|
||||
Debug.Assert(resolver.OnError == typeof(FooEvent).GetMethod("OnError", BindingFlags.Instance | BindingFlags.NonPublic));
|
||||
|
||||
var serializer = JsonSerializer.Create(new JsonSerializerSettings
|
||||
{
|
||||
// If I don't specify Error here, the callback isn't called
|
||||
// either, but no exception is thrown.
|
||||
MissingMemberHandling = MissingMemberHandling.Error,
|
||||
});
|
||||
|
||||
// This throws with missing member exception, rather than calling my callback.
|
||||
var foo = serializer.Deserialize<FooEvent>(new JsonTextReader(new StringReader("{ Id: 25 }")));
|
||||
|
||||
// When fixed, this would pass.
|
||||
Debug.Assert(foo.Identifier == 25);
|
||||
}
|
||||
|
||||
public class FooEvent
|
||||
{
|
||||
public int Identifier { get; set; }
|
||||
|
||||
[OnError]
|
||||
private void OnError(StreamingContext context, ErrorContext error)
|
||||
{
|
||||
this.Identifier = 25;
|
||||
|
||||
// Here we could for example manually copy the
|
||||
// persisted "Id" value into the renamed "Identifier"
|
||||
// property, etc.
|
||||
error.Handled = true;
|
||||
|
||||
// We never get here :(
|
||||
Console.WriteLine("Error has been fixed");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
#endif
|
||||
@@ -624,10 +624,12 @@ namespace Newtonsoft.Json.Serialization
|
||||
switch (reader.TokenType)
|
||||
{
|
||||
case JsonToken.PropertyName:
|
||||
object keyValue;
|
||||
object keyValue = reader.Value;
|
||||
try
|
||||
{
|
||||
keyValue = EnsureType(reader.Value, CultureInfo.InvariantCulture, contract.DictionaryKeyType);
|
||||
try
|
||||
{
|
||||
keyValue = EnsureType(keyValue, CultureInfo.InvariantCulture, contract.DictionaryKeyType);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
@@ -637,8 +639,6 @@ namespace Newtonsoft.Json.Serialization
|
||||
if (!ReadForType(reader, contract.DictionaryValueType, null))
|
||||
throw new JsonSerializationException("Unexpected end when deserializing object.");
|
||||
|
||||
try
|
||||
{
|
||||
dictionary[keyValue] = CreateValueNonProperty(reader, contract.DictionaryValueType, GetContractSafe(contract.DictionaryValueType));
|
||||
}
|
||||
catch (Exception ex)
|
||||
@@ -811,6 +811,8 @@ namespace Newtonsoft.Json.Serialization
|
||||
|
||||
contract.InvokeOnDeserializing(newObject, Serializer.Context);
|
||||
|
||||
int initialDepth = reader.Depth;
|
||||
|
||||
bool exit = false;
|
||||
do
|
||||
{
|
||||
@@ -818,6 +820,9 @@ namespace Newtonsoft.Json.Serialization
|
||||
{
|
||||
case JsonToken.PropertyName:
|
||||
string memberName = reader.Value.ToString();
|
||||
|
||||
try
|
||||
{
|
||||
if (!reader.Read())
|
||||
throw new JsonSerializationException("Unexpected end when setting {0}'s value.".FormatWith(CultureInfo.InvariantCulture, memberName));
|
||||
|
||||
@@ -835,6 +840,14 @@ namespace Newtonsoft.Json.Serialization
|
||||
|
||||
newObject.TrySetMember(memberName, value);
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
if (IsErrorHandled(newObject, contract, memberName, ex))
|
||||
HandleError(reader, initialDepth);
|
||||
else
|
||||
throw;
|
||||
}
|
||||
break;
|
||||
case JsonToken.EndObject:
|
||||
exit = true;
|
||||
@@ -1067,6 +1080,8 @@ namespace Newtonsoft.Json.Serialization
|
||||
case JsonToken.PropertyName:
|
||||
string memberName = reader.Value.ToString();
|
||||
|
||||
try
|
||||
{
|
||||
// attempt exact case match first
|
||||
// then try match ignoring case
|
||||
JsonProperty property = contract.Properties.GetClosestMatchProperty(memberName);
|
||||
@@ -1085,8 +1100,6 @@ namespace Newtonsoft.Json.Serialization
|
||||
|
||||
SetRequiredProperty(reader, property, requiredProperties);
|
||||
|
||||
try
|
||||
{
|
||||
SetPropertyValue(property, reader, newObject);
|
||||
}
|
||||
catch (Exception ex)
|
||||
|
||||
Reference in New Issue
Block a user