Codebase list libcryptx-perl / 5a42f11
fix #72 - drop optional dependencies JSON::XS+Cpanel::JSON::XS, use optionally only pure JSON Karel Miko 2 years ago
1 changed file(s) with 5 addition(s) and 24 deletion(s). Raw diff Collapse all Expand all
1010 my $has_json;
1111
1212 BEGIN {
13 if (eval { require Cpanel::JSON::XS }) {
14 Cpanel::JSON::XS->import(qw(encode_json decode_json));
15 $has_json = 1;
16 }
17 elsif (eval { require JSON::XS }) {
18 JSON::XS->import(qw(encode_json decode_json));
19 $has_json = 2;
20 }
21 elsif (eval { require JSON::PP }) {
22 JSON::PP->import(qw(encode_json decode_json));
23 $has_json = 3;
24 }
25 else {
26 $has_json = 0;
27 }
13 $has_json = 1 if eval { require JSON; 1 };
2814 }
2915
3016 sub _croak {
3723 }
3824
3925 sub _decode_json {
40 croak "FATAL: cannot find JSON::PP or JSON::XS or Cpanel::JSON::XS" if !$has_json;
41 decode_json(shift);
26 croak "FATAL: cannot find JSON module" if !$has_json;
27 return JSON->new->utf8->decode(shift);
4228 }
4329
4430 sub _encode_json {
45 croak "FATAL: cannot find JSON::PP or JSON::XS or Cpanel::JSON::XS" if !$has_json;
46 my $data = shift;
47 my $rv = encode_json($data); # non-canonical fallback
48 return(eval { Cpanel::JSON::XS->new->canonical->encode($data) } || $rv) if $has_json == 1;
49 return(eval { JSON::XS->new->canonical->encode($data) } || $rv) if $has_json == 2;
50 return(eval { JSON::PP->new->canonical->encode($data) } || $rv) if $has_json == 3;
51 return($rv);
31 croak "FATAL: cannot find JSON module" if !$has_json;
32 return JSON->new->utf8->canonical->encode(shift);
5233 }
5334
5435 1;