Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hash::Ordered versus Tie::IxHash with JSON::XS encode

Tags:

json

hash

perl

I'm trying Hash::Ordered instead of Tie::IxHash, because it seems to be faster.

While Tie::IxHash is working fine, I struggle with some problems with Hash::Ordered. The point is to have the hashes ordered (which are usually random in Perl).

use Hash::Ordered;
use JSON::XS;
use Data::Dumper;

use strict;
use warnings;

my $json = JSON::XS->new;

my $oh = Hash::Ordered->new;
$oh->push('result' => { 'counter' => "123" }, 'number' => { 'num' => '55' });

my @r = $oh->as_list;

$json->pretty(1);
my $jsondata = $json->encode(\@r);
print Dumper $jsondata;

The result is odd:

 [
   "result",
   {
      "counter" : "123"
   },
   "number",
   {
      "num" : "55"
   }
]

Here is the working example with Tie::IxHash, I try to get the same results with Hash::Ordered.

use Data::Dumper;
use Tie::IxHash;
use JSON::XS;

use strict;
use warnings;

my $json = JSON::XS->new;

my %h;
tie(%h, 'Tie::IxHash', result => { counter => "123" }, number => { num => '55' });

  $json->pretty(1);
  my $pretty_json = $json->encode(\%h);

  print Dumper $pretty_json;

Output

{
   "result" : {
      "counter" : "123"
   },
   "number" : {
      "num" : "55"
   }
}
like image 625
user3606329 Avatar asked Jun 25 '16 12:06

user3606329


2 Answers

The object-oriented interface of Hash::Ordered is much faster that the tied interface, but some utilities (like $json->encode) require a real hash reference

The way to get the best of both worlds is to tie a hash for use with those utilities, and use tied to extract the underlying Hash::Ordered object so that you can use the faster method calls to manipulate it

This short program demonstrates. The only slow part of this code is when the hash is passed to encode to be translated to JSON. The push call doesn't use the tied interface and remains fast

use strict;
use warnings;

use Hash::Ordered;
use JSON::XS;

my $json = JSON::XS->new->pretty;

tie my %h, 'Hash::Ordered';
my $oh = tied %h;
$oh->push( result => { counter => 123 }, number => { num => 55 } );

print $json->encode(\%h), "\n";

output

{
   "result" : {
      "counter" : 123
   },
   "number" : {
      "num" : 55
   }
}
like image 131
Borodin Avatar answered Oct 19 '22 20:10

Borodin


Use the Hash::Ordered tied interface:

my $json = JSON::XS->new;

tie my %hash, "Hash::Ordered";

$hash{'result'} = { 'counter' => "123" };
$hash{'number1'} = { 'num' => '1' };
$hash{'number2'} = { 'num' => '2' };
$hash{'number3'} = { 'num' => '3' };
$hash{'last'} = { 'num' => 'last' };

$json->pretty(1);
my $jsondata = $json->encode(\%hash);

And the JSON data you get is:

{
   "result" : {
      "counter" : "123"
   },
   "number1" : {
      "num" : "1"
   },
   "number2" : {
      "num" : "2"
   },
   "number3" : {
      "num" : "3"
   },
   "last" : {
      "num" : "last"
   }
}
like image 3
Miguel Prz Avatar answered Oct 19 '22 21:10

Miguel Prz