1

我有两个应用程序使用队列相互通信,到目前为止它们运行完全相同版本的 ruby​​ (1.8.7),所以我只是来回编组对象;只有标准库中的对象主要是哈希、字符串、时间和日期对象。

现在我正在迁移到 Ruby 1.9.1,当时只有一个应用程序,这意味着我将使用 1.8.7 运行一个应用程序,而另一个使用 1.9.1 运行一段时间。通过运行我的测试,我知道 Marshal 跨版本不可靠,我可以使用 YAML,但它要慢得多,JSON 似乎更快,但它不直接处理日期/时间对象。

是否有一种可靠且快速的方法可以跨不同版本序列化 ruby​​ 对象?

4

3 回答 3

0

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.

于 2010-02-19T00:19:24.550 回答
0

Protobuf 很好,但如果我记得的话,它需要你预先定义你的数据结构。Thrift 类似于 protobufs,但具有一些不错的代码生成功能。

Apple 的二进制属性列表格式听起来很接近您的需要。它在行为上类似于 JSON,但更紧凑并支持一些额外的类型,包括日期时间和未编码的二进制文件。github 上有几个 ruby​​ 实现。

你最好的选择可能是BERT。BERT 基于 Erlang 的二进制术语序列化格式。它很紧凑,包括数据时间序列化,并以十几种语言实现,包括 ruby​​。

于 2010-06-15T09:33:16.693 回答
0

我还没有在 ruby​​ 上尝试过,但你可以看看协议缓冲区?设计为一种快速但可移植的二进制格式,它在这里有一个 ruby​​ 端口。不过,您可能不得不将生成的类型视为单独的 DTO 层(即,您将现有数据映射到新类型,而不是序列化现有对象)。请注意,没有内置的日期时间支持,但您可以只在一个纪元中使用刻度等。

于 2010-02-18T20:39:31.933 回答