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
|
|
205241
|
use strict; |
|
2
|
|
|
|
|
24
|
|
|
2
|
|
|
|
|
70
|
|
5
|
2
|
|
|
2
|
|
11
|
use warnings; |
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
114
|
|
6
|
|
|
|
|
|
|
|
7
|
|
|
|
|
|
|
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 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
|
|
11
|
use feature qw(state); |
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
302
|
|
36
|
|
|
|
|
|
|
|
37
|
2
|
|
|
2
|
|
1086
|
use JSON::MaybeXS; |
|
2
|
|
|
|
|
12486
|
|
|
2
|
|
|
|
|
139
|
|
38
|
2
|
|
|
2
|
|
1046
|
use Unicode::UTF8 qw(encode_utf8 decode_utf8); |
|
2
|
|
|
|
|
1016
|
|
|
2
|
|
|
|
|
131
|
|
39
|
|
|
|
|
|
|
|
40
|
2
|
|
|
2
|
|
16
|
use Exporter qw(import export_to_level); |
|
2
|
|
|
|
|
5
|
|
|
2
|
|
|
|
|
1019
|
|
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
|
|
|
|
|
|
|
format_json_text |
62
|
|
|
|
|
|
|
); |
63
|
|
|
|
|
|
|
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 |
77
|
|
|
|
|
|
|
if it exists. |
78
|
|
|
|
|
|
|
|
79
|
|
|
|
|
|
|
=cut |
80
|
|
|
|
|
|
|
|
81
|
|
|
|
|
|
|
sub decode_json_utf8 { |
82
|
1004
|
|
|
1004
|
1
|
4346
|
state $json = JSON::MaybeXS->new; |
83
|
1004
|
50
|
|
|
|
3306
|
die 'bad json state' if $json->get_utf8; |
84
|
1004
|
50
|
|
|
|
2555
|
return $json->decode_utf8($_[0]) unless $REMOVE_BOM; |
85
|
1004
|
|
|
|
|
4284
|
(my $txt = decode_utf8(shift)) =~ s{^\x{feff}}{}; |
86
|
1004
|
|
|
|
|
4775
|
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
|
|
|
|
|
|
|
sub encode_json_utf8 { |
97
|
1004
|
|
|
1004
|
1
|
4341
|
state $json = JSON::MaybeXS->new; |
98
|
1004
|
50
|
|
|
|
3882
|
die 'bad json state' if $json->get_utf8; |
99
|
1004
|
|
|
|
|
6708
|
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
|
|
|
|
|
|
|
sub decode_json_text { |
110
|
1008
|
|
|
1008
|
1
|
3894
|
state $json = JSON::MaybeXS->new; |
111
|
1008
|
50
|
|
|
|
3498
|
die 'bad json state' if $json->get_utf8; |
112
|
1008
|
|
|
|
|
2302
|
my $txt = shift; |
113
|
1008
|
50
|
|
|
|
4815
|
$txt =~ s{^\x{feff}}{} if $REMOVE_BOM; |
114
|
1008
|
|
|
|
|
7960
|
$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
|
|
|
|
|
|
|
sub encode_json_text { |
125
|
1004
|
|
|
1004
|
1
|
4132568
|
state $json = JSON::MaybeXS->new; |
126
|
1004
|
50
|
|
|
|
5932
|
die 'bad json state' if $json->get_utf8; |
127
|
1004
|
|
|
|
|
10049
|
$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, but with |
136
|
|
|
|
|
|
|
indentation to make it readable, and with defined key ordering which |
137
|
|
|
|
|
|
|
should make it easier to C two different data structures. |
138
|
|
|
|
|
|
|
|
139
|
|
|
|
|
|
|
=cut |
140
|
|
|
|
|
|
|
|
141
|
|
|
|
|
|
|
sub format_json_text { |
142
|
4
|
|
|
4
|
0
|
2528
|
state $json = JSON::MaybeXS->new( |
143
|
|
|
|
|
|
|
pretty => 1, |
144
|
|
|
|
|
|
|
canonical => 1, |
145
|
|
|
|
|
|
|
); |
146
|
4
|
50
|
|
|
|
40
|
die 'bad json state' if $json->get_utf8; |
147
|
4
|
|
|
|
|
38
|
$json->encode(shift) |
148
|
|
|
|
|
|
|
} |
149
|
|
|
|
|
|
|
|
150
|
|
|
|
|
|
|
1; |
151
|
|
|
|
|
|
|
|
152
|
|
|
|
|
|
|
=head1 AUTHOR |
153
|
|
|
|
|
|
|
|
154
|
|
|
|
|
|
|
Tom Molesworth |
155
|
|
|
|
|
|
|
|
156
|
|
|
|
|
|
|
=head1 LICENSE |
157
|
|
|
|
|
|
|
|
158
|
|
|
|
|
|
|
Copyright Tom Molesworth 2017-2021. Licensed under the same terms as Perl itself. |
159
|
|
|
|
|
|
|
|