| 1 |
package Test2::Harness::Util::File::JSON; |
| 2 |
use strict; |
| 3 |
use warnings; |
| 4 |
|
| 5 |
our $VERSION = '1.000179'; |
| 6 |
|
| 7 |
use Carp qw/croak confess/; |
| 8 |
use Test2::Harness::Util::JSON qw/encode_json decode_json encode_pretty_json/; |
| 9 |
|
| 10 |
use parent 'Test2::Harness::Util::File'; |
| 11 |
use Test2::Harness::Util::HashBase qw/pretty/; |
| 12 |
|
| 13 |
sub decode { shift; decode_json(@_) } |
| 14 |
sub encode { shift->pretty ? encode_pretty_json(@_) : encode_json(@_) } |
| 15 |
|
| 16 |
sub reset { croak "line reading is disabled for json files" } |
| 17 |
sub read_line { croak "line reading is disabled for json files" } |
| 18 |
|
| 19 |
sub maybe_read { |
| 20 |
my $self = shift; |
| 21 |
|
| 22 |
return undef unless -e $self->{+NAME}; |
| 23 |
my $out = Test2::Harness::Util::read_file($self->{+NAME}); |
| 24 |
|
| 25 |
return undef unless defined($out) && length($out); |
| 26 |
|
| 27 |
eval { $out = $self->decode($out); 1 } or confess "$self->{+NAME}: $@"; |
| 28 |
return $out; |
| 29 |
} |
| 30 |
|
| 31 |
1; |
| 32 |
|
| 33 |
__END__ |
| 34 |
|
| 35 |
=pod |
| 36 |
|
| 37 |
=encoding UTF-8 |
| 38 |
|
| 39 |
=head1 NAME |
| 40 |
|
| 41 |
Test2::Harness::Util::File::JSON - Utility class for a JSON file. |
| 42 |
|
| 43 |
=head1 DESCRIPTION |
| 44 |
|
| 45 |
Subclass of L<Test2::Harness::Util::File> which automatically handles |
| 46 |
encoding/decoding JSON data. |
| 47 |
|
| 48 |
=head1 SYNOPSIS |
| 49 |
|
| 50 |
require Test2::Harness::Util::File::JSON; |
| 51 |
my $file = Test2::Harness::Util::File::JSON->new(name => '/path/to/file.json'); |
| 52 |
|
| 53 |
$hash = $file->read; |
| 54 |
# or |
| 55 |
$$file->write({...}); |
| 56 |
|
| 57 |
=head1 SEE ALSO |
| 58 |
|
| 59 |
See the base class L<Test2::Harness::Util::File> for methods. |
| 60 |
|
| 61 |
=head1 SOURCE |
| 62 |
|
| 63 |
The source code repository for Test2-Harness can be found at |
| 64 |
F<http://github.com/Test-More/Test2-Harness/>. |
| 65 |
|
| 66 |
=head1 MAINTAINERS |
| 67 |
|
| 68 |
=over 4 |
| 69 |
|
| 70 |
=item Chad Granum E<lt>[email protected]<gt> |
| 71 |
|
| 72 |
=back |
| 73 |
|
| 74 |
=head1 AUTHORS |
| 75 |
|
| 76 |
=over 4 |
| 77 |
|
| 78 |
=item Chad Granum E<lt>[email protected]<gt> |
| 79 |
|
| 80 |
=back |
| 81 |
|
| 82 |
=head1 COPYRIGHT |
| 83 |
|
| 84 |
Copyright 2020 Chad Granum E<lt>[email protected]<gt>. |
| 85 |
|
| 86 |
This program is free software; you can redistribute it and/or |
| 87 |
modify it under the same terms as Perl itself. |
| 88 |
|
| 89 |
See F<http://dev.perl.org/licenses/> |
| 90 |
|
| 91 |
=cut |