BinaryFormatter 中的引用完整性
Protobuf-net 中 AsReference 选项的存在以及 BinaryFormatter 是“图形序列化器”这个词让我假设 BinaryFormatter 不维护引用,并且它会复制每个对象。
但我做了一些测试,发现即使对于递归引用,单个 BinaryFormatter Serialize() 或 Deserialize() 调用中的所有引用也会得到维护。
我可以确认 BinaryFormatter 确实维护引用吗?这与 Protobuf-net 有什么不同?看来我对“图序列化”的理解不正确?我还应该注意什么?
提前致谢。
The existence of AsReference option in Protobuf-net and the word that BinaryFormatter is a "graph serializer" lead me to assume that BinaryFormatter does not maintain references and that it makes a copy of every object.
But I did some tests and found out that all references in a single BinaryFormatter Serialize() or Deserialize() call are maintained even for recursive referencing.
Can I confirm that BinaryFormatter does indeed maintain references? How is this different from Protobuf-net? Seems like I understand "graph serialization" incorrectly? What else should I look out for?
Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
太长;博士;版本 -
BinaryFormatter
始终保留引用。BinaryFormatter
是一个“图形序列化器”,这一事实与“它保留引用”同义,因为这是准确再现图形所必需的。另一种选择是树序列化器(这是其中的大部分;XmlSerializer
、DataContractSerializer
(没有启用特定选项),JavascriptSerializer
和没有AsReference
的 protobuf-net 都是基于树的)。树序列化器通常不保留引用(除非它们使用一些技巧,即如果启用全图模式,DataContractSerializer
会做什么,或者protobuf-net使用AsReference会做什么)。树序列化器(没有启用巫毒)往往会在递归模型中爆炸成混乱,这使得它们很容易被发现。
tl;dr; version -
BinaryFormatter
always preserves references.The fact that
BinaryFormatter
is a "graph serializer" is synonymous with "it preserves references", since that is required to accurately reproduce a graph. The alternative is a tree serializer (which is most of them;XmlSerializer
,DataContractSerializer
(without a particular option enabled),JavascriptSerializer
and protobuf-net withoutAsReference
are all tree-based). Tree serializers do not generally preserve references (unless they work some tricks, i.e. whatDataContractSerializer
does if you enable full-graph mode, or protobuf-net does withAsReference
). Tree serializers (without voodoo enabled) tend to explode in a mess with a recursive model, which makes them easy to spot.