【问题标题】:Why the difference in behavior of YAML parsers (syck and psych)?为什么 YAML 解析器(syck 和 psych)的行为不同?
【发布时间】:2012-02-04 12:24:08
【问题描述】:

看这个案例:

ruby 1.9.2p0(2010-08-18 修订版 29036)[i686-linux]

irb(main):006:0> YAML::ENGINE.yamler = "psych"
=> "psych"

irb(main):007:0> '8902-20-13'.to_yaml
ArgumentError: invalid date
from /usr/local/lib/ruby/1.9.1/date.rb:1022:in `new_by_frags'
from /usr/local/lib/ruby/1.9.1/date.rb:1046:in `strptime'
from /usr/local/lib/ruby/1.9.1/psych/scalar_scanner.rb:45:in `tokenize'
from /usr/local/lib/ruby/1.9.1/psych/visitors/yaml_tree.rb:191:in `visit_String'
from /usr/local/lib/ruby/1.9.1/psych/visitors/yaml_tree.rb:63:in `accept'
from /usr/local/lib/ruby/1.9.1/psych/visitors/yaml_tree.rb:36:in `<<'
from /usr/local/lib/ruby/1.9.1/psych.rb:165:in `dump'
from /usr/local/lib/ruby/1.9.1/psych/core_ext.rb:13:in `psych_to_yaml'

irb(main):008:0> YAML::ENGINE.yamler = "syck"
=> "syck"

irb(main):009:0> '8902-20-13'.to_yaml
=> "--- \"8902-20-13\"\n"

当我使用 psych 解析器并且我需要格式化一个模糊地类似于日期的字符串时,它会引发异常,因为他认为这是一个日期字符串。使用syck不会出现这个问题。

有人知道吗?

【问题讨论】:

标签: ruby yaml syck psych


【解决方案1】:

Psych 几乎独立于 Syck,我不认为 Psych 的作者有兴趣与 Syck 成为bug-compatible。我认为他们无论如何都实现了不同版本的 YAML 规范。

一种解决方法应该是在类似日期的数字周围放置字符串。

【讨论】:

    【解决方案2】:

    正如@matt 所说,答案是这个拉取请求:https://github.com/ruby/ruby/commit/9f688d53c2b5af5960d1e8d8fb09b26aa9d8b5f9

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-02-03
      • 2023-03-18
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多