mirror of
https://github.com/percona/percona-toolkit.git
synced 2026-02-28 02:07:54 +08:00
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:
@@ -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 {
|
||||||
|
|||||||
@@ -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 {
|
||||||
|
|||||||
Reference in New Issue
Block a user