Skip to content

Instantly share code, notes, and snippets.

@perlpunk
Last active December 14, 2017 00:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save perlpunk/cff476611612d7035c7ce275817ccc04 to your computer and use it in GitHub Desktop.
Save perlpunk/cff476611612d7035c7ce275817ccc04 to your computer and use it in GitHub Desktop.
#!/usr/bin/env perl
use strict;
use warnings;
use 5.010;
use JSON ();
use JSON::PP ();
use JSON::XS ();
use Cpanel::JSON::XS ();
use Mojo::JSON ();
use B ();
use Text::Table;
my @classes = qw/ JSON JSON::PP JSON::XS Cpanel::JSON::XS Mojo::JSON /;
my $t = Text::Table->new(
qw/
Class
Version
3=IV NV PV
3.14=IV NV PV
3.0=IV NV PV
0.3e3=IV NV PV
encode
/,
);
my $json = <<'EOM';
[ 3, 3.14, 3.0, 0.3e3 ]
EOM
my @rows;
for my $class (@classes) {
my $version = $class->VERSION;
my @row = ( $class, $version );
my $decode = $class->can("decode_json");
my $encode = $class->can("encode_json");
my $data = $decode->($json);
for my $num (@$data) {
my $flags = B::svref_2object(\$num)->FLAGS;
my $int = $flags & B::SVp_IOK ? 1 : 0;
my $float = $flags & B::SVp_NOK ? 1 : 0;
my $str = $flags & B::SVp_POK ? 1 : 0;
push @row, $int, $float, $str;
}
my $enc = $encode->($data);
push @row, $enc;
push @rows, \@row;
}
say "Input: $json";
$t->load(@rows);
say $t;
Input: [ 3, 3.14, 3.0, 0.3e3 ]
Class Version 3=IV NV PV 3.14=IV NV PV 3.0=IV NV PV 0.3e3=IV NV PV encode
JSON 2.94 1 0 0 0 1 0 0 1 0 0 1 0 [3,3.14,3,300]
JSON::PP 2.27300_01 1 0 0 0 1 0 0 1 0 1 0 0 [3,3.14,3,300]
JSON::XS 3.03 1 0 0 0 1 0 0 1 0 0 1 0 [3,3.14,3,300]
Cpanel::JSON::XS 3.0233 1 0 0 0 1 0 0 1 0 0 1 0 [3,3.14,3.0,300.0]
Mojo::JSON 1 0 0 0 1 0 0 1 0 1 0 0 [3,3.14,3,300]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment