ingydotnet / yaml-libyaml-pm

Perl Binding to libyaml
http://search.cpan.org/dist/YAML-LibYAML/
33 stars 37 forks source link

Inconsistency between YAML and YAML::XS in Dump() results #9

Closed SanyaZZZ closed 6 years ago

SanyaZZZ commented 10 years ago
$ /opt/drw/bin/perl/perl -MYAML -e 'print Dump( { asd => [ { qwe => "rty", zxc => "vbn" } ] } );'

---
asd:
  - qwe: rty
    zxc: vbn

$ /opt/drw/bin/perl/perl -MYAML::XS -e 'print Dump( { asd => [ { qwe => "rty", zxc => "vbn" } ] } );'

---
asd:
- qwe: rty
  zxc: vbn

There is no indent for an array in YAML::XS results, and this dump can't be loaded by YAML::Load(). Versions used: YAML v0.97, YAML::XS v0.52, perl v5.16.3 and 5.18.2

preaction commented 9 years ago

I just ran into this trying to make a thing that can use either module. Is this a problem with YAML::XS::Dump or with YAML::Load?

SanyaZZZ commented 9 years ago
$ perl -MYAML -e 'print Dump( { "a" => [ "b" ] } )' | perl -MYAML::XS -e 'local $/; my $x = <STDIN>; my $y = Load( $x ); print $y->{a}->[0],"\n";'
b
$ perl -MYAML::XS -e 'print Dump( { "a" => [ "b" ] } )' | perl -MYAML -e 'local $/; my $x = <STDIN>; my $y = Load( $x ); print $y->{a}->[0],"\n";'
YAML Error: Invalid element in map
   Code: YAML_LOAD_ERR_BAD_MAP_ELEMENT
   Line: 3
   Document: 1
 at /usr/local/share/perl/5.10.1/YAML/Loader.pm line 352

The problem is, the output produced by YAML::XS::Dump() can't be parsed by YAML::Load() because of no indention for the inner array. I think YAML::XS::Dump() needs to be fixed.

ingydotnet commented 9 years ago

No. The problem is that YAML::Load is broken.

foo:
- bar

Is valid YAML, and is what almost every modern YAML dumper will (and should) produce.

When you have a block sequence within a mapping, the '- ' counts as indentation.

YAML.pm was made way before the YAML spec was finalized. It should probably not be used in production. There are plans to rewrite it, but fixing this bug is probably not going to happen until then.

rurban commented 8 years ago

Ingy: I'm not sure if you are right here. The specs say: "Each node must be indented further than its parent node" http://yaml.org/spec/1.2/spec.html#6.1

The samples in the spec also use this indentation for seq elements. I would rather blame libyaml dumper, because it generates unparsable YAML. e.g. by the Perl PP YAML parser. But note that all other perl YAML readers can handle the unindented seq.

Should I fix libyaml dumper or YAML Loader?

rurban commented 8 years ago

Fixed with the option IndentlessMap in https://github.com/ingydotnet/yaml-libyaml-pm/pull/43

perlpunk commented 6 years ago

@rurban Please see

Both YAML 1.1 and 1.2 allow zero indented sequences. Like @ingydotnet said, YAML.pm is broken here. Pull requests for YAML.pm welcome.

In general, allowing to configure indendation and other stuff in YAML::XS would be desirable, but I don't like an IndentlessMap option.

The samples in the spec also use this indentation for seq elements.

Yes, examples use zero indented sequences. So?

Closing...

perlpunk commented 6 years ago

Released YAML.pm 1.25_001 which supports zero indented block sequences