| 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 = '2.000'; | |||||||||
| 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 | format_json_text | ||||||||||
| 62 | ); | ||||||||||
| 63 | 3 | our %EXPORT_TAGS = ( | |||||||||
| 64 | v1 => [ qw( | ||||||||||
| 65 | decode_json_utf8 | ||||||||||
| 66 | encode_json_utf8 | ||||||||||
| 67 | decode_json_text | ||||||||||
| 68 | encode_json_text | ||||||||||
| 69 | ) ], | ||||||||||
| 70 | v2 => [ @EXPORT_OK ], | ||||||||||
| 71 | ); | ||||||||||
| 72 | |||||||||||
| 73 | =head2 decode_json_utf8 | ||||||||||
| 74 | |||||||||||
| 75 | Given a UTF-8-encoded JSON byte string, returns a Perl data | ||||||||||
| 76 | structure. May optionally remove the UTF-8 L<BOM|https://en.wikipedia.org/wiki/Byte_order_mark#UTF-8> | ||||||||||
| 77 | if it exists. | ||||||||||
| 78 | |||||||||||
| 79 | =cut | ||||||||||
| 80 | |||||||||||
| 81 | 1004 | ▶ | sub decode_json_utf8 { | ||||||||
|
Subroutine
decode_json_utf8: called CC=3 SCAR=11.3
Pod
documented
| |||||||||||
| 82 | 1004 | state $json = JSON::MaybeXS->new; | |||||||||
| 83 | 1004 | ▶ | die 'bad json state' if $json->get_utf8; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 84 | 1004 | ▶ | return $json->decode_utf8($_[0]) unless $REMOVE_BOM; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 85 | 1004 | (my $txt = decode_utf8(shift)) =~ s{^\x{feff}}{}; | |||||||||
| 86 | 1004 | return $json->decode($txt); | |||||||||
| 87 | } | ||||||||||
| 88 | |||||||||||
| 89 | =head2 encode_json_utf8 | ||||||||||
| 90 | |||||||||||
| 91 | Given a Perl data structure, returns a UTF-8-encoded JSON | ||||||||||
| 92 | byte string. | ||||||||||
| 93 | |||||||||||
| 94 | =cut | ||||||||||
| 95 | |||||||||||
| 96 | 1004 | ▶ | sub encode_json_utf8 { | ||||||||
|
Subroutine
encode_json_utf8: called CC=2 SCAR=7.1
Pod
documented
| |||||||||||
| 97 | 1004 | state $json = JSON::MaybeXS->new; | |||||||||
| 98 | 1004 | ▶ | die 'bad json state' if $json->get_utf8; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 99 | 1004 | encode_utf8($json->encode(shift)) | |||||||||
| 100 | } | ||||||||||
| 101 | |||||||||||
| 102 | =head2 decode_json_text | ||||||||||
| 103 | |||||||||||
| 104 | Given a JSON string composed of Unicode characters (in | ||||||||||
| 105 | Perl's internal encoding), returns a Perl data structure. | ||||||||||
| 106 | |||||||||||
| 107 | =cut | ||||||||||
| 108 | |||||||||||
| 109 | 1008 | ▶ | sub decode_json_text { | ||||||||
|
Subroutine
decode_json_text: called CC=3 SCAR=11.3
Pod
documented
| |||||||||||
| 110 | 1008 | state $json = JSON::MaybeXS->new; | |||||||||
| 111 | 1008 | ▶ | die 'bad json state' if $json->get_utf8; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 112 | 1008 | my $txt = shift; | |||||||||
| 113 | 1008 | ▶ | $txt =~ s{^\x{feff}}{} if $REMOVE_BOM; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 114 | 1008 | $json->decode($txt); | |||||||||
| 115 | } | ||||||||||
| 116 | |||||||||||
| 117 | =head2 encode_json_text | ||||||||||
| 118 | |||||||||||
| 119 | Given a Perl data structure, returns a JSON string composed | ||||||||||
| 120 | of Unicode characters (in Perl's internal encoding). | ||||||||||
| 121 | |||||||||||
| 122 | =cut | ||||||||||
| 123 | |||||||||||
| 124 | 1004 | ▶ | sub encode_json_text { | ||||||||
|
Subroutine
encode_json_text: called CC=2 SCAR=7.1
Pod
documented
| |||||||||||
| 125 | 1004 | state $json = JSON::MaybeXS->new; | |||||||||
| 126 | 1004 | ▶ | die 'bad json state' if $json->get_utf8; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 127 | 1004 | $json->encode(shift) | |||||||||
| 128 | } | ||||||||||
| 129 | |||||||||||
| 130 | =head2 encode_json_text | ||||||||||
| 131 | |||||||||||
| 132 | Given a Perl data structure, returns a formatted JSON string composed | ||||||||||
| 133 | of Unicode characters (in Perl's internal encoding). | ||||||||||
| 134 | |||||||||||
| 135 | This is functionally identical to L</encode_json_text>, but with | ||||||||||
| 136 | indentation to make it readable, and with defined key ordering which | ||||||||||
| 137 | should make it easier to C<diff> two different data structures. | ||||||||||
| 138 | |||||||||||
| 139 | =cut | ||||||||||
| 140 | |||||||||||
| 141 | 4 | ▶ | sub format_json_text { | ||||||||
|
Subroutine
format_json_text: called CC=2 SCAR=7.1
Pod
undocumented
| |||||||||||
| 142 | 4 | state $json = JSON::MaybeXS->new( | |||||||||
| 143 | pretty => 1, | ||||||||||
| 144 | canonical => 1, | ||||||||||
| 145 | ); | ||||||||||
| 146 | 4 | ▶ | die 'bad json state' if $json->get_utf8; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 147 | 4 | $json->encode(shift) | |||||||||
| 148 | } | ||||||||||
| 149 | |||||||||||
| 150 | 3 | 1; | |||||||||
| 151 | |||||||||||
| 152 | =head1 AUTHOR | ||||||||||
| 153 | |||||||||||
| 154 | Tom Molesworth <TEAM@cpan.org> | ||||||||||
| 155 | |||||||||||
| 156 | =head1 LICENSE | ||||||||||
| 157 | |||||||||||
| 158 | Copyright Tom Molesworth 2017-2021. Licensed under the same terms as Perl itself. | ||||||||||
| 159 | |||||||||||