ingydotnet / yaml-pm

YAML Perl Module
http://search.cpan.org/dist/YAML/
20 stars 27 forks source link

optionally preserve hash order when loading [rt.cpan.org #83788] #132

Closed ingydotnet closed 7 years ago

ingydotnet commented 10 years ago

https://rt.cpan.org/Ticket/Display.html?id=83788

The current implementation of YAML does not allow to preserve the hash
order when loading a YAML document. I know, this is not really in the
spec, but:
- YAML documents come with an implicit order of the keys (as they appear
in the document)
- YAML::Node supports to keep the hash order via a tied hash

In fact, with the Loader changed on one line (around ~326):

-my $mapping = {};
+require YAML::Node;
+my $mapping = YAML::Node->new({});

... this works immediately! I am wondering whether this could not be
provided as a global option e.g. when $YAML::SortKeys is set to 0.

What do you think?

Many thanks,

Marek
ingydotnet commented 10 years ago

ingy@ingy.net - 2013-03-08 03:17:39

Hi Marek,

I think it's useful, but I think it should have its own config option.

Maybe $YAML::KeyOrder=1 or $YAML::LoadNode

Would you be interested in forking this on github, patching it with tests, and submitting a pull request?

Cheers, Ingy

On Wed, Mar 6, 2013 at 7:48 AM, Marek Rouchal via RT bug-YAML@rt.cpan.orgwrote:

Wed Mar 06 10:48:49 2013: Request 83788 was acted upon. Transaction: Ticket created by MAREKR Queue: YAML Subject: optionally preserve hash order when loading Broken in: 0.84 Severity: Normal Owner: Nobody Requestors: Marek.Rouchal@gmx.net Status: new Ticket <URL: https://rt.cpan.org/Ticket/Display.html?id=83788 >

The current implementation of YAML does not allow to preserve the hash order when loading a YAML document. I know, this is not really in the spec, but:

  • YAML documents come with an implicit order of the keys (as they appear in the document)
  • YAML::Node supports to keep the hash order via a tied hash

In fact, with the Loader changed on one line (around ~326):

-my $mapping = {}; +require YAML::Node; +my $mapping = YAML::Node->new({});

... this works immediately! I am wondering whether this could not be provided as a global option e.g. when $YAML::SortKeys is set to 0.

What do you think?

Many thanks,

Marek

sergeyromanov commented 9 years ago

It looks like this has been addressed by #9; tests are pending, though.

perlpunk commented 7 years ago

Released as $YAML::Preserve in 1.20