line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
35
|
|
|
35
|
|
264
|
use strict; |
|
35
|
|
|
|
|
64
|
|
|
35
|
|
|
|
|
1077
|
|
2
|
35
|
|
|
35
|
|
180
|
use warnings; |
|
35
|
|
|
|
|
123
|
|
|
35
|
|
|
|
|
1796
|
|
3
|
|
|
|
|
|
|
package YAML::PP::Schema::JSON; |
4
|
|
|
|
|
|
|
|
5
|
|
|
|
|
|
|
our $VERSION = '0.036'; # VERSION |
6
|
|
|
|
|
|
|
|
7
|
35
|
|
|
35
|
|
210
|
use base 'Exporter'; |
|
35
|
|
|
|
|
116
|
|
|
35
|
|
|
|
|
4913
|
|
8
|
|
|
|
|
|
|
our @EXPORT_OK = qw/ |
9
|
|
|
|
|
|
|
represent_int represent_float represent_literal represent_bool |
10
|
|
|
|
|
|
|
represent_undef |
11
|
|
|
|
|
|
|
/; |
12
|
|
|
|
|
|
|
|
13
|
35
|
|
|
35
|
|
264
|
use B; |
|
35
|
|
|
|
|
77
|
|
|
35
|
|
|
|
|
1620
|
|
14
|
35
|
|
|
35
|
|
274
|
use Carp qw/ croak /; |
|
35
|
|
|
|
|
89
|
|
|
35
|
|
|
|
|
2454
|
|
15
|
|
|
|
|
|
|
|
16
|
35
|
|
|
35
|
|
265
|
use YAML::PP::Common qw/ YAML_PLAIN_SCALAR_STYLE YAML_SINGLE_QUOTED_SCALAR_STYLE /; |
|
35
|
|
|
|
|
71
|
|
|
35
|
|
|
|
|
40040
|
|
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
|
168
|
|
|
168
|
|
868
|
sub _to_int { 0 + $_[2]->[0] } |
22
|
|
|
|
|
|
|
|
23
|
|
|
|
|
|
|
# DaTa++ && shmem++ |
24
|
37
|
|
|
37
|
|
331
|
sub _to_float { unpack F => pack F => $_[2]->[0] } |
25
|
|
|
|
|
|
|
|
26
|
|
|
|
|
|
|
sub register { |
27
|
22
|
|
|
22
|
1
|
87
|
my ($self, %args) = @_; |
28
|
22
|
|
|
|
|
52
|
my $schema = $args{schema}; |
29
|
22
|
|
|
|
|
36
|
my $options = $args{options}; |
30
|
22
|
|
|
|
|
35
|
my $empty_null = 0; |
31
|
22
|
|
|
|
|
51
|
for my $opt (@$options) { |
32
|
2
|
50
|
|
|
|
20
|
if ($opt eq 'empty=str') { |
|
|
100
|
|
|
|
|
|
33
|
|
|
|
|
|
|
} |
34
|
|
|
|
|
|
|
elsif ($opt eq 'empty=null') { |
35
|
1
|
|
|
|
|
4
|
$empty_null = 1; |
36
|
|
|
|
|
|
|
} |
37
|
|
|
|
|
|
|
else { |
38
|
1
|
|
|
|
|
273
|
croak "Invalid option for JSON Schema: '$opt'"; |
39
|
|
|
|
|
|
|
} |
40
|
|
|
|
|
|
|
} |
41
|
|
|
|
|
|
|
|
42
|
|
|
|
|
|
|
$schema->add_resolver( |
43
|
21
|
|
|
|
|
87
|
tag => 'tag:yaml.org,2002:null', |
44
|
|
|
|
|
|
|
match => [ equals => null => undef ], |
45
|
|
|
|
|
|
|
); |
46
|
21
|
100
|
|
|
|
70
|
if ($empty_null) { |
47
|
1
|
|
|
|
|
17
|
$schema->add_resolver( |
48
|
|
|
|
|
|
|
tag => 'tag:yaml.org,2002:null', |
49
|
|
|
|
|
|
|
match => [ equals => '' => undef ], |
50
|
|
|
|
|
|
|
implicit => 1, |
51
|
|
|
|
|
|
|
); |
52
|
|
|
|
|
|
|
} |
53
|
|
|
|
|
|
|
else { |
54
|
20
|
|
|
|
|
103
|
$schema->add_resolver( |
55
|
|
|
|
|
|
|
tag => 'tag:yaml.org,2002:str', |
56
|
|
|
|
|
|
|
match => [ equals => '' => '' ], |
57
|
|
|
|
|
|
|
implicit => 1, |
58
|
|
|
|
|
|
|
); |
59
|
|
|
|
|
|
|
} |
60
|
21
|
|
|
|
|
129
|
$schema->add_resolver( |
61
|
|
|
|
|
|
|
tag => 'tag:yaml.org,2002:bool', |
62
|
|
|
|
|
|
|
match => [ equals => true => $schema->true ], |
63
|
|
|
|
|
|
|
); |
64
|
21
|
|
|
|
|
99
|
$schema->add_resolver( |
65
|
|
|
|
|
|
|
tag => 'tag:yaml.org,2002:bool', |
66
|
|
|
|
|
|
|
match => [ equals => false => $schema->false ], |
67
|
|
|
|
|
|
|
); |
68
|
21
|
|
|
|
|
119
|
$schema->add_resolver( |
69
|
|
|
|
|
|
|
tag => 'tag:yaml.org,2002:int', |
70
|
|
|
|
|
|
|
match => [ regex => $RE_INT => \&_to_int ], |
71
|
|
|
|
|
|
|
); |
72
|
21
|
|
|
|
|
99
|
$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
|
|
180
|
match => [ all => sub { $_[1]->{value} } ], |
|
338
|
|
|
|
|
1550
|
|
79
|
|
|
|
|
|
|
); |
80
|
|
|
|
|
|
|
|
81
|
21
|
|
|
|
|
99
|
$schema->add_representer( |
82
|
|
|
|
|
|
|
undefined => \&represent_undef, |
83
|
|
|
|
|
|
|
); |
84
|
|
|
|
|
|
|
|
85
|
21
|
|
|
|
|
39
|
my $int_flags = B::SVp_IOK; |
86
|
21
|
|
|
|
|
40
|
my $float_flags = B::SVp_NOK; |
87
|
21
|
|
|
|
|
76
|
$schema->add_representer( |
88
|
|
|
|
|
|
|
flags => $int_flags, |
89
|
|
|
|
|
|
|
code => \&represent_int, |
90
|
|
|
|
|
|
|
); |
91
|
21
|
|
|
|
|
82
|
my %special = ( (0+'nan').'' => '.nan', (0+'inf').'' => '.inf', (0-'inf').'' => '-.inf' ); |
92
|
21
|
|
|
|
|
64
|
$schema->add_representer( |
93
|
|
|
|
|
|
|
flags => $float_flags, |
94
|
|
|
|
|
|
|
code => \&represent_float, |
95
|
|
|
|
|
|
|
); |
96
|
|
|
|
|
|
|
$schema->add_representer( |
97
|
|
|
|
|
|
|
equals => $_, |
98
|
|
|
|
|
|
|
code => \&represent_literal, |
99
|
21
|
|
|
|
|
109
|
) for ("", qw/ true false null /); |
100
|
21
|
|
|
|
|
507
|
$schema->add_representer( |
101
|
|
|
|
|
|
|
regex => qr{$RE_INT|$RE_FLOAT}, |
102
|
|
|
|
|
|
|
code => \&represent_literal, |
103
|
|
|
|
|
|
|
); |
104
|
|
|
|
|
|
|
|
105
|
21
|
100
|
|
|
|
57
|
if ($schema->bool_class) { |
106
|
2
|
|
|
|
|
5
|
for my $class (@{ $schema->bool_class }) { |
|
2
|
|
|
|
|
5
|
|
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
|
60
|
|
|
60
|
1
|
136
|
my ($rep, $node) = @_; |
119
|
60
|
|
|
|
|
129
|
$node->{style} = YAML_PLAIN_SCALAR_STYLE; |
120
|
60
|
|
|
|
|
106
|
$node->{data} = 'null'; |
121
|
60
|
|
|
|
|
196
|
return 1; |
122
|
|
|
|
|
|
|
} |
123
|
|
|
|
|
|
|
|
124
|
|
|
|
|
|
|
sub represent_literal { |
125
|
187
|
|
|
187
|
1
|
406
|
my ($rep, $node) = @_; |
126
|
187
|
|
100
|
|
|
907
|
$node->{style} ||= YAML_SINGLE_QUOTED_SCALAR_STYLE; |
127
|
187
|
|
|
|
|
416
|
$node->{data} = "$node->{value}"; |
128
|
187
|
|
|
|
|
655
|
return 1; |
129
|
|
|
|
|
|
|
} |
130
|
|
|
|
|
|
|
|
131
|
|
|
|
|
|
|
|
132
|
|
|
|
|
|
|
sub represent_int { |
133
|
312
|
|
|
312
|
1
|
634
|
my ($rep, $node) = @_; |
134
|
312
|
100
|
|
|
|
976
|
if (int($node->{value}) ne $node->{value}) { |
135
|
6
|
|
|
|
|
21
|
return 0; |
136
|
|
|
|
|
|
|
} |
137
|
306
|
|
|
|
|
598
|
$node->{style} = YAML_PLAIN_SCALAR_STYLE; |
138
|
306
|
|
|
|
|
608
|
$node->{data} = "$node->{value}"; |
139
|
306
|
|
|
|
|
1027
|
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
|
117
|
|
|
117
|
1
|
212
|
my ($rep, $node) = @_; |
149
|
117
|
100
|
|
|
|
559
|
if (exists $special{ $node->{value} }) { |
150
|
48
|
|
|
|
|
113
|
$node->{style} = YAML_PLAIN_SCALAR_STYLE; |
151
|
48
|
|
|
|
|
112
|
$node->{data} = $special{ $node->{value} }; |
152
|
48
|
|
|
|
|
162
|
return 1; |
153
|
|
|
|
|
|
|
} |
154
|
69
|
100
|
|
|
|
349
|
if (0.0 + $node->{value} ne $node->{value}) { |
155
|
3
|
|
|
|
|
21
|
return 0; |
156
|
|
|
|
|
|
|
} |
157
|
66
|
100
|
66
|
|
|
363
|
if (int($node->{value}) eq $node->{value} and not $node->{value} =~ m/\./) { |
158
|
33
|
|
|
|
|
71
|
$node->{value} .= '.0'; |
159
|
|
|
|
|
|
|
} |
160
|
66
|
|
|
|
|
148
|
$node->{style} = YAML_PLAIN_SCALAR_STYLE; |
161
|
66
|
|
|
|
|
164
|
$node->{data} = "$node->{value}"; |
162
|
66
|
|
|
|
|
234
|
return 1; |
163
|
|
|
|
|
|
|
} |
164
|
|
|
|
|
|
|
|
165
|
|
|
|
|
|
|
sub represent_bool { |
166
|
63
|
|
|
63
|
1
|
137
|
my ($rep, $node) = @_; |
167
|
63
|
100
|
|
|
|
247
|
my $string = $node->{value} ? 'true' : 'false'; |
168
|
63
|
|
|
|
|
559
|
$node->{style} = YAML_PLAIN_SCALAR_STYLE; |
169
|
63
|
|
|
|
|
98
|
@{ $node->{items} } = $string; |
|
63
|
|
|
|
|
175
|
|
170
|
63
|
|
|
|
|
121
|
$node->{data} = $string; |
171
|
63
|
|
|
|
|
213
|
return 1; |
172
|
|
|
|
|
|
|
} |
173
|
|
|
|
|
|
|
|
174
|
|
|
|
|
|
|
1; |
175
|
|
|
|
|
|
|
|
176
|
|
|
|
|
|
|
__END__ |