Stories
Slash Boxes
Comments
NOTE: use Perl; is on undef hiatus. You can read content, but you can't post it. More info will be forthcoming forthcomingly.

All the Perl that's Practical to Extract and Report

use Perl Log In

Log In

[ Create a new account ]

Journal of jjore (6662)

Saturday January 24, 2009
06:09 AM

Dweomer in YAML::Syck and Ruby

[ #38335 ]

YAML has a thing called “implicit typing” where unquoted values are translated into language-specific entities. What you expect is what when you write the number 1, you might get an integer back. You do. What you might not expect is that when you type the word yes, you get a boolean back and not the word.

We are using YAML 1.0 but the documentation for this has fallen off the internet. It can still be retrieved from the internet archive at http://web.archive.org/web/20041204101603/http://yaml.org/spec/type.html. YAML 1.1+’s type system is still expansive but it is also still linked from the yaml spec at http://yaml.org/type/.

The following YAML document does all kinds of strange things depending on whether Ruby or Perl is reading and writing it. In general, Ruby’s default library is more magical and requires hand-holding to cope with whatever it did. To force “ordinary” interpretations, quote everything. Be liberal with it. Quote more than you think a person should ever reasonably be expected to quote.

The following YAML document is 99% magic. The only non-magical things are the strings bool, float, int, and date. Everything else is potentially transmogrified. Everything else is potentially transformed by YAML’s implicit typing.

---
bool:
  true: [ y, Y, yes, Yes, YES, true, True, TRUE, on, On, ON ]
  false: [ n, N, no, No, NO, false, False, FALSE, off, Off, OFF ]
float: [ .inf, +.inf, -.inf, .nan ]
int: [ 0b10, 010, 0xf, 19:59 ]
null: [ ~, null ]
date: [ 2008-10, 2008-10-02T21:21:42Z-08 ]

The canonical representation for booleans in yaml are the unquoted literals y and n but yaml.rb treats them like strings while converting everything else. Perl’s YAML::Syck defaults to treating things purely as strings unless it is asked to apply implicit typing and then it acts like Ruby. Notice the odd base-60 integer conversion from 19:59 -> 1199. Notice that all integer conversions are valid except binary which as inexplicably left off.

Ruby’s yaml.rb

y                                              -> 'y'
Y                                              -> 'Y'
Yes, Yes, YES, true, True, TRUE, on, On, ON    -> true
n                                              -> 'n'
N                                              -> 'N'
no, No, NO, false, False, FALSE, off, Off, OFF -> false
19:59 -> 1199

Perl’s YAML::Syck, $YAML::Syck::ImplicitTyping = 1

y                                              -> 'y'
Y                                              -> 'Y'
Yes, Yes, YES, true, True, TRUE, on, On, ON    -> '1' / 1
n                                              -> 'n'
N                                              -> 'N'
no, No, NO, false, False, FALSE, off, Off, OFF -> '' / 0

Ruby's yaml.rb

.inf, +.inf  -> Infinity
-.inf        -> -Infinity
.nan         -> NaN
~, null      -> nil

Perl

.inf, +.inf  -> 'inf' / inf
-.inf        -> '-inf' / -inf
.nan         -> 'nan' / nan
~, null      -> undef

I separated inf, nan, and nil/undef out because their target language implementations are different while the meaning is identical. If you treat the perl strings ‘inf’, ‘-inf’, ‘+inf’, ‘nan’ like numbers, they are numified as the equivalent floating point value. Also, Ruby’s nil and Perl’s undef are really the same thing but they go by different names.

Thought I’d share. Remember, quote, quote, quote, when in doubt, quote. When in Ruby, always, always convert with to_i, to_s or whatever is appropriate for the data. There are some other issues related to undesired de-quoting by passing data through yaml.rb that make the actual type of a value unpredictable.

The Fine Print: The following comments are owned by whoever posted them. We are not responsible for them in any way.
 Full
 Abbreviated
 Hidden
More | Login | Reply
Loading... please wait.
  • YAML's implicit typing is still a pain, but 1.2 makes things a lot simpler by having far, far fewer implicit types, basically just enough to load JSON properly.

    Sexagesimal is Right Out.

    --
    rjbs