line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
35
|
|
|
35
|
|
247
|
use strict; |
|
35
|
|
|
|
|
72
|
|
|
35
|
|
|
|
|
1103
|
|
2
|
35
|
|
|
35
|
|
186
|
use warnings; |
|
35
|
|
|
|
|
111
|
|
|
35
|
|
|
|
|
1772
|
|
3
|
|
|
|
|
|
|
package YAML::PP::Schema::JSON; |
4
|
|
|
|
|
|
|
|
5
|
|
|
|
|
|
|
our $VERSION = '0.036_001'; # TRIAL VERSION |
6
|
|
|
|
|
|
|
|
7
|
35
|
|
|
35
|
|
216
|
use base 'Exporter'; |
|
35
|
|
|
|
|
89
|
|
|
35
|
|
|
|
|
4688
|
|
8
|
|
|
|
|
|
|
our @EXPORT_OK = qw/ |
9
|
|
|
|
|
|
|
represent_int represent_float represent_literal represent_bool |
10
|
|
|
|
|
|
|
represent_undef |
11
|
|
|
|
|
|
|
/; |
12
|
|
|
|
|
|
|
|
13
|
35
|
|
|
35
|
|
279
|
use B; |
|
35
|
|
|
|
|
84
|
|
|
35
|
|
|
|
|
1567
|
|
14
|
35
|
|
|
35
|
|
271
|
use Carp qw/ croak /; |
|
35
|
|
|
|
|
107
|
|
|
35
|
|
|
|
|
2422
|
|
15
|
|
|
|
|
|
|
|
16
|
35
|
|
|
35
|
|
256
|
use YAML::PP::Common qw/ YAML_PLAIN_SCALAR_STYLE YAML_SINGLE_QUOTED_SCALAR_STYLE /; |
|
35
|
|
|
|
|
78
|
|
|
35
|
|
|
|
|
40508
|
|
17
|
|
|
|
|
|
|
|
18
|
|
|
|
|
|
|
my $RE_INT = qr{^(-?(?:0|[1-9][0-9]*))$}; |
19
|
|
|
|
|
|
|
my $RE_FLOAT = qr{^(-?(?:0|[1-9][0-9]*)(?:\.[0-9]*)?(?:[eE][+-]?[0-9]+)?)$}; |
20
|
|
|
|
|
|
|
|
21
|
382
|
|
|
382
|
|
2061
|
sub _to_int { 0 + $_[2]->[0] } |
22
|
|
|
|
|
|
|
|
23
|
|
|
|
|
|
|
# DaTa++ && shmem++ |
24
|
76
|
|
|
76
|
|
700
|
sub _to_float { unpack F => pack F => $_[2]->[0] } |
25
|
|
|
|
|
|
|
|
26
|
|
|
|
|
|
|
sub register { |
27
|
22
|
|
|
22
|
1
|
82
|
my ($self, %args) = @_; |
28
|
22
|
|
|
|
|
43
|
my $schema = $args{schema}; |
29
|
22
|
|
|
|
|
73
|
my $options = $args{options}; |
30
|
22
|
|
|
|
|
36
|
my $empty_null = 0; |
31
|
22
|
|
|
|
|
58
|
for my $opt (@$options) { |
32
|
2
|
50
|
|
|
|
8
|
if ($opt eq 'empty=str') { |
|
|
100
|
|
|
|
|
|
33
|
|
|
|
|
|
|
} |
34
|
|
|
|
|
|
|
elsif ($opt eq 'empty=null') { |
35
|
1
|
|
|
|
|
2
|
$empty_null = 1; |
36
|
|
|
|
|
|
|
} |
37
|
|
|
|
|
|
|
else { |
38
|
1
|
|
|
|
|
280
|
croak "Invalid option for JSON Schema: '$opt'"; |
39
|
|
|
|
|
|
|
} |
40
|
|
|
|
|
|
|
} |
41
|
|
|
|
|
|
|
|
42
|
|
|
|
|
|
|
$schema->add_resolver( |
43
|
21
|
|
|
|
|
95
|
tag => 'tag:yaml.org,2002:null', |
44
|
|
|
|
|
|
|
match => [ equals => null => undef ], |
45
|
|
|
|
|
|
|
); |
46
|
21
|
100
|
|
|
|
74
|
if ($empty_null) { |
47
|
1
|
|
|
|
|
9
|
$schema->add_resolver( |
48
|
|
|
|
|
|
|
tag => 'tag:yaml.org,2002:null', |
49
|
|
|
|
|
|
|
match => [ equals => '' => undef ], |
50
|
|
|
|
|
|
|
implicit => 1, |
51
|
|
|
|
|
|
|
); |
52
|
|
|
|
|
|
|
} |
53
|
|
|
|
|
|
|
else { |
54
|
20
|
|
|
|
|
80
|
$schema->add_resolver( |
55
|
|
|
|
|
|
|
tag => 'tag:yaml.org,2002:str', |
56
|
|
|
|
|
|
|
match => [ equals => '' => '' ], |
57
|
|
|
|
|
|
|
implicit => 1, |
58
|
|
|
|
|
|
|
); |
59
|
|
|
|
|
|
|
} |
60
|
21
|
|
|
|
|
75
|
$schema->add_resolver( |
61
|
|
|
|
|
|
|
tag => 'tag:yaml.org,2002:bool', |
62
|
|
|
|
|
|
|
match => [ equals => true => $schema->true ], |
63
|
|
|
|
|
|
|
); |
64
|
21
|
|
|
|
|
91
|
$schema->add_resolver( |
65
|
|
|
|
|
|
|
tag => 'tag:yaml.org,2002:bool', |
66
|
|
|
|
|
|
|
match => [ equals => false => $schema->false ], |
67
|
|
|
|
|
|
|
); |
68
|
21
|
|
|
|
|
96
|
$schema->add_resolver( |
69
|
|
|
|
|
|
|
tag => 'tag:yaml.org,2002:int', |
70
|
|
|
|
|
|
|
match => [ regex => $RE_INT => \&_to_int ], |
71
|
|
|
|
|
|
|
); |
72
|
21
|
|
|
|
|
106
|
$schema->add_resolver( |
73
|
|
|
|
|
|
|
tag => 'tag:yaml.org,2002:float', |
74
|
|
|
|
|
|
|
match => [ regex => $RE_FLOAT => \&_to_float ], |
75
|
|
|
|
|
|
|
); |
76
|
|
|
|
|
|
|
$schema->add_resolver( |
77
|
|
|
|
|
|
|
tag => 'tag:yaml.org,2002:str', |
78
|
21
|
|
|
338
|
|
164
|
match => [ all => sub { $_[1]->{value} } ], |
|
338
|
|
|
|
|
1291
|
|
79
|
|
|
|
|
|
|
); |
80
|
|
|
|
|
|
|
|
81
|
21
|
|
|
|
|
94
|
$schema->add_representer( |
82
|
|
|
|
|
|
|
undefined => \&represent_undef, |
83
|
|
|
|
|
|
|
); |
84
|
|
|
|
|
|
|
|
85
|
21
|
|
|
|
|
35
|
my $int_flags = B::SVp_IOK; |
86
|
21
|
|
|
|
|
35
|
my $float_flags = B::SVp_NOK; |
87
|
21
|
|
|
|
|
73
|
$schema->add_representer( |
88
|
|
|
|
|
|
|
flags => $int_flags, |
89
|
|
|
|
|
|
|
code => \&represent_int, |
90
|
|
|
|
|
|
|
); |
91
|
21
|
|
|
|
|
78
|
my %special = ( (0+'nan').'' => '.nan', (0+'inf').'' => '.inf', (0-'inf').'' => '-.inf' ); |
92
|
21
|
|
|
|
|
68
|
$schema->add_representer( |
93
|
|
|
|
|
|
|
flags => $float_flags, |
94
|
|
|
|
|
|
|
code => \&represent_float, |
95
|
|
|
|
|
|
|
); |
96
|
|
|
|
|
|
|
$schema->add_representer( |
97
|
|
|
|
|
|
|
equals => $_, |
98
|
|
|
|
|
|
|
code => \&represent_literal, |
99
|
21
|
|
|
|
|
97
|
) for ("", qw/ true false null /); |
100
|
21
|
|
|
|
|
458
|
$schema->add_representer( |
101
|
|
|
|
|
|
|
regex => qr{$RE_INT|$RE_FLOAT}, |
102
|
|
|
|
|
|
|
code => \&represent_literal, |
103
|
|
|
|
|
|
|
); |
104
|
|
|
|
|
|
|
|
105
|
21
|
100
|
|
|
|
70
|
if ($schema->bool_class) { |
106
|
2
|
|
|
|
|
4
|
for my $class (@{ $schema->bool_class }) { |
|
2
|
|
|
|
|
7
|
|
107
|
2
|
|
|
|
|
6
|
$schema->add_representer( |
108
|
|
|
|
|
|
|
class_equals => $class, |
109
|
|
|
|
|
|
|
code => \&represent_bool, |
110
|
|
|
|
|
|
|
); |
111
|
|
|
|
|
|
|
} |
112
|
|
|
|
|
|
|
} |
113
|
|
|
|
|
|
|
|
114
|
21
|
|
|
|
|
104
|
return; |
115
|
|
|
|
|
|
|
} |
116
|
|
|
|
|
|
|
|
117
|
|
|
|
|
|
|
sub represent_undef { |
118
|
110
|
|
|
110
|
1
|
232
|
my ($rep, $node) = @_; |
119
|
110
|
|
|
|
|
250
|
$node->{style} = YAML_PLAIN_SCALAR_STYLE; |
120
|
110
|
|
|
|
|
233
|
$node->{data} = 'null'; |
121
|
110
|
|
|
|
|
373
|
return 1; |
122
|
|
|
|
|
|
|
} |
123
|
|
|
|
|
|
|
|
124
|
|
|
|
|
|
|
sub represent_literal { |
125
|
218
|
|
|
218
|
1
|
419
|
my ($rep, $node) = @_; |
126
|
218
|
|
100
|
|
|
1193
|
$node->{style} ||= YAML_SINGLE_QUOTED_SCALAR_STYLE; |
127
|
218
|
|
|
|
|
494
|
$node->{data} = "$node->{value}"; |
128
|
218
|
|
|
|
|
754
|
return 1; |
129
|
|
|
|
|
|
|
} |
130
|
|
|
|
|
|
|
|
131
|
|
|
|
|
|
|
|
132
|
|
|
|
|
|
|
sub represent_int { |
133
|
384
|
|
|
384
|
1
|
716
|
my ($rep, $node) = @_; |
134
|
384
|
100
|
|
|
|
1231
|
if (int($node->{value}) ne $node->{value}) { |
135
|
6
|
|
|
|
|
22
|
return 0; |
136
|
|
|
|
|
|
|
} |
137
|
378
|
|
|
|
|
804
|
$node->{style} = YAML_PLAIN_SCALAR_STYLE; |
138
|
378
|
|
|
|
|
814
|
$node->{data} = "$node->{value}"; |
139
|
378
|
|
|
|
|
1344
|
return 1; |
140
|
|
|
|
|
|
|
} |
141
|
|
|
|
|
|
|
|
142
|
|
|
|
|
|
|
my %special = ( |
143
|
|
|
|
|
|
|
(0+'nan').'' => '.nan', |
144
|
|
|
|
|
|
|
(0+'inf').'' => '.inf', |
145
|
|
|
|
|
|
|
(0-'inf').'' => '-.inf' |
146
|
|
|
|
|
|
|
); |
147
|
|
|
|
|
|
|
sub represent_float { |
148
|
130
|
|
|
130
|
1
|
266
|
my ($rep, $node) = @_; |
149
|
130
|
100
|
|
|
|
724
|
if (exists $special{ $node->{value} }) { |
150
|
48
|
|
|
|
|
98
|
$node->{style} = YAML_PLAIN_SCALAR_STYLE; |
151
|
48
|
|
|
|
|
108
|
$node->{data} = $special{ $node->{value} }; |
152
|
48
|
|
|
|
|
176
|
return 1; |
153
|
|
|
|
|
|
|
} |
154
|
82
|
100
|
|
|
|
395
|
if (0.0 + $node->{value} ne $node->{value}) { |
155
|
3
|
|
|
|
|
11
|
return 0; |
156
|
|
|
|
|
|
|
} |
157
|
79
|
100
|
66
|
|
|
423
|
if (int($node->{value}) eq $node->{value} and not $node->{value} =~ m/\./) { |
158
|
35
|
|
|
|
|
77
|
$node->{value} .= '.0'; |
159
|
|
|
|
|
|
|
} |
160
|
79
|
|
|
|
|
209
|
$node->{style} = YAML_PLAIN_SCALAR_STYLE; |
161
|
79
|
|
|
|
|
223
|
$node->{data} = "$node->{value}"; |
162
|
79
|
|
|
|
|
316
|
return 1; |
163
|
|
|
|
|
|
|
} |
164
|
|
|
|
|
|
|
|
165
|
|
|
|
|
|
|
sub represent_bool { |
166
|
64
|
|
|
64
|
1
|
130
|
my ($rep, $node) = @_; |
167
|
64
|
100
|
|
|
|
315
|
my $string = $node->{value} ? 'true' : 'false'; |
168
|
64
|
|
|
|
|
579
|
$node->{style} = YAML_PLAIN_SCALAR_STYLE; |
169
|
64
|
|
|
|
|
127
|
@{ $node->{items} } = $string; |
|
64
|
|
|
|
|
189
|
|
170
|
64
|
|
|
|
|
151
|
$node->{data} = $string; |
171
|
64
|
|
|
|
|
220
|
return 1; |
172
|
|
|
|
|
|
|
} |
173
|
|
|
|
|
|
|
|
174
|
|
|
|
|
|
|
1; |
175
|
|
|
|
|
|
|
|
176
|
|
|
|
|
|
|
__END__ |