line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package JSON::Create; |
2
|
48
|
|
|
48
|
|
3675589
|
use warnings; |
|
48
|
|
|
|
|
578
|
|
|
48
|
|
|
|
|
1800
|
|
3
|
48
|
|
|
48
|
|
283
|
use strict; |
|
48
|
|
|
|
|
114
|
|
|
48
|
|
|
|
|
43584
|
|
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_01'; |
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
|
1901
|
my ($obj, $fformat) = @_; |
44
|
8
|
50
|
|
|
|
26
|
if (! $fformat) { |
45
|
0
|
|
|
|
|
0
|
$obj->set_fformat_unsafe (0); |
46
|
0
|
|
|
|
|
0
|
return; |
47
|
|
|
|
|
|
|
} |
48
|
8
|
100
|
|
|
|
60
|
if ($fformat =~ /^%(?:(?:([0-9]+)?(?:\.([0-9]+)?)?)?[fFgGeE])$/) { |
49
|
6
|
|
|
|
|
23
|
my $d1 = $1; |
50
|
6
|
|
|
|
|
14
|
my $d2 = $2; |
51
|
6
|
100
|
66
|
|
|
68
|
if ((defined ($d1) && $d1 > 20) || (defined ($d2) && $d2 > 20)) { |
|
|
|
100
|
|
|
|
|
|
|
|
66
|
|
|
|
|
52
|
2
|
|
|
|
|
34
|
warn "Format $fformat is too long"; |
53
|
2
|
|
|
|
|
23
|
$obj->set_fformat_unsafe (0); |
54
|
|
|
|
|
|
|
} |
55
|
|
|
|
|
|
|
else { |
56
|
4
|
|
|
|
|
23
|
$obj->set_fformat_unsafe ($fformat); |
57
|
|
|
|
|
|
|
} |
58
|
|
|
|
|
|
|
} |
59
|
|
|
|
|
|
|
else { |
60
|
2
|
|
|
|
|
39
|
warn "Format $fformat is not OK for floating point numbers"; |
61
|
2
|
|
|
|
|
20
|
$obj->set_fformat_unsafe (0); |
62
|
|
|
|
|
|
|
} |
63
|
|
|
|
|
|
|
} |
64
|
|
|
|
|
|
|
|
65
|
|
|
|
|
|
|
sub bool |
66
|
|
|
|
|
|
|
{ |
67
|
4
|
|
|
4
|
1
|
2716
|
my ($obj, @list) = @_; |
68
|
4
|
|
|
|
|
20
|
my $handlers = $obj->get_handlers (); |
69
|
4
|
|
|
|
|
10
|
for my $item (@list) { |
70
|
4
|
|
|
|
|
17
|
$handlers->{$item} = 'bool'; |
71
|
|
|
|
|
|
|
} |
72
|
|
|
|
|
|
|
} |
73
|
|
|
|
|
|
|
|
74
|
|
|
|
|
|
|
sub obj |
75
|
|
|
|
|
|
|
{ |
76
|
4
|
|
|
4
|
1
|
1456
|
my ($obj, %handlers) = @_; |
77
|
4
|
|
|
|
|
17
|
my $handlers = $obj->get_handlers (); |
78
|
4
|
|
|
|
|
14
|
for my $item (keys %handlers) { |
79
|
5
|
|
|
|
|
9
|
my $value = $handlers{$item}; |
80
|
|
|
|
|
|
|
# Check it's a code reference somehow. |
81
|
5
|
|
|
|
|
16
|
$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
|
1148
|
my ($obj, $value) = @_; |
97
|
2
|
50
|
|
|
|
9
|
if ($value) { |
98
|
2
|
|
|
|
|
975
|
require JSON::Parse; |
99
|
2
|
|
|
|
|
2719
|
JSON::Parse->import ('assert_valid_json'); |
100
|
|
|
|
|
|
|
} |
101
|
2
|
|
|
|
|
45
|
$obj->set_validate ($value); |
102
|
|
|
|
|
|
|
} |
103
|
|
|
|
|
|
|
|
104
|
|
|
|
|
|
|
sub new |
105
|
|
|
|
|
|
|
{ |
106
|
48
|
|
|
48
|
1
|
344758
|
my ($class, %args) = @_; |
107
|
48
|
|
|
|
|
107
|
my $jc; |
108
|
48
|
100
|
|
|
|
164
|
if ($xsok) { |
109
|
24
|
|
|
|
|
446
|
$jc = bless jcnew (), $class; |
110
|
|
|
|
|
|
|
} |
111
|
|
|
|
|
|
|
else { |
112
|
24
|
|
|
|
|
140
|
$jc = JSON::Create::PP->new (); |
113
|
|
|
|
|
|
|
} |
114
|
48
|
|
|
|
|
305
|
$jc->set (%args); |
115
|
48
|
|
|
|
|
165
|
return $jc; |
116
|
|
|
|
|
|
|
} |
117
|
|
|
|
|
|
|
|
118
|
|
|
|
|
|
|
sub write_json |
119
|
|
|
|
|
|
|
{ |
120
|
2
|
|
|
2
|
1
|
1684
|
my ($filename, $obj, %options) = @_; |
121
|
2
|
|
|
|
|
59
|
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
|
|
|
|
|
8
|
my $encoding = ':encoding(utf8)'; |
125
|
2
|
50
|
|
|
|
8
|
if ($options{downgrade_utf8}) { |
126
|
0
|
|
|
|
|
0
|
$encoding = ':raw'; |
127
|
|
|
|
|
|
|
} |
128
|
2
|
50
|
|
|
|
160
|
open my $out, ">$encoding", $filename or die $!; |
129
|
2
|
|
|
|
|
181
|
print $out $json; |
130
|
2
|
50
|
|
|
|
167
|
close $out or die $!; |
131
|
|
|
|
|
|
|
} |
132
|
|
|
|
|
|
|
|
133
|
|
|
|
|
|
|
sub run |
134
|
|
|
|
|
|
|
{ |
135
|
0
|
|
|
0
|
1
|
|
goto &create; |
136
|
|
|
|
|
|
|
} |
137
|
|
|
|
|
|
|
|
138
|
|
|
|
|
|
|
1; |