Skip to content
This repository was archived by the owner on Dec 24, 2022. It is now read-only.

Commit c42fffb

Browse files
committed
Add JsConfig.OnDeserializationError for better introspection of deseriailization errors
1 parent 5ac379e commit c42fffb

File tree

5 files changed

+25
-0
lines changed

5 files changed

+25
-0
lines changed

src/ServiceStack.Text/Common/DeserializeTypeRefJson.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,7 @@ internal static object StringToType(
160160
}
161161
catch (Exception e)
162162
{
163+
if (JsConfig.OnDeserializationError != null) JsConfig.OnDeserializationError(instance, propType, propertyName, propertyValueStr, e);
163164
if (JsConfig.ThrowOnDeserializationError) throw DeserializeTypeRef.GetSerializationException(propertyName, propertyValueStr, propType, e);
164165
else Tracer.Instance.WriteWarning("WARN: failed to set dynamic property {0} with: {1}", propertyName, propertyValueStr);
165166
}
@@ -176,6 +177,7 @@ internal static object StringToType(
176177
}
177178
catch (Exception e)
178179
{
180+
if (JsConfig.OnDeserializationError != null) JsConfig.OnDeserializationError(instance, propType, propertyName, propertyValueStr, e);
179181
if (JsConfig.ThrowOnDeserializationError) throw DeserializeTypeRef.GetSerializationException(propertyName, propertyValueStr, typeAccessor.PropertyType, e);
180182
else Tracer.Instance.WriteWarning("WARN: failed to set property {0} with: {1}", propertyName, propertyValueStr);
181183
}

src/ServiceStack.Text/Common/DeserializeTypeRefJsv.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,7 @@ internal static object StringToType(
113113
}
114114
catch (Exception e)
115115
{
116+
if (JsConfig.OnDeserializationError != null) JsConfig.OnDeserializationError(instance, propType, propertyName, propertyValueStr, e);
116117
if (JsConfig.ThrowOnDeserializationError) throw DeserializeTypeRef.GetSerializationException(propertyName, propertyValueStr, propType, e);
117118
else Tracer.Instance.WriteWarning("WARN: failed to set dynamic property {0} with: {1}", propertyName, propertyValueStr);
118119
}
@@ -129,6 +130,7 @@ internal static object StringToType(
129130
}
130131
catch (Exception e)
131132
{
133+
if (JsConfig.OnDeserializationError != null) JsConfig.OnDeserializationError(instance, propType, propertyName, propertyValueStr, e);
132134
if (JsConfig.ThrowOnDeserializationError) throw DeserializeTypeRef.GetSerializationException(propertyName, propertyValueStr, propType, e);
133135
else Tracer.Instance.WriteWarning("WARN: failed to set property {0} with: {1}", propertyName, propertyValueStr);
134136
}

src/ServiceStack.Text/Common/JsDelegates.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,4 +33,6 @@ namespace ServiceStack.Text.Common
3333
public delegate object ConvertObjectDelegate(object fromObject);
3434

3535
public delegate object ConvertInstanceDelegate(object obj, Type type);
36+
37+
public delegate void DeserializationErrorDelegate(object instance, Type propertyType, string propertyName, string propertyValueStr, Exception ex);
3638
}

src/ServiceStack.Text/JsConfig.cs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -516,6 +516,22 @@ public static bool EscapeUnicode
516516
}
517517
}
518518

519+
/// <summary>
520+
/// Gets or sets a value indicating if the framework should call an error handler when
521+
/// an exception happens during the deserialization.
522+
/// </summary>
523+
/// <remarks>Parameters have following meaning in order: deserialized entity, property name, parsed value, property type, caught exception.</remarks>
524+
private static DeserializationErrorDelegate sOnDeserializationError;
525+
public static DeserializationErrorDelegate OnDeserializationError
526+
{
527+
get
528+
{
529+
return (JsConfigScope.Current != null ? JsConfigScope.Current.OnDeserializationError : null)
530+
?? sOnDeserializationError;
531+
}
532+
set { sOnDeserializationError = value; }
533+
}
534+
519535
internal static HashSet<Type> HasSerializeFn = new HashSet<Type>();
520536

521537
internal static HashSet<Type> HasIncludeDefaultValue = new HashSet<Type>();
@@ -691,6 +707,7 @@ public static void Reset()
691707
sAssumeUtc = null;
692708
sAppendUtcOffset = null;
693709
sEscapeUnicode = null;
710+
sOnDeserializationError = null;
694711
sIncludePublicFields = null;
695712
sReuseStringBuffer = null;
696713
HasSerializeFn = new HashSet<Type>();

src/ServiceStack.Text/JsConfigScope.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using System.Text;
55
using System.Threading;
66
using System.Diagnostics;
7+
using ServiceStack.Text.Common;
78

89
namespace ServiceStack.Text
910
{
@@ -81,6 +82,7 @@ public void Dispose()
8182
public bool? IncludePublicFields { get; set; }
8283
public bool? ReuseStringBuffer { get; set; }
8384
public int? MaxDepth { get; set; }
85+
public DeserializationErrorDelegate OnDeserializationError { get; set; }
8486
public EmptyCtorFactoryDelegate ModelFactory { get; set; }
8587
public string[] ExcludePropertyReferences { get; set; }
8688
public HashSet<Type> ExcludeTypes { get; set; }

0 commit comments

Comments
 (0)