in reply to Re^2: Outputting JSON with sorted names/keys
in thread Outputting JSON with sorted names/keys
You may find a wrapper like this helpful:
sub h { tie my %hash, 'Tie::Hash::MultiValueOrdered'; while (my ($k, $v) = splice @_, 0, 2) { $hash{$k} = $v; } \%hash; }
That way you can build your data structure pretty naturally, just using h(...) for a hashref instead of {...}:
my $people = [ h( name => "Alice", age => 21 ), h( name => "Bob", name => "Robert", age => 22 ), ]; print JSON::MultiValueOrdered->new(pretty=>1)->encode($people);
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^4: Outputting JSON with sorted names/keys
by pryrt (Abbot) on Jan 27, 2020 at 14:12 UTC |