Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
SanyaZZZ opened this issue Sep 25, 2014 · 7 comments
Closed

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

SanyaZZZ opened this issue Sep 25, 2014 · 7 comments

Comments

@SanyaZZZ
Copy link

$ /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
Copy link

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
Copy link
Author

$ 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
Copy link
Owner

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
Copy link

rurban commented Mar 2, 2016

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 pushed a commit to rurban/yaml-pm that referenced this issue Mar 2, 2016
YAML::XS generates unintended seq elements. See
ingydotnet/yaml-libyaml-pm#9

Ingy claims that it is valid YAML. So test for it.
@rurban
Copy link

rurban commented Mar 10, 2016

Fixed with the option IndentlessMap in #43

@perlpunk
Copy link
Collaborator

@rurban
Please see

[201] seq-spaces(n,c) ::= c = block-out ⇒ n-1
                          c = block-in  ⇒ n
[197] seq-spaces(n,c) ::= c = block-out ⇒ n-1
                          c = block-in  ⇒ n

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
Copy link
Collaborator

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants