Rails 2.3.11 中的 OrderedHash YAML 序列化损坏?

发布于 2024-11-27 07:00:40 字数 827 浏览 1 评论 0原文

我有复杂的数据结构,如下所示:OrderedHash,键是日期,值是有序哈希,其中包含带有字符串键的整数。我需要序列化它们才能存储在数据库中。但在 to_yaml/YAML.load 后,数据有时会被破坏——一些二级哈希值会被一级哈希值替换,反之亦然。但有时它并没有被破坏。

yaml 表示看起来像这样

- 2011-07-10: !omap 
    - 00:00-01:00: 0
    - 01:00-02:00: 0
    - 02:00-03:00: 0
    - 03:00-04:00: 0
    - 04:00-05:00: 0
    - 05:00-06:00: 0
    - 06:00-07:00: 0
    - 07:00-08:00: 0
    - *id010
    - 09:00-10:00: 0
    - 10:00-11:00: 0
    - 11:00-12:00: 0
    - 12:00-13:00: 0
    - 13:00-14:00: 0
    - 14:00-15:00: 0
    - 15:00-16:00: 0
    - 16:00-17:00: 0
    - 17:00-18:00: 0
    - 18:00-19:00: 0
    - 19:00-20:00: 0
    - 20:00-21:00: 0
    - 21:00-22:00: 0
    - 22:00-23:00: 0
    - 23:00-23:59: 0

*id010/&id010 部分被随机插入到 yaml 代码的不同位置。我认为这是错误的原因。

有人知道 yaml 序列化有什么问题吗? ruby 1.8.6,升级到 1.9 不是一个选项:(

I have complex data structure, like this: OrderedHash, keys are dates and values are ordered hashes which hold integers with string keys. I need to serialize those in order to store in db. But after to_yaml/YAML.load data is sometimes broken — some of second-level hashes are replaced with first-level or vice-versa. Sometimes it is not broken, though.

yaml representation looks like this

- 2011-07-10: !omap 
    - 00:00-01:00: 0
    - 01:00-02:00: 0
    - 02:00-03:00: 0
    - 03:00-04:00: 0
    - 04:00-05:00: 0
    - 05:00-06:00: 0
    - 06:00-07:00: 0
    - 07:00-08:00: 0
    - *id010
    - 09:00-10:00: 0
    - 10:00-11:00: 0
    - 11:00-12:00: 0
    - 12:00-13:00: 0
    - 13:00-14:00: 0
    - 14:00-15:00: 0
    - 15:00-16:00: 0
    - 16:00-17:00: 0
    - 17:00-18:00: 0
    - 18:00-19:00: 0
    - 19:00-20:00: 0
    - 20:00-21:00: 0
    - 21:00-22:00: 0
    - 22:00-23:00: 0
    - 23:00-23:59: 0

This *id010/&id010 part gets randomly inserted in different places of yaml code. I think it's the cause of error.

Does anybody have the idea of what is wrong with yaml serialization?
ruby 1.8.6, upgrade to 1.9 isn't an option :(

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

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

发布评论

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

评论(2

提笔书几行 2024-12-04 07:00:40

很有可能是有bug。我相信 Ruby 1.8 使用的 YAML 引擎叫做 Syck,这个代码是 _why 多年前创建的。从那时起,该代码就没有得到适当的维护。

Ruby 1.9 应该使用一个名为 Psych 的新引擎,但我不确定它与 Ruby 1.8 的兼容性如何。

在 Github 上我还发现了另一种替代方案,看起来可能值得您尝试一下:
https://github.com/cesare/ruby-libc-libyaml

Syck (您可以尝试这个版本,因为它看起来像是半维护的):
https://github.com/indeyets/syck

Psych (您也可以尝试看看是否可以在 1.8 上运行):
https://github.com/tenderlove/psych


编辑

也许 JSON 可以是您也可以选择吗?查看 to_json 方法,看看它是否可以用于您的目的,也许可以通过这种方式规避 YAML 问题。

It's very possible there is a bug. I believe the YAML engine Ruby 1.8 uses is called Syck, and this code was created by _why many years ago. The code has not been properly maintained since then.

Ruby 1.9 is supposed to use a new engine called Psych, but I am not sure how compatible this is with Ruby 1.8.

On Github I also found another alternative, which looks like it might be worth a try for you:
https://github.com/cesare/ruby-libc-libyaml

Syck (you might try this version, since it looks like it's being semi-maintained):
https://github.com/indeyets/syck

Psych (you could also try and see if this runs on 1.8):
https://github.com/tenderlove/psych


EDIT

Perhaps JSON could be an alternative for you also? Take a look at the to_json method and see if that could be used for your purposes, and perhaps circumvent the YAML problems this way.

深居我梦 2024-12-04 07:00:40

我的看法是 id010 在那里是因为你可能有某种递归/自引用结构,这就是处理它的方法。

My take is that the id010 is there because you might have some kind of recursion/self-referencing structure, and that's the way to deal with it.

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