protobuf.net&条件序列化 [英] protobuf.net & conditional serialization

查看:79
本文介绍了protobuf.net&条件序列化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们正在使用protobuf.net在移动设备和后端服务之间序列化类,但是现在我们需要根据用户的上下文来调整发送回客户端的内容。

We are using protobuf.net to serialize classes between mobile devices and back end services, but we now need to adjust what is sent back to the client based upon the 'context' of the user.

通常,我们将通过实现ISerializable接口并查看上下文值来确定要序列化的内容,以实现此目的。

We would typically do this by implementing the ISerializable interface and look at the context value to then decide what to serialize. Similarly in the constructor we would then deserialize the provided values.

但是似乎proerbuf.net的ISerializable没有实现/支持(我知道为什么),因此我们采用了 ShouldSerialize *和 OnSerializing方法来解决此问题。但是,这确实意味着我们最终不得不在感觉不正确的每个类中存储StreamingContext。我们可以将其保留在全球范围内,但感觉也不对。

But it would appear that ISerializable isn't implemented/support (i can see why) for protobuf.net, so we have got around this by taking the 'ShouldSerialize*' and 'OnSerializing' approaches. This does however mean that we end up having to store the StreamingContext in each class which doesn't feel right. We could potentially stick it in a global but this also doesn't feel right.

是否有更好的方法来实现我们想要的目标,例如

Is there a better way to achieve what we want, e.g. serialization only using protobuf.net format but with what is serialized being influenced by an externally provided context?

推荐答案

仅使用protobuf.net格式进行序列化,而序列化的内容受外部提供的上下文影响吗? 。当前支持的模式(ShouldSerialize *等)完全从BCL按原样借用,因此没有上下文-但是,没有理由不能像回调那样支持参数-实际上,对于它支持的回调几乎任何用法(有/无上下文等)-因此,我想不出一个很好的理由不支持它们。

It is a good question. The patterns currently supported (ShouldSerialize* etc) are borrowed "as is" entirely from the BCL, hence no context - however there is no reason it can't support parameters in the same way that the callbacks do - indeed, for callbacks it supports pretty much any usage (with/without context etc) - so I can't think of a good reason not to support them here to.

您说对了目前尚不支持,但可能是-让我知道这会很有用。

You are right t say it isn't supported currently, but it could be - let me know of this would be useful.

这篇关于protobuf.net&条件序列化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆