[pm-h] JSON vs JSON::PP vs JSON:XS between EL5 and EL6

Reini Urban rurban at x-ray.at
Fri Apr 17 08:01:32 PDT 2015


2015-04-14 5:51 GMT+02:00 Michael R. Davis via Houston <houston at pm.org>:
> perl -e '
> use strict;
> use warnings;
> use Data::Dumper qw{Dumper};
> use Hash::Ordered qw{};
> *Hash::Ordered::TO_JSON_WITH_TYPE=sub {HASH=>shift->as_list};
> use Cpanel::JSON::XS qw{};
> my $oh=Hash::Ordered->new(a => 1, b=>2, c=>3, d=>4, e=>5);
> print Dumper($oh);
> print join(",", $oh->TO_JSON_WITH_TYPE), "\n";
> my $coder = Cpanel::JSON::XS->new->allow_blessed->convert_blessed;
> print $coder->encode({hash=>$oh}), "\n";
> '

Hm,

adding sort_by support seems to better, because JSON::PP provides that
already, and we should inherit from that.

using the as_list method as TO_JSON hook is possible already, maybe
something like this:

use Hash::Ordered qw{};
sub Hash::Ordered::TO_JSON { [ shift->as_list ] };
use Cpanel::JSON::XS qw{};
my $oh=Hash::Ordered->new(a => 1, b=>2, c=>3, d=>4, e=>5);
print Dumper($oh);
print join(",", @{$oh->TO_JSON}), "\n";
my $coder = Cpanel::JSON::XS->new->allow_blessed->convert_blessed;
print $coder->encode({hash=>$oh}), "\n";
-- 
Reini Urban
http://cpanel.net/   http://www.perl-compiler.org/


More information about the Houston mailing list