| 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.001'; | |
| 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 | 3 | our @EXPORT_OK = qw( | |
| 43 | decode_json_utf8 | ||
| 44 | encode_json_utf8 | ||
| 45 | decode_json_text | ||
| 46 | encode_json_text | ||
| 47 | ); | ||
| 48 | 3 | our %EXPORT_TAGS = ( | |
| 49 | v1 => [ @EXPORT_OK ], | ||
| 50 | ); | ||
| 51 | |||
| 52 | =head2 decode_json_utf8 | ||
| 53 | |||
| 54 | Given a UTF-8-encoded JSON byte string, returns a Perl data | ||
| 55 | structure. | ||
| 56 | |||
| 57 | =cut | ||
| 58 | |||
| 59 | sub decode_json_utf8 { | ||
| 60 | 1002 | ▶ | state $json = JSON::MaybeXS->new; |
|
Subroutine
decode_json_utf8: called CC=1 SCAR=0.0
Pod
documented
| |||
| 61 | 1002 | $json->decode(decode_utf8(shift)) | |
| 62 | } | ||
| 63 | |||
| 64 | =head2 encode_json_utf8 | ||
| 65 | |||
| 66 | Given a Perl data structure, returns a UTF-8-encoded JSON | ||
| 67 | byte string. | ||
| 68 | |||
| 69 | =cut | ||
| 70 | |||
| 71 | sub encode_json_utf8 { | ||
| 72 | 1002 | ▶ | state $json = JSON::MaybeXS->new; |
|
Subroutine
encode_json_utf8: called CC=1 SCAR=0.0
Pod
documented
| |||
| 73 | 1002 | encode_utf8($json->encode(shift)) | |
| 74 | } | ||
| 75 | |||
| 76 | =head2 decode_json_text | ||
| 77 | |||
| 78 | Given a JSON string composed of Unicode characters (in | ||
| 79 | Perl's internal encoding), returns a Perl data structure. | ||
| 80 | |||
| 81 | =cut | ||
| 82 | |||
| 83 | sub decode_json_text { | ||
| 84 | 1002 | ▶ | state $json = JSON::MaybeXS->new; |
|
Subroutine
decode_json_text: called CC=1 SCAR=0.0
Pod
documented
| |||
| 85 | 1002 | $json->decode(shift) | |
| 86 | } | ||
| 87 | |||
| 88 | =head2 encode_json_text | ||
| 89 | |||
| 90 | Given a Perl data structure, returns a JSON string composed | ||
| 91 | of Unicode characters (in Perl's internal encoding). | ||
| 92 | |||
| 93 | =cut | ||
| 94 | |||
| 95 | sub encode_json_text { | ||
| 96 | 1002 | ▶ | state $json = JSON::MaybeXS->new; |
|
Subroutine
encode_json_text: called CC=1 SCAR=0.0
Pod
documented
| |||
| 97 | 1002 | $json->encode(shift) | |
| 98 | } | ||
| 99 | |||
| 100 | 3 | 1; | |
| 101 | |||
| 102 | =head1 AUTHOR | ||
| 103 | |||
| 104 | Tom Molesworth <TEAM@cpan.org> | ||
| 105 | |||
| 106 | =head1 LICENSE | ||
| 107 | |||
| 108 | Copyright Tom Molesworth 2017. Licensed under the same terms as Perl itself. | ||
| 109 | |||