开发者

broken OrderedHash YAML serialization in Rails 2.3.11?

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 :(


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜