diff --git a/bin/pt-query-digest b/bin/pt-query-digest index 136703a0..8d485231 100755 --- a/bin/pt-query-digest +++ b/bin/pt-query-digest @@ -7977,14 +7977,14 @@ sub _d { { package JSONReportFormatter; use Mo; -use JSON (); - -use List::Util qw(sum); +use List::Util qw(sum); use Transformers qw(make_checksum parse_timestamp); use constant PTDEBUG => $ENV{PTDEBUG} || 0; +my $have_json = eval { require JSON }; + our $pretty_json = undef; our $sorted_json = undef; @@ -7994,11 +7994,23 @@ has _json => ( is => 'ro', init_arg => undef, builder => '_build_json', - handles => { encode_json => 'encode' }, ); 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 { diff --git a/lib/JSONReportFormatter.pm b/lib/JSONReportFormatter.pm index 764232a2..be0e534f 100644 --- a/lib/JSONReportFormatter.pm +++ b/lib/JSONReportFormatter.pm @@ -1,14 +1,14 @@ { package JSONReportFormatter; use Mo; -use JSON (); - -use List::Util qw(sum); +use List::Util qw(sum); use Transformers qw(make_checksum parse_timestamp); use constant PTDEBUG => $ENV{PTDEBUG} || 0; +my $have_json = eval { require JSON }; + our $pretty_json = undef; our $sorted_json = undef; @@ -18,11 +18,23 @@ has _json => ( is => 'ro', init_arg => undef, builder => '_build_json', - handles => { encode_json => 'encode' }, ); 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 {