ServiceStack.Text doesn't support these attributes by default, but you can implement serialization callbacks with Custom Type Configuration, e.g:
JsConfig<MyType>.OnDeserializedFn = o =>
{
o.OnDeserialized(null);
return o;
};
The SerializationHookTests.cs shows how you can use the Type Filters to wire up these callbacks for a type using this helper:
static void AddSerializeHooksForType<T>()
{
Type type = typeof(T);
System.Reflection.MethodInfo[] typeMethods = type
.GetMethods(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);
var onSerializingMethods = typeMethods.Where(m =>
m.GetCustomAttributes(typeof(OnSerializingAttribute), true).Length > 0);
var OnDeserializedMethods = typeMethods.Where(m =>
m.GetCustomAttributes(typeof(OnDeserializedAttribute), true).Length > 0);
var OnSerializedMethods = typeMethods.Where(m =>
m.GetCustomAttributes(typeof(OnSerializedAttribute), true).Length > 0);
Object[] Parameters = { null };
if (onSerializingMethods.Any()) {
ServiceStack.Text.JsConfig<T>.OnSerializingFn = s => {
foreach (var method in onSerializingMethods)
method.Invoke(s, Parameters);
return s;
};
}
if (OnSerializedMethods.Any()) {
ServiceStack.Text.JsConfig<T>.OnSerializedFn = s => {
foreach (var method in OnSerializedMethods)
method.Invoke(s, Parameters);
};
}
if (OnDeserializedMethods.Any()) {
ServiceStack.Text.JsConfig<T>.OnDeserializedFn = s => {
foreach (var method in OnDeserializedMethods)
method.Invoke(s, Parameters);
return s;
};
}
}
Which you can wire up for a type with:
AddSerializeHooksForType<HookTest>();
Where it will call the desired callbacks, e.g:
public class HookTest
{
/// <summary>
/// Will be executed when deserializing starts
/// </summary>
[OnDeserializing]
protected void OnDeserializing(StreamingContext ctx) { }
/// <summary>
/// Will be executed when deserializing finished
/// </summary>
[OnDeserialized]
protected void OnDeserialized(StreamingContext ctx) { }
/// <summary>
/// Will be executed when serializing starts
/// </summary>
[OnSerializing]
protected void OnSerializing(StreamingContext ctx) { }
/// <summary>
/// Will be executed when serializing finished
/// </summary>
[OnSerialized]
protected void OnSerialized(StreamingContext ctx) { }
}