perlpunk / YAML-PP-p5

A YAML 1.2 processor in perl
https://metacpan.org/pod/YAML::PP
24 stars 8 forks source link

quote special YAML keywords when dumping #38

Closed maxatome closed 4 years ago

maxatome commented 4 years ago

Hello,

perl -Ilib -MYAML::PP -MData::Dumper -E '
  my $x = YAML::PP::->new->load_string(qq(---\ntest:\n  - "no"\n  - "3x"\n  - "true"));
  print Dumper($x);
  print YAML::PP::->new->dump_string($x)'

"no" and "3x" should be quoted in resulting YAML as "true" is. Perhaps am I missing something?

perlpunk commented 4 years ago

YAML::PP is using the YAML 1.2 Core schema as a default. For a comparison: https://perlpunk.github.io/YAML-PP-p5/schemas.html In YAML 1.1, no was resolved as a boolean, but not anymore in YAML 1.2. You can use the 1.1 schema explicitly: https://metacpan.org/pod/YAML::PP::Schema::YAML1_1

But 3x doesn't need quotes in any of the schemas.

Hope that helps :)

perlpunk commented 4 years ago

Btw, if you simply want to preserve the quoting style from loading, you can use:

YAML::PP->new( preserve => PRESERVE_SCALAR_STYLE );

See the preserve option here: https://metacpan.org/pod/YAML::PP#new It's still experimental though and might not work correctly for all styles.

maxatome commented 4 years ago

Hi, Sorry for the delay. I missed the YAML 1.1 vs 1.2 differences. I have to check on decoder side (not YAML::PP one) why I encountered a problem with "3x". Sorry for the noise @perlpunk and thanks!