X-Git-Url: https://git.dogcows.com/gitweb?a=blobdiff_plain;f=lib%2FApp%2FCodeowners%2FFormatter%2FJSON.pm;fp=lib%2FApp%2FCodeowners%2FFormatter%2FJSON.pm;h=3c7f8b8e2311832c212aa4b0e5846d628ed93c76;hb=26eed33eb4aa577d9347e5ebaf577b3e3a2c0396;hp=0000000000000000000000000000000000000000;hpb=f5bbfcbc345874483160d1cf8fb52871ab6a7d85;p=chaz%2Fgit-codeowners diff --git a/lib/App/Codeowners/Formatter/JSON.pm b/lib/App/Codeowners/Formatter/JSON.pm new file mode 100644 index 0000000..3c7f8b8 --- /dev/null +++ b/lib/App/Codeowners/Formatter/JSON.pm @@ -0,0 +1,41 @@ +package App::Codeowners::Formatter::JSON; +# ABSTRACT: Format codeowners output as JSON + +=head1 DESCRIPTION + +This is a L that formats output using L. + +=cut + +use warnings; +use strict; + +our $VERSION = '9999.999'; # VERSION + +use parent 'App::Codeowners::Formatter'; + +use App::Codeowners::Util qw(zip); + +=attr format + +If unset (default), the output will be compact. If "pretty", the output will look nicer to humans. + +=cut + +sub finish { + my $self = shift; + my $results = shift; + + eval { require JSON::MaybeXS } or die "Missing dependency: JSON::MaybeXS\n"; + + my %options; + $options{pretty} = 1 if lc($self->format) eq 'pretty'; + + my $json = JSON::MaybeXS->new(canonical => 1, utf8 => 1, %options); + + my $columns = $self->columns; + $results = [map { +{zip @$columns, @$_} } @$results]; + print { $self->handle } $json->encode($results); +} + +1;