有条件反序列化

发布于 2024-08-25 21:21:06 字数 458 浏览 8 评论 0原文

我仍然不确定我的问题的标题是否正确,而且很可能不正确。然而,我花了一些时间在网络和 stackoverflow 上搜索,但找不到我所面临的问题的良好描述。

基本上我想要实现的是读取一些原始字节并根据其中一些字节的值以不同方式解释其余字节的能力。这就是 TLV 的工作方式,您检查标签并取决于it - 解释结果。当然,我始终可以将该逻辑保留在我的 C++ 代码中,但是我正在寻找一种解决方案,可以将逻辑移出源代码(可能移至某些 xml 描述)。这将使我能够更轻松地描述不同的编码(协议)。我熟悉协议缓冲区和其他一些序列化库,但是它们都解决了不同的问题。他们假设他们位于通信的两端,而我想描述通信(某种程度上)。

这样的解决方案是否可用,如果没有 - 为什么不呢?我在实施它时会遇到一些固有的困难吗?

I am still not sure whether the title of my question is correct and it most probably is not. However I have spent some time searching both the net and stackoverflow and I can not find a good description of the issue I am facing.

Basically what I want to achieve is the ability to read some raw bytes and based on the value of some of them to interpret the rest in different ways. This is how TLV works in a way, you check the tag and depending on it - interpret the result. Of course I can always keep that logic in my C++ code, however I am looking for a solution which would move the logic out of the source code (maybe to some xml description). This would allow me to describe different encodings (protocols) more easily. I am familiar with Protocol Buffers and some other serialization libraries, however all of them solve different issue. They assume they are on both ends of the communication, while I want to describe the communication (sort of).

Is such solution available, if no - why not? Are there some inherent difficulties I will face trying to implement it.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

深爱不及久伴 2024-09-01 21:21:06

我相信这是一项简单的任务,工作量很大,但是很容易。

两个重要概念:1) 工厂和 2) 对象控制反序列化。工厂将根据 ID 或标签创建对象实例。一旦创建了对象,该对象就负责从数据流(或缓冲区)加载其成员。

更简单的设计可能是实现嵌套工厂。工厂根据ID调用子工厂。这可以沿着链继续下去,直到调用最终的静态创建方法来创建适当的对象实例。

最终对象将被传递到流(或缓冲区),并且它将加载其数据成员。让对象加载其成员(而不是外部对象或函数)的原因是该对象知道其成员的类型和布局。此外,所有内容都在一个位置,因此当添加或删除数据成员时,只有对象发生变化,而其他外部函数(访问对象数据成员)不会发生变化。

希望这有帮助。

I believe this is an easy task, a lot of work, but easy.

Two important concepts: 1) Factories and 2) Object controls deserialization. The factories will create an object instance based on an ID or tag. Once the object is created, the object is responsible for loading its members from a data stream (or buffer).

A simpler design may be to implement nested factories. A factory calls a sub-factory based on an ID. This may continue down a chain until a final static creation method is called to create the appropriate object instance.

The final object would be passed the stream (or buffer) and it would load its data members. The reason for having the object load its members (rather than an external object or function) is that the object knows the types and layouts of its members. Also, one location for everything, so that when an data member is added or removed, only the object changes, not other external functions (that access the objects data members).

Hope this helps.

梦情居士 2024-09-01 21:21:06

我不知道有这样的事。

至于为什么没有任何东西,可能归结为没有人真正需要它。通信协议不太可能发生任何重大变化,那么为什么要为对不会发生的变化做出反应的能力付费呢?

I know of nothing like this.

As to why there isn't anything it probably comes down to nobody really needs it. A communications protocol is unlikely to change in any significant way so why pay for the ability to react to changes that won't occur?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文