blib/lib/JSON/MaybeUTF8.pm

filter: Statementstmt 100% 39 / 39 Branchbran 50.0% 6 / 12 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 10 / 10 Podpod 100% 4 / 4 totaltotal 90.7% 59 / 65 CC 7 SCAR 7.2
CC
7
Cov
88%
decode_json_utf8
3.1
decode_json_text
3.1
encode_json_utf8
2.0
CRAP 2.0
↑ Summary
1 package JSON::MaybeUTF8;
2 # ABSTRACT: Simple wrapper for explicit JSON Unicode text/UTF-8 byte functions
3  
4 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 3 our $VERSION = '1.002';
8  
9 =head1 NAME
10  
11 JSON::MaybeUTF8 - provide explicit text/UTF-8 JSON functions
12  
13 =head1 SYNOPSIS
14  
15 use JSON::MaybeUTF8 qw(:v1);
16 binmode STDOUT, ':encoding(UTF-8)';
17 binmode STDERR, ':raw';
18 (*STDOUT)->print(encode_json_text({ text => '...' }));
19 (*STDERR)->print(encode_json_utf8({ text => '...' }));
20  
21 =head1 DESCRIPTION
22  
23 Combines L<JSON::MaybeXS> with L<Unicode::UTF8> to provide
24 4 functions that handle the combinations of JSON and UTF-8
25 encoding/decoding.
26  
27 The idea is to make the UTF-8-or-not behaviour more explicit
28 in code that deals with multiple transport layers such as
29 database, cache and I/O.
30  
31 This is a trivial wrapper around two other modules.
32  
33 =cut
34  
35 3 ▶use feature qw(state);
Subroutine BEGIN: called CC=1 SCAR=0.0
36  
37 3 ▶use JSON::MaybeXS;
Subroutine BEGIN: called CC=1 SCAR=0.0
38 3 ▶use Unicode::UTF8 qw(encode_utf8 decode_utf8);
Subroutine BEGIN: called CC=1 SCAR=0.0
39  
40 3 ▶use Exporter qw(import export_to_level);
Subroutine BEGIN: called CC=1 SCAR=0.0
41  
42 =head2 BOM removal
43  
44 The C<< $JSON::Maybe::UTF8::REMOVE_BOM >> flag is B<set by default> due
45 to L<https://github.com/rurban/Cpanel-JSON-XS/issues/125>. If you would
46 prefer to disable this, add C<< $JSON::Maybe::UTF8::REMOVE_BOM = 0; >>
47 in your code.
48  
49 Note that this only affects things when L<Cpanel::JSON::XS> is used (preferred by L<JSON::MaybeXS>
50 if it can be loaded).
51  
52 =cut
53  
54 3 our $REMOVE_BOM = 1;
55  
56 3 our @EXPORT_OK = qw(
57 decode_json_utf8
58 encode_json_utf8
59 decode_json_text
60 encode_json_text
61 );
62 3 our %EXPORT_TAGS = (
63 v1 => [ @EXPORT_OK ],
64 );
65  
66 =head2 decode_json_utf8
67  
68 Given a UTF-8-encoded JSON byte string, returns a Perl data
69 structure. May optionally remove the UTF-8 L<BOM|https://en.wikipedia.org/wiki/Byte_order_mark#UTF-8>
70 if it exists.
71  
72 =cut
73  
74 sub decode_json_utf8 {
75 1004 ▶ state $json = JSON::MaybeXS->new;
Subroutine decode_json_utf8: called CC=3 SCAR=11.3
Pod documented
76 1004 ▶ die 'bad json state' if $json->get_utf8;
Branch: 1 branch
BranchTrueFalseTotal
if $json->get_utf8 0 1004 1004
77 1004 ▶ return $json->decode_utf8($_[0]) unless $REMOVE_BOM;
Branch: 1 branch
BranchTrueFalseTotal
unless $JSON::MaybeUTF8::REMOVE_BOM 0 1004 1004
78 1004 (my $txt = decode_utf8(shift)) =~ s{^\x{feff}}{};
79 1004 return $json->decode($txt);
80 }
81  
82 =head2 encode_json_utf8
83  
84 Given a Perl data structure, returns a UTF-8-encoded JSON
85 byte string.
86  
87 =cut
88  
89 sub encode_json_utf8 {
90 1004 ▶ state $json = JSON::MaybeXS->new;
Subroutine encode_json_utf8: called CC=2 SCAR=7.1
Pod documented
91 1004 ▶ die 'bad json state' if $json->get_utf8;
Branch: 1 branch
BranchTrueFalseTotal
if $json->get_utf8 0 1004 1004
92 1004 encode_utf8($json->encode(shift))
93 }
94  
95 =head2 decode_json_text
96  
97 Given a JSON string composed of Unicode characters (in
98 Perl's internal encoding), returns a Perl data structure.
99  
100 =cut
101  
102 sub decode_json_text {
103 1004 ▶ state $json = JSON::MaybeXS->new;
Subroutine decode_json_text: called CC=3 SCAR=11.3
Pod documented
104 1004 ▶ die 'bad json state' if $json->get_utf8;
Branch: 1 branch
BranchTrueFalseTotal
if $json->get_utf8 0 1004 1004
105 1004 my $txt = shift;
106 1004 ▶ $txt =~ s{^\x{feff}}{} if $REMOVE_BOM;
Branch: 1 branch
BranchTrueFalseTotal
if $JSON::MaybeUTF8::REMOVE_BOM 1004 0 1004
107 1004 $json->decode($txt);
108 }
109  
110 =head2 encode_json_text
111  
112 Given a Perl data structure, returns a JSON string composed
113 of Unicode characters (in Perl's internal encoding).
114  
115 =cut
116  
117 sub encode_json_text {
118 1004 ▶ state $json = JSON::MaybeXS->new;
Subroutine encode_json_text: called CC=2 SCAR=7.1
Pod documented
119 1004 ▶ die 'bad json state' if $json->get_utf8;
Branch: 1 branch
BranchTrueFalseTotal
if $json->get_utf8 0 1004 1004
120 1004 $json->encode(shift)
121 }
122  
123 3 1;
124  
125 =head1 AUTHOR
126  
127 Tom Molesworth <TEAM@cpan.org>
128  
129 =head1 LICENSE
130  
131 Copyright Tom Molesworth 2017. Licensed under the same terms as Perl itself.
132  
↑ Summary