« /usr/local/lib/perl5/5.44.0/x86_64-linux/Encode.pm
↑ Summary
/usr/local/lib/perl5/site_perl/5.44.0/Moo/_Utils.pm »
| 1 | package JSON::MaybeXS; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3 | 7 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4 | 7 | ▶ | use warnings FATAL => 'all'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 5 | 7 | ▶ | use base qw(Exporter); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 7 | 7 | our $VERSION = '1.004008'; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 8 | 7 | $VERSION =~ tr/_//d; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 10 | 7 | ▶ | sub _choose_json_module { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_choose_json_module: called CC=7 SCAR=31.3
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 11 | 7 | ▶ | return 'Cpanel::JSON::XS' if $INC{'Cpanel/JSON/XS.pm'}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 12 | 0 | ▶ | return 'JSON::XS' if $INC{'JSON/XS.pm'} && eval { JSON::XS->VERSION(3.0); 1 }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$INC{"JSON/XS.pm"} and eval {
do {
"JSON::XS"->VERSION(3);
1
}
}
MC/DC0%0/2
$INC{"JSON/XS.pm"} and eval {
do {
"JSON::XS"->VERSION(3);
1
}
}
$INC{"JSON/XS.pm"}
eval {
do {
"JSON::XS"->VERSION(3);
1
}
}
Truth table33%1/3
A and B
A= $INC{"JSON/XS.pm"}B= eval {
do {
"JSON::XS"->VERSION(3);
1
}
}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 14 | 7 | my @err; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 16 | 7 | ▶ | return 'Cpanel::JSON::XS' if eval { require Cpanel::JSON::XS; 1; }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 17 | 0 | push @err, "Error loading Cpanel::JSON::XS: $@"; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 19 | 0 | ▶ | return 'JSON::XS' if eval { require JSON::XS; JSON::XS->VERSION(3.0); 1; }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 20 | 0 | push @err, "Error loading JSON::XS: $@"; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 22 | 0 | ▶ | return 'JSON::PP' if eval { require JSON::PP; 1 }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 23 | 0 | push @err, "Error loading JSON::PP: $@"; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 25 | 0 | die join( "\n", "Couldn't load a JSON module:", @err ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 27 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 29 | 7 | ▶ | BEGIN { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 30 | 7 | our $JSON_Class = _choose_json_module(); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 31 | 7 | $JSON_Class->import(qw(encode_json decode_json)); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 32 | 7 | ▶ | no strict 'refs'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 33 | *$_ = $JSON_Class->can($_) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 34 | 7 | for qw(true false); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 35 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 37 | 7 | our @EXPORT = qw(encode_json decode_json JSON); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 38 | 7 | my @EXPORT_ALL = qw(is_bool); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 39 | 7 | our @EXPORT_OK = qw(is_bool to_json from_json); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 40 | 7 | our %EXPORT_TAGS = ( all => [ @EXPORT, @EXPORT_ALL ], | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 41 | legacy => [ @EXPORT, @EXPORT_OK ], | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 42 | ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 44 | 0 | ▶ | sub JSON () { our $JSON_Class } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
JSON: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 46 | 7 | ▶ | sub new { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
new: called CC=3 SCAR=11.4
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 47 | 7 | shift; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 48 | 0 | ▶ | my %args = @_ == 1 ? %{$_[0]} : @_; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 49 | 7 | my $new = (our $JSON_Class)->new; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 50 | 7 | $new->$_($args{$_}) for keys %args; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 51 | 7 | return $new; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 52 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 54 | 7 | ▶ | use Scalar::Util (); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 55 | 7 | ▶ | use constant HAVE_BUILTIN => "$]" >= 5.036; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 56 | 7 | ▶ | use if HAVE_BUILTIN, experimental => 'builtin'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 58 | 228 | ▶ | sub is_bool { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
is_bool: called CC=6 SCAR=22.1
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 59 | 228 | ▶ | die 'is_bool is not a method' if @_ > 1; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 61 | 228 | ▶ | HAVE_BUILTIN and builtin::is_bool($_[0]) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition50%6/12
builtin::is_bool($_[0]) || !!builtin::blessed($_[0]) && ($_[0]->isa('JSON::PP::Boolean') || $_[0]->isa('Cpanel::JSON::XS::Boolean') || $_[0]->isa('JSON::XS::Boolean'))
not !builtin::blessed($_[0]) and $_[0]->isa("JSON::PP::Boolean") || $_[0]->isa("Cpanel::JSON::XS::Boolean") || $_[0]->isa("JSON::XS::Boolean")
$_[0]->isa("JSON::PP::Boolean") or $_[0]->isa("Cpanel::JSON::XS::Boolean") or $_[0]->isa("JSON::XS::Boolean")
$_[0]->isa("JSON::PP::Boolean") or $_[0]->isa("Cpanel::JSON::XS::Boolean")
MC/DC20%1/5
builtin::is_bool($_[0]) || !!builtin::blessed($_[0]) && ($_[0]->isa('JSON::PP::Boolean') || $_[0]->isa('Cpanel::JSON::XS::Boolean') || $_[0]->isa('JSON::XS::Boolean'))
builtin::is_bool($_[0])
not !builtin::blessed($_[0])
$_[0]->isa("JSON::PP::Boolean")
$_[0]->isa("Cpanel::JSON::XS::Boolean")
$_[0]->isa("JSON::XS::Boolean")
Truth table33%2/6
A || B and C or D or E
A= builtin::is_bool($_[0])B= not !builtin::blessed($_[0])C= $_[0]->isa("JSON::PP::Boolean")D= $_[0]->isa("Cpanel::JSON::XS::Boolean")E= $_[0]->isa("JSON::XS::Boolean")
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 62 | or | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 63 | !!Scalar::Util::blessed($_[0]) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 64 | and ($_[0]->isa('JSON::PP::Boolean') | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 65 | or $_[0]->isa('Cpanel::JSON::XS::Boolean') | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 66 | or $_[0]->isa('JSON::XS::Boolean')); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 67 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 69 | # (mostly) CopyPasta from JSON.pm version 2.90 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 70 | 7 | ▶ | use Carp (); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 72 | 0 | ▶ | sub from_json ($@) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
from_json: not called CC=6 SCAR=37.4
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 73 | 0 | ▶ | if ( ref($_[0]) =~ /^JSON/ or $_[0] =~ /^JSON/ ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
ref($_[0]) =~ /^JSON/ or $_[0] =~ /^JSON/
MC/DC0%0/2
ref($_[0]) =~ /^JSON/ or $_[0] =~ /^JSON/
ref($_[0]) =~ /^JSON/
$_[0] =~ /^JSON/
Truth table0%0/3
A or B
A= ref($_[0]) =~ /^JSON/B= $_[0] =~ /^JSON/
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 74 | 0 | Carp::croak "from_json should not be called as a method."; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 75 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 76 | 0 | my $json = JSON()->new; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 78 | 0 | ▶ | if (@_ == 2 and ref $_[1] eq 'HASH') { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
@_ == 2 and ref $_[1] eq "HASH"
MC/DC0%0/2
@_ == 2 and ref $_[1] eq "HASH"
@_ == 2
ref $_[1] eq "HASH"
Truth table0%0/3
A and B
A= @_ == 2B= ref $_[1] eq "HASH"
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 79 | 0 | my $opt = $_[1]; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 80 | 0 | for my $method (keys %$opt) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 81 | 0 | $json->$method( $opt->{$method} ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 82 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 83 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 85 | 0 | return $json->decode( $_[0] ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 86 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 88 | 0 | ▶ | sub to_json ($@) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
to_json: not called CC=7 SCAR=40.3
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 89 | 0 | ▶ | if ( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/6
ref($_[0]) =~ /^JSON/ or @_ > 2 and $_[0] =~ /^JSON/
@_ > 2 and $_[0] =~ /^JSON/
MC/DC0%0/3
ref($_[0]) =~ /^JSON/ or @_ > 2 and $_[0] =~ /^JSON/
ref($_[0]) =~ /^JSON/
@_ > 2
$_[0] =~ /^JSON/
Truth table0%0/4
A or B and C
A= ref($_[0]) =~ /^JSON/B= @_ > 2C= $_[0] =~ /^JSON/
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 90 | ref($_[0]) =~ /^JSON/ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 91 | or (@_ > 2 and $_[0] =~ /^JSON/) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 92 | ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 93 | 0 | Carp::croak "to_json should not be called as a method."; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 94 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 95 | 0 | my $json = JSON()->new; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 97 | 0 | ▶ | if (@_ == 2 and ref $_[1] eq 'HASH') { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
@_ == 2 and ref $_[1] eq "HASH"
MC/DC0%0/2
@_ == 2 and ref $_[1] eq "HASH"
@_ == 2
ref $_[1] eq "HASH"
Truth table0%0/3
A and B
A= @_ == 2B= ref $_[1] eq "HASH"
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 98 | 0 | my $opt = $_[1]; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 99 | 0 | for my $method (keys %$opt) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 100 | 0 | $json->$method( $opt->{$method} ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 101 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 102 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 104 | 0 | $json->encode($_[0]); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 105 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 107 | 7 | 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 109 | =head1 NAME | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 111 | JSON::MaybeXS - Use L<Cpanel::JSON::XS> with a fallback to L<JSON::XS> and L<JSON::PP> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 113 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 115 | use JSON::MaybeXS; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 117 | my $data_structure = decode_json($json_input); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 119 | my $json_output = encode_json($data_structure); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 121 | my $json = JSON()->new; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 123 | my $json_with_args = JSON::MaybeXS->new(utf8 => 1); # or { utf8 => 1 } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 125 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 127 | This module first checks to see if either L<Cpanel::JSON::XS> or | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 128 | L<JSON::XS> (at at least version 3.0) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 129 | is already loaded, in which case it uses that module. Otherwise | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 130 | it tries to load L<Cpanel::JSON::XS>, then L<JSON::XS>, then L<JSON::PP> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 131 | in order, and either uses the first module it finds or throws an error. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 133 | It then exports the C<encode_json> and C<decode_json> functions from the | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 134 | loaded module, along with a C<JSON> constant that returns the class name | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 135 | for calling C<new> on. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 137 | If you're writing fresh code rather than replacing L<JSON.pm|JSON> usage, you might | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 138 | want to pass options as constructor args rather than calling mutators, so | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 139 | we provide our own C<new> method that supports that. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 141 | =head1 EXPORTS | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 143 | C<encode_json>, C<decode_json> and C<JSON> are exported by default; C<is_bool> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 144 | is exported on request. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 145 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 146 | To import only some symbols, specify them on the C<use> line: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 148 | use JSON::MaybeXS qw(encode_json decode_json is_bool); # functions only | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 150 | use JSON::MaybeXS qw(JSON); # JSON constant only | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 151 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 152 | To import all available sensible symbols (C<encode_json>, C<decode_json>, and | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 153 | C<is_bool>), use C<:all>: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 154 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 155 | use JSON::MaybeXS ':all'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 156 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 157 | To import all symbols including those needed by legacy apps that use L<JSON::PP>: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 158 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 159 | use JSON::MaybeXS ':legacy'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 160 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 161 | This imports the C<to_json> and C<from_json> symbols as well as everything in | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 162 | C<:all>. NOTE: This is to support legacy code that makes extensive | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 163 | use of C<to_json> and C<from_json> which you are not yet in a position to | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 164 | refactor. DO NOT use this import tag in new code, in order to avoid | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 165 | the crawling horrors of getting UTF-8 support subtly wrong. See the | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 166 | documentation for L<JSON> for further details. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 167 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 168 | =head2 encode_json | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 169 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 170 | This is the C<encode_json> function provided by the selected implementation | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 171 | module, and takes a perl data structure which is serialised to JSON text. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 172 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 173 | my $json_text = encode_json($data_structure); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 174 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 175 | =head2 decode_json | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 176 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 177 | This is the C<decode_json> function provided by the selected implementation | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 178 | module, and takes a string of JSON text to deserialise to a perl data structure. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 179 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 180 | my $data_structure = decode_json($json_text); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 181 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 182 | =head2 to_json | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 183 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 184 | This function is equivalent to calling C<< JSON()->new->encode($data_structure) >>. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 185 | It takes a perl data structure which is serialised to JSON text without encoding | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 186 | it to UTF-8. You should only use this function if you expect another layer to | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 187 | handle the UTF-8 encoding of the resulting JSON text. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 188 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 189 | my $json_text = to_json($data_structure); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 190 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 191 | Additional arguments can be passed and will be handled as in L<JSON/to_json>, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 192 | this is included to support legacy code B<only>. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 193 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 194 | =head2 from_json | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 195 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 196 | This function is equivalent to calling C<< JSON()->new->decode($json_text) >>. It | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 197 | takes a string of unencoded JSON text to deserialise to a perl data structure. You | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 198 | should only use this function if another layer is already handling the UTF-8 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 199 | decoding of the input JSON text. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 200 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 201 | my $data_structure = from_json($json_text); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 202 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 203 | Additional arguments can be passed and will be handled as in L<JSON/from_json>, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 204 | this is included to support legacy code B<only>. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 205 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 206 | =head2 JSON | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 207 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 208 | The C<JSON> constant returns the selected implementation module's name for | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 209 | use as a class name - so: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 210 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 211 | my $json_obj = JSON()->new; # returns a Cpanel::JSON::XS or JSON::PP object | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 212 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 213 | and that object can then be used normally: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 214 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 215 | my $data_structure = $json_obj->decode($json_text); # etc. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 216 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 217 | The use of parentheses here is optional, and only used as a hint to the reader | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 218 | that this use of C<JSON> is a I<subroutine> call, I<not> a class name. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 219 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 220 | =head2 is_bool | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 221 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 222 | $is_boolean = is_bool($scalar) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 223 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 224 | Returns true if the passed scalar represents either C<true> or | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 225 | C<false>, two constants that act like C<1> and C<0>, respectively | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 226 | and are used to represent JSON C<true> and C<false> values in Perl. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 227 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 228 | Since this is a bare sub in the various backend classes, it cannot be called as | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 229 | a class method like the other interfaces; it must be called as a function, with | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 230 | no invocant. It supports the representation used in all JSON backends. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 231 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 232 | Available since version 1.002004. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 233 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 234 | =head1 CONSTRUCTOR | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 235 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 236 | =head2 new | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 237 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 238 | With L<JSON::PP>, L<JSON::XS> and L<Cpanel::JSON::XS> you are required to call | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 239 | mutators to set options, such as: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 240 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 241 | my $json = $class->new->utf8(1)->pretty(1); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 242 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 243 | Since this is a trifle irritating and noticeably un-perlish, we also offer: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 244 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 245 | my $json = JSON::MaybeXS->new(utf8 => 1, pretty => 1); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 246 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 247 | which works equivalently to the above (and in the usual tradition will accept | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 248 | a hashref instead of a hash, should you so desire). | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 249 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 250 | The resulting object is blessed into the underlying backend, which offers (at | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 251 | least) the methods C<encode> and C<decode>. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 252 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 253 | =head1 BOOLEANS | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 254 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 255 | To include JSON-aware booleans (C<true>, C<false>) in your data, just do: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 256 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 257 | use JSON::MaybeXS; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 258 | my $true = JSON()->true; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 259 | my $false = JSON()->false; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 260 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 261 | The booleans are also available as subs or methods on JSON::MaybeXS. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 262 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 263 | use JSON::MaybeXS (); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 264 | my $true = JSON::MaybeXS::true; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 265 | my $true = JSON::MaybeXS->true; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 266 | my $false = JSON::MaybeXS::false; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 267 | my $false = JSON::MaybeXS->false; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 268 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 269 | =head1 CONVERTING FROM JSON::Any | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 270 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 271 | L<JSON::Any> used to be the favoured compatibility layer above the various | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 272 | JSON backends, but over time has grown a lot of extra code to deal with legacy | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 273 | backends (e.g. L<JSON::Syck>) that are no longer needed. This is a rough guide of translating such code: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 274 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 275 | Change code from: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 276 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 277 | use JSON::Any; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 278 | my $json = JSON::Any->new->objToJson($data); # or to_json($data), or Dump($data) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 279 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 280 | to: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 281 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 282 | use JSON::MaybeXS; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 283 | my $json = encode_json($data); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 284 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 285 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 286 | Change code from: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 287 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 288 | use JSON::Any; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 289 | my $data = JSON::Any->new->jsonToObj($json); # or from_json($json), or Load($json) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 290 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 291 | to: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 292 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 293 | use JSON::MaybeXS; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 294 | my $json = decode_json($data); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 295 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 296 | =head1 CAVEATS | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 297 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 298 | The C<new()> method in this module is technically a factory, not a | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 299 | constructor, because the objects it returns will I<NOT> be blessed into the | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 300 | C<JSON::MaybeXS> class. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 301 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 302 | If you are using an object returned by this module as a Moo(se) attribute, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 303 | this type constraint code: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 304 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 305 | is 'json' => ( isa => 'JSON::MaybeXS' ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 306 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 307 | will I<NOT> do what you expect. Instead, either rely on the C<JSON> class | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 308 | constant described above, as so: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 309 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 310 | is 'json' => ( isa => JSON::MaybeXS::JSON() ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 311 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 312 | Alternatively, you can use duck typing: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 313 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 314 | use Moose::Util::TypeConstraints 'duck_type'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 315 | is 'json' => ( isa => Object , duck_type([qw/ encode decode /])); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 316 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 317 | =head1 INSTALLATION | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 318 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 319 | At installation time, F<Makefile.PL> will attempt to determine if you have a | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 320 | working compiler available, and therefore whether you are able to run XS code. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 321 | If so, L<Cpanel::JSON::XS> will be added to the prerequisite list, unless | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 322 | L<JSON::XS> is already installed at a high enough version. L<JSON::XS> may | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 323 | also be upgraded to fix any incompatibility issues. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 324 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 325 | Because running XS code is not mandatory and L<JSON::PP> (which is in perl | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 326 | core) is used as a fallback backend, this module is safe to be used in a suite | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 327 | of code that is fatpacked or installed into a restricted-resource environment. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 328 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 329 | You can also prevent any XS dependencies from being installed by setting | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 330 | C<PUREPERL_ONLY=1> in F<Makefile.PL> options (or in the C<PERL_MM_OPT> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 331 | environment variable), or using the C<--pp> or C<--pureperl> flags with the | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 332 | L<cpanminus client|cpanm>. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 333 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 334 | =head1 AUTHOR | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 335 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 336 | mst - Matt S. Trout (cpan:MSTROUT) <mst@shadowcat.co.uk> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 337 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 338 | =head1 CONTRIBUTORS | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 339 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 340 | =over 4 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 341 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 342 | =item * Clinton Gormley <drtech@cpan.org> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 343 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 344 | =item * Karen Etheridge <ether@cpan.org> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 345 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 346 | =item * Kieren Diment <diment@gmail.com> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 347 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 348 | =back | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 349 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 350 | =head1 COPYRIGHT | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 351 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 352 | Copyright (c) 2013 the C<JSON::MaybeXS> L</AUTHOR> and L</CONTRIBUTORS> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 353 | as listed above. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 354 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 355 | =head1 LICENSE | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 356 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 357 | This library is free software and may be distributed under the same terms | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 358 | as perl itself. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 359 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 360 | =cut | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||