fix #72 - drop optional dependencies JSON::XS+Cpanel::JSON::XS, use optionally only pure JSON
Karel Miko
2 years ago
10 | 10 | my $has_json; |
11 | 11 | |
12 | 12 | 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 }; | |
28 | 14 | } |
29 | 15 | |
30 | 16 | sub _croak { |
37 | 23 | } |
38 | 24 | |
39 | 25 | 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); | |
42 | 28 | } |
43 | 29 | |
44 | 30 | 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); | |
52 | 33 | } |
53 | 34 | |
54 | 35 | 1; |