是否有一种快速可靠的方法可以跨不同版本的 Ruby 序列化对象?
我有两个应用程序使用队列相互通信,到目前为止,它们运行完全相同的 ruby 版本(1.8.7),所以我只是来回编组对象;仅标准库中的对象主要是哈希、字符串、时间和日期对象。
现在我正在转向 Ruby 1.9.1,当时只是一个应用程序,这意味着我将在一段时间内使用 1.8.7 运行一个应用程序,而使用 1.9.1 运行另一个应用程序一段时间。通过运行我的测试,我知道 Marshal 跨版本不可靠,我可以使用 YAML,但它慢得多,JSON 似乎更快,但它不直接处理日期/时间对象。
是否有一种可靠且快速的方法来跨不同版本序列化 ruby 对象?
I have two applications talking to each other using a queue, as of now they run exactly the same version of ruby (1.8.7), so I'm just marshaling objects back and forth; only objects from the standard lib mostly hashes, strings, time and date objects.
Right now I'm moving to Ruby 1.9.1, one app at the time, which means I'll be running one app with 1.8.7 and the other with 1.9.1 for a while. By running my tests I know Marshal will not be reliable across versions, I could use YAML, but it is much slower, JSON seems to be faster but it does not deal directly with the date/time objects.
Is there a reliable and fast way to serialize ruby objects across different versions?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我还没有在 ruby 上尝试过,但是你可以看看协议缓冲区吗?它被设计为一种快速但可移植的二进制格式,此处有一个 ruby 端口。不过,您可能必须将生成的类型视为单独的 DTO 层(即将现有数据映射到新类型,而不是序列化现有对象)。请注意,没有内置的日期时间支持,但您可以只使用纪元等中的刻度。
I haven't tried it on ruby, but you could look at protocol buffers? Designed as a fast but portable binary format, it has a ruby port here. You would probably have to treat the generated types as a separate DTO layer, though (i.e. you map your existing data into the new types, rather than serialize your existing objects). Note that there is no inbuilt date-time support, but you could just use ticks in an epoch etc.
这里的关键是找到一个通用的数据类型,您知道该数据类型将在 Ruby 版本中以相同的方式表示。这里显而易见的选择是将数据存储在外部数据库中(数据库接口库将处理所有转换)或以结构化文本格式写出数据。如果没有大量数据可供使用(并且数据大多是标准类型),我通常只是将其存储为文本;导出/导入需要更长的时间,但写入通常更快。
The key here is finding a common data type that you know will be represented the same across Ruby versions. The obvious choices here are storing data in an external database (the DB interface libraries will handle all the conversions) or writing the data out in a structured text format. If there's not a ton of data to work with (and the data is mostly standard types), I usually just store it as text; it takes longer to export/import but it's usually faster to write.
Protobuf 很好,但如果我没记错的话,需要您预先定义数据结构。 Thrift 与 protobuf 类似,但具有一些不错的代码生成功能。
Apple 的二进制属性列表格式听起来很接近您的需要。它在行为上类似于 JSON,但更紧凑并且支持一些额外的类型,包括日期时间和未编码的二进制。 github 上有几个 ruby 实现。
您最好的选择可能是 BERT。 BERT 基于 Erlang 的二进制术语序列化格式。它很紧凑,包括数据时间序列化,并用包括 ruby 在内的十几种语言实现。
Protobufs are good, but require you to pre-define your data structures, if I recall. Thrift is similar to protobufs, but has some decent code generation features.
Apple's binary property list format sounds close to what you need. It's similar to JSON in behavior, but is more compact and supports a few extra types, including datetime and unencoded binary. There are a couple ruby implementations on github.
Your best bet may be BERT. BERT is based on Erlang's binary term serialization format. It's compact, includes datatime serialization and is implemented in a dozen or so languages, including ruby.