ServiceStack OnDeserialized等效项 [英] ServiceStack OnDeserialized Equivalent
问题描述
我正在实时反序列化Websocket消息.在消息(json字符串)中,我收到一个unix时间戳(长).对每个对象进行反序列化后,我都需要它尽快调用方法,以便我可以捕获消息发送和接收之间的延迟.使用Json.NET很简单,我只是将此方法添加到了DataContract类中:
I am deserialize a websocket message in real time. In the message (string of json) I receive there is a unix timestamp (long). As soon as each object is deserialized I need it to call a method ASAP so that I can capture the delay between the time the message was sent and received. With Json.NET that was simple I just added this method to my DataContract class:
[OnDeserialized]
private void OnDeserialized(StreamingContext context)
{
Timestamp = DateTimeOffset.FromUnixTimeMilliseconds(TimestampLong).LocalDateTime;
Delay = DateTime.Now - Timestamp;
}
出于各种原因,我非常希望继续使用ServiceStack的反序列化器,但是我似乎无法找到一种实现此目的的方法.我确实在大约10年前找到了此StackOverflow帖子,但我希望这种情况有所改变,或者我有一个解决方法可以使用.
I would much prefer to use ServiceStack's deserializer going forward for various reasons but I can't seem to figure out a way to do this. I did find this StackOverflow post from almost 10 years ago but I'm hoping that's changed or that there is a workaround that I can use.
推荐答案
ServiceStack.Text 默认情况下不支持这些属性,但是您可以使用自定义类型配置,例如:
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;
};
SerializationHookTests.cs 显示如何使用此帮助程序使用类型过滤器为这些类型连接这些回调:
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) { }
}
这篇关于ServiceStack OnDeserialized等效项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!