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
|
|
154319
|
use strict; |
|
2
|
|
|
|
|
20
|
|
|
2
|
|
|
|
|
54
|
|
5
|
2
|
|
|
2
|
|
8
|
use warnings; |
|
2
|
|
|
|
|
2
|
|
|
2
|
|
|
|
|
88
|
|
6
|
|
|
|
|
|
|
|
7
|
|
|
|
|
|
|
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 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
|
|
10
|
use feature qw(state); |
|
2
|
|
|
|
|
2
|
|
|
2
|
|
|
|
|
228
|
|
36
|
|
|
|
|
|
|
|
37
|
2
|
|
|
2
|
|
953
|
use JSON::MaybeXS; |
|
2
|
|
|
|
|
9468
|
|
|
2
|
|
|
|
|
108
|
|
38
|
2
|
|
|
2
|
|
704
|
use Unicode::UTF8 qw(encode_utf8 decode_utf8); |
|
2
|
|
|
|
|
770
|
|
|
2
|
|
|
|
|
98
|
|
39
|
|
|
|
|
|
|
|
40
|
2
|
|
|
2
|
|
12
|
use Exporter qw(import export_to_level); |
|
2
|
|
|
|
|
5
|
|
|
2
|
|
|
|
|
398
|
|
41
|
|
|
|
|
|
|
|
42
|
|
|
|
|
|
|
our @EXPORT_OK = qw( |
43
|
|
|
|
|
|
|
decode_json_utf8 |
44
|
|
|
|
|
|
|
encode_json_utf8 |
45
|
|
|
|
|
|
|
decode_json_text |
46
|
|
|
|
|
|
|
encode_json_text |
47
|
|
|
|
|
|
|
); |
48
|
|
|
|
|
|
|
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
|
|
|
1002
|
1
|
2523
|
state $json = JSON::MaybeXS->new; |
61
|
1002
|
|
|
|
|
4266
|
$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
|
|
|
1002
|
1
|
2729
|
state $json = JSON::MaybeXS->new; |
73
|
1002
|
|
|
|
|
4178
|
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
|
|
|
1002
|
1
|
3350
|
state $json = JSON::MaybeXS->new; |
85
|
1002
|
|
|
|
|
4408
|
$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
|
|
|
1002
|
1
|
3322538
|
state $json = JSON::MaybeXS->new; |
97
|
1002
|
|
|
|
|
5707
|
$json->encode(shift) |
98
|
|
|
|
|
|
|
} |
99
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
1; |
101
|
|
|
|
|
|
|
|
102
|
|
|
|
|
|
|
=head1 AUTHOR |
103
|
|
|
|
|
|
|
|
104
|
|
|
|
|
|
|
Tom Molesworth |
105
|
|
|
|
|
|
|
|
106
|
|
|
|
|
|
|
=head1 LICENSE |
107
|
|
|
|
|
|
|
|
108
|
|
|
|
|
|
|
Copyright Tom Molesworth 2017. Licensed under the same terms as Perl itself. |
109
|
|
|
|
|
|
|
|