Updated JSONReportFormatter to drop the hard dependency on JSON.xs and use Transformers::encode_json if JSON is not available

This commit is contained in:
Brian Fraser
2013-01-30 17:56:19 -03:00
parent 98f490492c
commit 5fd8042e35
2 changed files with 34 additions and 10 deletions

View File

@@ -7977,14 +7977,14 @@ sub _d {
{ {
package JSONReportFormatter; package JSONReportFormatter;
use Mo; use Mo;
use JSON ();
use List::Util qw(sum);
use List::Util qw(sum);
use Transformers qw(make_checksum parse_timestamp); use Transformers qw(make_checksum parse_timestamp);
use constant PTDEBUG => $ENV{PTDEBUG} || 0; use constant PTDEBUG => $ENV{PTDEBUG} || 0;
my $have_json = eval { require JSON };
our $pretty_json = undef; our $pretty_json = undef;
our $sorted_json = undef; our $sorted_json = undef;
@@ -7994,11 +7994,23 @@ has _json => (
is => 'ro', is => 'ro',
init_arg => undef, init_arg => undef,
builder => '_build_json', builder => '_build_json',
handles => { encode_json => 'encode' },
); );
sub _build_json { sub _build_json {
return JSON->new->utf8->pretty($pretty_json)->canonical($sorted_json); return unless $have_json;
return JSON->new->utf8
->pretty($pretty_json)
->canonical($sorted_json);
}
sub encode_json {
my ($self, $encode) = @_;
if ( my $json = $self->_json ) {
return $json->encode($encode);
}
else {
return Transformers::encode_json($encode);
}
} }
override [qw(rusage date hostname files header profile prepared)] => sub { override [qw(rusage date hostname files header profile prepared)] => sub {

View File

@@ -1,14 +1,14 @@
{ {
package JSONReportFormatter; package JSONReportFormatter;
use Mo; use Mo;
use JSON ();
use List::Util qw(sum);
use List::Util qw(sum);
use Transformers qw(make_checksum parse_timestamp); use Transformers qw(make_checksum parse_timestamp);
use constant PTDEBUG => $ENV{PTDEBUG} || 0; use constant PTDEBUG => $ENV{PTDEBUG} || 0;
my $have_json = eval { require JSON };
our $pretty_json = undef; our $pretty_json = undef;
our $sorted_json = undef; our $sorted_json = undef;
@@ -18,11 +18,23 @@ has _json => (
is => 'ro', is => 'ro',
init_arg => undef, init_arg => undef,
builder => '_build_json', builder => '_build_json',
handles => { encode_json => 'encode' },
); );
sub _build_json { sub _build_json {
return JSON->new->utf8->pretty($pretty_json)->canonical($sorted_json); return unless $have_json;
return JSON->new->utf8
->pretty($pretty_json)
->canonical($sorted_json);
}
sub encode_json {
my ($self, $encode) = @_;
if ( my $json = $self->_json ) {
return $json->encode($encode);
}
else {
return Transformers::encode_json($encode);
}
} }
override [qw(rusage date hostname files header profile prepared)] => sub { override [qw(rusage date hostname files header profile prepared)] => sub {