DXM DXM - 2 months ago 8
Ruby Question

Cannot parse "%YAML:1.0" in Ruby

I'm using Ruby 1.9.3, and I have a YAML file generated by another program that I need to parse. It seems the Ruby YAML parser cannot recognize

%YAML:1.0
, which is just an indicator of YAML version.

This is my YAML file:

%YAML:1.0
task_name: "1000022_es-2013-03-19-12-00-00_1_90000_s1.vtsk"
worker_name: "1000022_es-2013-03-19-12-00-00_1_90000_s1.vtsk"
left_labels: !!opencv-matrix
rows: 8
cols: 1
dt: f
data: [ 8., 6., 2., 7., 8., 4., 10., 4. ]
right_labels: !!opencv-matrix
rows: 8
cols: 1
dt: f
data: [ 14., 6., 12., 7., 7., 4., 5., 1. ]
left_features: !!opencv-matrix
rows: 8
cols: 2
dt: f
data: [ 4692611., 12., 2.81733875e+06, 12., 2.99522725e+06, 12.,
4.00128050e+06, 12., 3.84592175e+06, 12., 2006966., 12.,
4.47367050e+06, 12., 9.56887875e+05, 0. ]
right_features: !!opencv-matrix
rows: 8
cols: 2
dt: f
data: [ 4692611., 12., 2.81733875e+06, 12., 2.99522725e+06, 12.,
4.00128050e+06, 12., 3.84592175e+06, 12., 2006966., 12.,
4.47367050e+06, 12., 9.56887875e+05, 0. ]


And when I do:

require 'yaml'
Psych.load(File.read('myfile.yaml'))


I get this error:


Psych::SyntaxError: (): couldn't parse YAML at line 0 column 5


If I remove
%YAML:1.0
from the file, then everything is fine.

Answer

It looks like the Ruby libraries do not properly handle the YAML 1.0 YAML directive. As far as I can tell, Psych 2.0.0 only handles YAML directives explicitly for version 1.1 (%YAML 1.1) ; it fails on %YAML:1.0 and %YAML 1.2. (Starting with version 1.1, the colon was dropped from the directive syntax.)

What this means to me is that Psych 2.0.0 is only compatible with YAML 1.1, but since the rest of your file appears to be valid YAML 1.X, removing the incompatible directive avoids the problem.

Your options appear to be:

  1. Get the other program to output YAML 1.1,
  2. Strip the directive from the top of the file ahead of time, or
  3. Strip the directive from the buffer prior to passing it to Psych for parsing.