| 1 | package Test::Smoke::Util::LoadAJSON; | ||||||||||
| 2 | 15 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 3 | 11 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | |||||||||||
| 5 | 11 | our $VERSION = '0.04'; | |||||||||
| 6 | |||||||||||
| 7 | =head1 NAME | ||||||||||
| 8 | |||||||||||
| 9 | Test::Smoke::Util::LoadAJSON - A Cpanel::JSON::XS/JSON:PP/JSON::XS Factory Class | ||||||||||
| 10 | |||||||||||
| 11 | =head1 SYNOPSIS | ||||||||||
| 12 | |||||||||||
| 13 | use Test::Smoke::Util::LoadAJSON; | ||||||||||
| 14 | my $json = Test::Smoke::Util::LoadAJSON->new->utf8->pretty->encode(\%data); | ||||||||||
| 15 | |||||||||||
| 16 | =head1 DESCRIPTION | ||||||||||
| 17 | |||||||||||
| 18 | This is purely a fallback factory class that helps keep our code clean. | ||||||||||
| 19 | |||||||||||
| 20 | This is for people with a clean perl 5.14+ install that have L<JSON::PP> but not | ||||||||||
| 21 | JSON. Also people that installed L<JSON::XS> on a pre-5.14 system. | ||||||||||
| 22 | |||||||||||
| 23 | Also checks for C<$ENV{PERL_JSON_BACKEND}> to force either of the two. | ||||||||||
| 24 | |||||||||||
| 25 | =cut | ||||||||||
| 26 | |||||||||||
| 27 | 11 | ▶ | use Exporter 'import'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 28 | 11 | our @EXPORT = qw/encode_json decode_json JSON/; | |||||||||
| 29 | |||||||||||
| 30 | 11 | ▶ | no warnings 'redefine'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 31 | 11 | my $json_base_class; | |||||||||
| 32 | 15 | ▶ | sub import { | ||||||||
|
Subroutine
import: called CC=2 SCAR=6.9
| |||||||||||
| 33 | 15 | my ($class) = @_; | |||||||||
| 34 | 15 | $json_base_class = $class->find_base_class; | |||||||||
| 35 | |||||||||||
| 36 | 15 | ▶ | die "Could not find a supported JSON implementation.\n" | ||||||||
|
Branch: 1 branch
| |||||||||||
| 37 | if !$json_base_class; | ||||||||||
| 38 | |||||||||||
| 39 | { | ||||||||||
| 40 | 11 | ▶ | no warnings 'redefine', 'once'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 41 | 15 | *encode_json = \&{$json_base_class."::encode_json"}; | |||||||||
| 42 | 15 | *decode_json = \&{$json_base_class."::decode_json"}; | |||||||||
| 43 | } | ||||||||||
| 44 | 15 | goto &Exporter::import; | |||||||||
| 45 | } | ||||||||||
| 46 | |||||||||||
| 47 | =head2 my $class = Test::Smoke::Util::LoadAJSON->find_base_class() | ||||||||||
| 48 | |||||||||||
| 49 | On success returns one of: B<Cpanel::JSON::XS>, B<JSON::PP>, B<JSON::XS> | ||||||||||
| 50 | |||||||||||
| 51 | Returns undef on failure. | ||||||||||
| 52 | |||||||||||
| 53 | =cut | ||||||||||
| 54 | |||||||||||
| 55 | 15 | ▶ | sub find_base_class { | ||||||||
|
Subroutine
find_base_class: called CC=4 SCAR=14.8
Pod
documented
| |||||||||||
| 56 | my @backends = $ENV{PERL_JSON_BACKEND} | ||||||||||
| 57 | ? ($ENV{PERL_JSON_BACKEND}) | ||||||||||
| 58 | 15 | ▶ | : qw/Cpanel::JSON::XS JSON::PP JSON::XS/; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 59 | 15 | for my $try_class (@backends) { | |||||||||
| 60 | 11 | ▶ | eval "use $try_class"; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 61 | 15 | ▶ | next if $@; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 62 | 15 | return $try_class; | |||||||||
| 63 | 0 | last; | |||||||||
| 64 | } | ||||||||||
| 65 | 0 | return; | |||||||||
| 66 | } | ||||||||||
| 67 | |||||||||||
| 68 | =head2 JSON | ||||||||||
| 69 | |||||||||||
| 70 | Returns the current C<$json_base_class>. | ||||||||||
| 71 | |||||||||||
| 72 | =cut | ||||||||||
| 73 | |||||||||||
| 74 | 17 | ▶ | sub JSON { $json_base_class } | ||||||||
|
Subroutine
JSON: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 75 | |||||||||||
| 76 | =head2 my $obj = Test::Smoke::Util::LoadAJSON->new(<arguments>) | ||||||||||
| 77 | |||||||||||
| 78 | If a base class is found, will return an instantiated object. | ||||||||||
| 79 | |||||||||||
| 80 | This will die() if no base class could be found. | ||||||||||
| 81 | |||||||||||
| 82 | =cut | ||||||||||
| 83 | |||||||||||
| 84 | 1 | ▶ | sub new { | ||||||||
|
Subroutine
new: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 85 | 1 | my $class = shift; | |||||||||
| 86 | 1 | return $json_base_class->new(@_); | |||||||||
| 87 | } | ||||||||||
| 88 | |||||||||||
| 89 | 11 | 1; | |||||||||
| 90 | |||||||||||
| 91 | =head1 COPYRIGHT | ||||||||||
| 92 | |||||||||||
| 93 | E<copy> MMXIV-MMXXIII, All rights reserved. | ||||||||||
| 94 | |||||||||||
| 95 | * Abe Timmerman <abeltje@cpan.org> | ||||||||||
| 96 | |||||||||||
| 97 | This library is free software; you can redistribute it and/or modify | ||||||||||
| 98 | it under the same terms as Perl itself. | ||||||||||
| 99 | |||||||||||
| 100 | See: | ||||||||||
| 101 | |||||||||||
| 102 | =over 4 | ||||||||||
| 103 | |||||||||||
| 104 | =item * L<http://www.perl.com/perl/misc/Artistic.html> | ||||||||||
| 105 | |||||||||||
| 106 | =item * L<http://www.gnu.org/copyleft/gpl.html> | ||||||||||
| 107 | |||||||||||
| 108 | =back | ||||||||||
| 109 | |||||||||||
| 110 | This program is distributed in the hope that it will be useful, | ||||||||||
| 111 | but WITHOUT ANY WARRANTY; without even the implied warranty of | ||||||||||
| 112 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | ||||||||||
| 113 | |||||||||||
| 114 | =cut | ||||||||||