line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package JSON::Create; |
2
|
46
|
|
|
46
|
|
3448035
|
use warnings; |
|
46
|
|
|
|
|
554
|
|
|
46
|
|
|
|
|
1686
|
|
3
|
46
|
|
|
46
|
|
304
|
use strict; |
|
46
|
|
|
|
|
86
|
|
|
46
|
|
|
|
|
40834
|
|
4
|
|
|
|
|
|
|
require Exporter; |
5
|
|
|
|
|
|
|
our @ISA = qw(Exporter); |
6
|
|
|
|
|
|
|
our @EXPORT_OK = qw/create_json create_json_strict write_json/; |
7
|
|
|
|
|
|
|
our %EXPORT_TAGS = ( |
8
|
|
|
|
|
|
|
all => \@EXPORT_OK, |
9
|
|
|
|
|
|
|
); |
10
|
|
|
|
|
|
|
our $VERSION = '0.34'; |
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
# Are we running as XS? |
13
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
our $noxs; |
15
|
|
|
|
|
|
|
|
16
|
|
|
|
|
|
|
# The environment variable JSONCreatePP controls whether this runs as |
17
|
|
|
|
|
|
|
# XS or purely Perl. |
18
|
|
|
|
|
|
|
|
19
|
|
|
|
|
|
|
$noxs = $ENV{JSONCreatePP}; |
20
|
|
|
|
|
|
|
|
21
|
|
|
|
|
|
|
# Did the XS load OK? |
22
|
|
|
|
|
|
|
|
23
|
|
|
|
|
|
|
our $xsok; |
24
|
|
|
|
|
|
|
|
25
|
|
|
|
|
|
|
if (! $noxs) { |
26
|
|
|
|
|
|
|
eval { |
27
|
|
|
|
|
|
|
require XSLoader; |
28
|
|
|
|
|
|
|
XSLoader::load ('JSON::Create', $VERSION); |
29
|
|
|
|
|
|
|
$xsok = 1; |
30
|
|
|
|
|
|
|
}; |
31
|
|
|
|
|
|
|
if ($@) { |
32
|
|
|
|
|
|
|
$xsok = 0; |
33
|
|
|
|
|
|
|
} |
34
|
|
|
|
|
|
|
} |
35
|
|
|
|
|
|
|
|
36
|
|
|
|
|
|
|
if (! $xsok || $noxs) { |
37
|
|
|
|
|
|
|
require JSON::Create::PP; |
38
|
|
|
|
|
|
|
JSON::Create::PP->import (':all'); |
39
|
|
|
|
|
|
|
} |
40
|
|
|
|
|
|
|
|
41
|
|
|
|
|
|
|
sub set_fformat |
42
|
|
|
|
|
|
|
{ |
43
|
8
|
|
|
8
|
1
|
1994
|
my ($obj, $fformat) = @_; |
44
|
8
|
50
|
|
|
|
23
|
if (! $fformat) { |
45
|
0
|
|
|
|
|
0
|
$obj->set_fformat_unsafe (0); |
46
|
0
|
|
|
|
|
0
|
return; |
47
|
|
|
|
|
|
|
} |
48
|
8
|
100
|
|
|
|
48
|
if ($fformat =~ /^%(?:(?:([0-9]+)?(?:\.([0-9]+)?)?)?[fFgGeE])$/) { |
49
|
6
|
|
|
|
|
16
|
my $d1 = $1; |
50
|
6
|
|
|
|
|
15
|
my $d2 = $2; |
51
|
6
|
100
|
66
|
|
|
55
|
if ((defined ($d1) && $d1 > 20) || (defined ($d2) && $d2 > 20)) { |
|
|
|
100
|
|
|
|
|
|
|
|
66
|
|
|
|
|
52
|
2
|
|
|
|
|
28
|
warn "Format $fformat is too long"; |
53
|
2
|
|
|
|
|
23
|
$obj->set_fformat_unsafe (0); |
54
|
|
|
|
|
|
|
} |
55
|
|
|
|
|
|
|
else { |
56
|
4
|
|
|
|
|
20
|
$obj->set_fformat_unsafe ($fformat); |
57
|
|
|
|
|
|
|
} |
58
|
|
|
|
|
|
|
} |
59
|
|
|
|
|
|
|
else { |
60
|
2
|
|
|
|
|
39
|
warn "Format $fformat is not OK for floating point numbers"; |
61
|
2
|
|
|
|
|
21
|
$obj->set_fformat_unsafe (0); |
62
|
|
|
|
|
|
|
} |
63
|
|
|
|
|
|
|
} |
64
|
|
|
|
|
|
|
|
65
|
|
|
|
|
|
|
sub bool |
66
|
|
|
|
|
|
|
{ |
67
|
4
|
|
|
4
|
1
|
2862
|
my ($obj, @list) = @_; |
68
|
4
|
|
|
|
|
21
|
my $handlers = $obj->get_handlers (); |
69
|
4
|
|
|
|
|
13
|
for my $item (@list) { |
70
|
4
|
|
|
|
|
14
|
$handlers->{$item} = 'bool'; |
71
|
|
|
|
|
|
|
} |
72
|
|
|
|
|
|
|
} |
73
|
|
|
|
|
|
|
|
74
|
|
|
|
|
|
|
sub obj |
75
|
|
|
|
|
|
|
{ |
76
|
4
|
|
|
4
|
1
|
1924
|
my ($obj, %handlers) = @_; |
77
|
4
|
|
|
|
|
24
|
my $handlers = $obj->get_handlers (); |
78
|
4
|
|
|
|
|
15
|
for my $item (keys %handlers) { |
79
|
5
|
|
|
|
|
11
|
my $value = $handlers{$item}; |
80
|
|
|
|
|
|
|
# Check it's a code reference somehow. |
81
|
5
|
|
|
|
|
19
|
$handlers->{$item} = $value; |
82
|
|
|
|
|
|
|
} |
83
|
|
|
|
|
|
|
} |
84
|
|
|
|
|
|
|
|
85
|
|
|
|
|
|
|
sub del |
86
|
|
|
|
|
|
|
{ |
87
|
0
|
|
|
0
|
0
|
0
|
my ($obj, @list) = @_; |
88
|
0
|
|
|
|
|
0
|
my $handlers = $obj->get_handlers (); |
89
|
0
|
|
|
|
|
0
|
for my $item (@list) { |
90
|
0
|
|
|
|
|
0
|
delete $handlers->{$item}; |
91
|
|
|
|
|
|
|
} |
92
|
|
|
|
|
|
|
} |
93
|
|
|
|
|
|
|
|
94
|
|
|
|
|
|
|
sub validate |
95
|
|
|
|
|
|
|
{ |
96
|
2
|
|
|
2
|
1
|
1258
|
my ($obj, $value) = @_; |
97
|
2
|
50
|
|
|
|
10
|
if ($value) { |
98
|
2
|
|
|
|
|
1306
|
require JSON::Parse; |
99
|
2
|
|
|
|
|
3098
|
JSON::Parse->import ('assert_valid_json'); |
100
|
|
|
|
|
|
|
} |
101
|
2
|
|
|
|
|
19
|
$obj->set_validate ($value); |
102
|
|
|
|
|
|
|
} |
103
|
|
|
|
|
|
|
|
104
|
|
|
|
|
|
|
sub new |
105
|
|
|
|
|
|
|
{ |
106
|
48
|
|
|
48
|
1
|
341546
|
my ($class, %args) = @_; |
107
|
48
|
|
|
|
|
104
|
my $jc; |
108
|
48
|
100
|
|
|
|
169
|
if ($xsok) { |
109
|
24
|
|
|
|
|
437
|
$jc = bless jcnew (), $class; |
110
|
|
|
|
|
|
|
} |
111
|
|
|
|
|
|
|
else { |
112
|
24
|
|
|
|
|
154
|
$jc = JSON::Create::PP->new (); |
113
|
|
|
|
|
|
|
} |
114
|
48
|
|
|
|
|
340
|
$jc->set (%args); |
115
|
48
|
|
|
|
|
188
|
return $jc; |
116
|
|
|
|
|
|
|
} |
117
|
|
|
|
|
|
|
|
118
|
|
|
|
|
|
|
sub write_json |
119
|
|
|
|
|
|
|
{ |
120
|
2
|
|
|
2
|
1
|
1621
|
my ($filename, $obj, %options) = @_; |
121
|
2
|
|
|
|
|
66
|
my $json = create_json ($obj, %options); |
122
|
|
|
|
|
|
|
# create_json's output is either ASCII or it is marked as utf8, so |
123
|
|
|
|
|
|
|
# the following is always safe. |
124
|
2
|
|
|
|
|
7
|
my $encoding = ':encoding(utf8)'; |
125
|
2
|
50
|
|
|
|
8
|
if ($options{downgrade_utf8}) { |
126
|
0
|
|
|
|
|
0
|
$encoding = ':raw'; |
127
|
|
|
|
|
|
|
} |
128
|
2
|
50
|
|
|
|
159
|
open my $out, ">$encoding", $filename or die $!; |
129
|
2
|
|
|
|
|
183
|
print $out $json; |
130
|
2
|
50
|
|
|
|
166
|
close $out or die $!; |
131
|
|
|
|
|
|
|
} |
132
|
|
|
|
|
|
|
|
133
|
|
|
|
|
|
|
sub run |
134
|
|
|
|
|
|
|
{ |
135
|
0
|
|
|
0
|
1
|
|
goto &create; |
136
|
|
|
|
|
|
|
} |
137
|
|
|
|
|
|
|
|
138
|
|
|
|
|
|
|
1; |