line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package JSON::MaybeUTF8; |
2
|
|
|
|
|
|
|
# ABSTRACT: Simple wrapper for explicit JSON Unicode text/UTF-8 byte functions |
3
|
|
|
|
|
|
|
|
4
|
2
|
|
|
2
|
|
153018
|
use strict; |
|
2
|
|
|
|
|
20
|
|
|
2
|
|
|
|
|
47
|
|
5
|
2
|
|
|
2
|
|
8
|
use warnings; |
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
75
|
|
6
|
|
|
|
|
|
|
|
7
|
|
|
|
|
|
|
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 with L 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
|
2
|
|
|
2
|
|
9
|
use feature qw(state); |
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
242
|
|
36
|
|
|
|
|
|
|
|
37
|
2
|
|
|
2
|
|
736
|
use JSON::MaybeXS; |
|
2
|
|
|
|
|
9645
|
|
|
2
|
|
|
|
|
98
|
|
38
|
2
|
|
|
2
|
|
767
|
use Unicode::UTF8 qw(encode_utf8 decode_utf8); |
|
2
|
|
|
|
|
688
|
|
|
2
|
|
|
|
|
97
|
|
39
|
|
|
|
|
|
|
|
40
|
2
|
|
|
2
|
|
11
|
use Exporter qw(import export_to_level); |
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
636
|
|
41
|
|
|
|
|
|
|
|
42
|
|
|
|
|
|
|
=head2 BOM removal |
43
|
|
|
|
|
|
|
|
44
|
|
|
|
|
|
|
The C<< $JSON::Maybe::UTF8::REMOVE_BOM >> flag is B due |
45
|
|
|
|
|
|
|
to L. 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 is used (preferred by L |
50
|
|
|
|
|
|
|
if it can be loaded). |
51
|
|
|
|
|
|
|
|
52
|
|
|
|
|
|
|
=cut |
53
|
|
|
|
|
|
|
|
54
|
|
|
|
|
|
|
our $REMOVE_BOM = 1; |
55
|
|
|
|
|
|
|
|
56
|
|
|
|
|
|
|
our @EXPORT_OK = qw( |
57
|
|
|
|
|
|
|
decode_json_utf8 |
58
|
|
|
|
|
|
|
encode_json_utf8 |
59
|
|
|
|
|
|
|
decode_json_text |
60
|
|
|
|
|
|
|
encode_json_text |
61
|
|
|
|
|
|
|
); |
62
|
|
|
|
|
|
|
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 |
70
|
|
|
|
|
|
|
if it exists. |
71
|
|
|
|
|
|
|
|
72
|
|
|
|
|
|
|
=cut |
73
|
|
|
|
|
|
|
|
74
|
|
|
|
|
|
|
sub decode_json_utf8 { |
75
|
1004
|
|
|
1004
|
1
|
2498
|
state $json = JSON::MaybeXS->new; |
76
|
1004
|
50
|
|
|
|
2068
|
die 'bad json state' if $json->get_utf8; |
77
|
1004
|
50
|
|
|
|
1838
|
return $json->decode_utf8($_[0]) unless $REMOVE_BOM; |
78
|
1004
|
|
|
|
|
2932
|
(my $txt = decode_utf8(shift)) =~ s{^\x{feff}}{}; |
79
|
1004
|
|
|
|
|
3352
|
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
|
|
|
1004
|
1
|
2742
|
state $json = JSON::MaybeXS->new; |
91
|
1004
|
50
|
|
|
|
2487
|
die 'bad json state' if $json->get_utf8; |
92
|
1004
|
|
|
|
|
4138
|
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
|
|
|
1004
|
1
|
4685
|
state $json = JSON::MaybeXS->new; |
104
|
1004
|
50
|
|
|
|
2123
|
die 'bad json state' if $json->get_utf8; |
105
|
1004
|
|
|
|
|
1274
|
my $txt = shift; |
106
|
1004
|
50
|
|
|
|
2727
|
$txt =~ s{^\x{feff}}{} if $REMOVE_BOM; |
107
|
1004
|
|
|
|
|
4713
|
$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
|
|
|
1004
|
1
|
3421527
|
state $json = JSON::MaybeXS->new; |
119
|
1004
|
50
|
|
|
|
3362
|
die 'bad json state' if $json->get_utf8; |
120
|
1004
|
|
|
|
|
5395
|
$json->encode(shift) |
121
|
|
|
|
|
|
|
} |
122
|
|
|
|
|
|
|
|
123
|
|
|
|
|
|
|
1; |
124
|
|
|
|
|
|
|
|
125
|
|
|
|
|
|
|
=head1 AUTHOR |
126
|
|
|
|
|
|
|
|
127
|
|
|
|
|
|
|
Tom Molesworth |
128
|
|
|
|
|
|
|
|
129
|
|
|
|
|
|
|
=head1 LICENSE |
130
|
|
|
|
|
|
|
|
131
|
|
|
|
|
|
|
Copyright Tom Molesworth 2017. Licensed under the same terms as Perl itself. |
132
|
|
|
|
|
|
|
|