| 1 | #======================================================================== | ||||||||||||||||||||||||||||
| 2 | # | ||||||||||||||||||||||||||||
| 3 | # Badger::Codec::JSON | ||||||||||||||||||||||||||||
| 4 | # | ||||||||||||||||||||||||||||
| 5 | # DESCRIPTION | ||||||||||||||||||||||||||||
| 6 | # Codec module for encoding/decoding Base64 | ||||||||||||||||||||||||||||
| 7 | # | ||||||||||||||||||||||||||||
| 8 | # AUTHOR | ||||||||||||||||||||||||||||
| 9 | # Andy Wardley <abw@wardley.org> | ||||||||||||||||||||||||||||
| 10 | # | ||||||||||||||||||||||||||||
| 11 | #======================================================================== | ||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | package Badger::Codec::JSON; | ||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | use Badger::Class | ||||||||||||||||||||||||||||
| 16 | 1 | version => 0.01, | |||||||||||||||||||||||||||
| 17 | base => 'Badger::Codec', | ||||||||||||||||||||||||||||
| 18 | import => 'class CLASS', | ||||||||||||||||||||||||||||
| 19 | 1 | ▶ | codecs => 'utf8'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||
| 21 | 1 | our ($HAS_CP_JSON_XS, $HAS_JSON_XS, $HAS_JSON); | |||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | # Cpanel::JSON::XS is more complete/correct than JSON::XS | ||||||||||||||||||||||||||||
| 24 | 1 | eval "require Cpanel::JSON::XS"; | |||||||||||||||||||||||||||
| 25 | 1 | ▶ | $HAS_CP_JSON_XS = $@ ? 0 : 1; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||
| 27 | # JSON::XS has bits missing (e.g. allow_bignum) | ||||||||||||||||||||||||||||
| 28 | 1 | ▶ | unless ($HAS_CP_JSON_XS) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 29 | 0 | eval "require JSON::XS"; | |||||||||||||||||||||||||||
| 30 | 0 | ▶ | $HAS_JSON_XS = $@ ? 0 : 1; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 31 | } | ||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||
| 33 | # fallback to Perl implementation | ||||||||||||||||||||||||||||
| 34 | 1 | ▶ | unless ($HAS_CP_JSON_XS || $HAS_JSON_XS) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$Badger::Codec::JSON::HAS_CP_JSON_XS or $Badger::Codec::JSON::HAS_JSON_XS
MC/DC0%0/2
$Badger::Codec::JSON::HAS_CP_JSON_XS or $Badger::Codec::JSON::HAS_JSON_XS
$Badger::Codec::JSON::HAS_CP_JSON_XS
$Badger::Codec::JSON::HAS_JSON_XS
Truth table33%1/3
A or B
A= $Badger::Codec::JSON::HAS_CP_JSON_XSB= $Badger::Codec::JSON::HAS_JSON_XS
| |||||||||||||||||||||||||||||
| 35 | 0 | eval "require JSON"; | |||||||||||||||||||||||||||
| 36 | 0 | ▶ | $HAS_JSON = $@ ? 0 : 1; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 37 | } | ||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||
| 39 | 1 | ▶ | our $MODULE = | ||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||
| 40 | $HAS_CP_JSON_XS ? 'Cpanel::JSON::XS' : | ||||||||||||||||||||||||||||
| 41 | $HAS_JSON_XS ? 'JSON::XS' : | ||||||||||||||||||||||||||||
| 42 | $HAS_JSON ? 'JSON' : | ||||||||||||||||||||||||||||
| 43 | CLASS->error("You don't have JSON, JSON::XS or Cpanel::JSON::XS installed"); | ||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||
| 45 | 1 | our $JSON = $MODULE->new; | |||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||
| 47 | sub encode_json { | ||||||||||||||||||||||||||||
| 48 | 1 | ▶ | $JSON->encode(shift); | ||||||||||||||||||||||||||
|
Subroutine
encode_json: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 49 | } | ||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||
| 51 | sub decode_json { | ||||||||||||||||||||||||||||
| 52 | 1 | ▶ | my $json = shift; | ||||||||||||||||||||||||||
|
Subroutine
decode_json: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 53 | # $json = encode_utf8($json); | ||||||||||||||||||||||||||||
| 54 | 1 | $JSON->decode($json); | |||||||||||||||||||||||||||
| 55 | } | ||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||
| 57 | sub encode { | ||||||||||||||||||||||||||||
| 58 | 1 | ▶ | shift; | ||||||||||||||||||||||||||
|
Subroutine
encode: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 59 | 1 | goto &encode_json; | |||||||||||||||||||||||||||
| 60 | } | ||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||
| 62 | sub decode { | ||||||||||||||||||||||||||||
| 63 | 1 | ▶ | shift; | ||||||||||||||||||||||||||
|
Subroutine
decode: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 64 | 1 | goto &decode_json; | |||||||||||||||||||||||||||
| 65 | } | ||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||
| 67 | sub encoder { | ||||||||||||||||||||||||||||
| 68 | 0 | ▶ | \&encode_json; | ||||||||||||||||||||||||||
|
Subroutine
encoder: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 69 | } | ||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||
| 71 | sub decoder { | ||||||||||||||||||||||||||||
| 72 | 0 | ▶ | \&decode_json; | ||||||||||||||||||||||||||
|
Subroutine
decoder: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 73 | } | ||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||
| 75 | 1 | 1; | |||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||
| 78 | __END__ | ||||||||||||||||||||||||||||