line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
35
|
|
|
35
|
|
638
|
use strict; |
|
35
|
|
|
|
|
80
|
|
|
35
|
|
|
|
|
1102
|
|
2
|
35
|
|
|
35
|
|
188
|
use warnings; |
|
35
|
|
|
|
|
96
|
|
|
35
|
|
|
|
|
1748
|
|
3
|
|
|
|
|
|
|
package YAML::PP::Schema::JSON; |
4
|
|
|
|
|
|
|
|
5
|
|
|
|
|
|
|
our $VERSION = '0.036_002'; # TRIAL VERSION |
6
|
|
|
|
|
|
|
|
7
|
35
|
|
|
35
|
|
217
|
use base 'Exporter'; |
|
35
|
|
|
|
|
81
|
|
|
35
|
|
|
|
|
5037
|
|
8
|
|
|
|
|
|
|
our @EXPORT_OK = qw/ |
9
|
|
|
|
|
|
|
represent_int represent_float represent_literal represent_bool |
10
|
|
|
|
|
|
|
represent_undef |
11
|
|
|
|
|
|
|
/; |
12
|
|
|
|
|
|
|
|
13
|
35
|
|
|
35
|
|
285
|
use B; |
|
35
|
|
|
|
|
87
|
|
|
35
|
|
|
|
|
1583
|
|
14
|
35
|
|
|
35
|
|
277
|
use Carp qw/ croak /; |
|
35
|
|
|
|
|
108
|
|
|
35
|
|
|
|
|
2522
|
|
15
|
|
|
|
|
|
|
|
16
|
35
|
|
|
35
|
|
275
|
use YAML::PP::Common qw/ YAML_PLAIN_SCALAR_STYLE YAML_SINGLE_QUOTED_SCALAR_STYLE /; |
|
35
|
|
|
|
|
81
|
|
|
35
|
|
|
|
|
40522
|
|
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
|
|
2477
|
sub _to_int { 0 + $_[2]->[0] } |
22
|
|
|
|
|
|
|
|
23
|
|
|
|
|
|
|
# DaTa++ && shmem++ |
24
|
76
|
|
|
76
|
|
808
|
sub _to_float { unpack F => pack F => $_[2]->[0] } |
25
|
|
|
|
|
|
|
|
26
|
|
|
|
|
|
|
sub register { |
27
|
22
|
|
|
22
|
1
|
71
|
my ($self, %args) = @_; |
28
|
22
|
|
|
|
|
42
|
my $schema = $args{schema}; |
29
|
22
|
|
|
|
|
43
|
my $options = $args{options}; |
30
|
22
|
|
|
|
|
31
|
my $empty_null = 0; |
31
|
22
|
|
|
|
|
52
|
for my $opt (@$options) { |
32
|
2
|
50
|
|
|
|
10
|
if ($opt eq 'empty=str') { |
|
|
100
|
|
|
|
|
|
33
|
|
|
|
|
|
|
} |
34
|
|
|
|
|
|
|
elsif ($opt eq 'empty=null') { |
35
|
1
|
|
|
|
|
3
|
$empty_null = 1; |
36
|
|
|
|
|
|
|
} |
37
|
|
|
|
|
|
|
else { |
38
|
1
|
|
|
|
|
224
|
croak "Invalid option for JSON Schema: '$opt'"; |
39
|
|
|
|
|
|
|
} |
40
|
|
|
|
|
|
|
} |
41
|
|
|
|
|
|
|
|
42
|
|
|
|
|
|
|
$schema->add_resolver( |
43
|
21
|
|
|
|
|
92
|
tag => 'tag:yaml.org,2002:null', |
44
|
|
|
|
|
|
|
match => [ equals => null => undef ], |
45
|
|
|
|
|
|
|
); |
46
|
21
|
100
|
|
|
|
55
|
if ($empty_null) { |
47
|
1
|
|
|
|
|
5
|
$schema->add_resolver( |
48
|
|
|
|
|
|
|
tag => 'tag:yaml.org,2002:null', |
49
|
|
|
|
|
|
|
match => [ equals => '' => undef ], |
50
|
|
|
|
|
|
|
implicit => 1, |
51
|
|
|
|
|
|
|
); |
52
|
|
|
|
|
|
|
} |
53
|
|
|
|
|
|
|
else { |
54
|
20
|
|
|
|
|
108
|
$schema->add_resolver( |
55
|
|
|
|
|
|
|
tag => 'tag:yaml.org,2002:str', |
56
|
|
|
|
|
|
|
match => [ equals => '' => '' ], |
57
|
|
|
|
|
|
|
implicit => 1, |
58
|
|
|
|
|
|
|
); |
59
|
|
|
|
|
|
|
} |
60
|
21
|
|
|
|
|
72
|
$schema->add_resolver( |
61
|
|
|
|
|
|
|
tag => 'tag:yaml.org,2002:bool', |
62
|
|
|
|
|
|
|
match => [ equals => true => $schema->true ], |
63
|
|
|
|
|
|
|
); |
64
|
21
|
|
|
|
|
70
|
$schema->add_resolver( |
65
|
|
|
|
|
|
|
tag => 'tag:yaml.org,2002:bool', |
66
|
|
|
|
|
|
|
match => [ equals => false => $schema->false ], |
67
|
|
|
|
|
|
|
); |
68
|
21
|
|
|
|
|
84
|
$schema->add_resolver( |
69
|
|
|
|
|
|
|
tag => 'tag:yaml.org,2002:int', |
70
|
|
|
|
|
|
|
match => [ regex => $RE_INT => \&_to_int ], |
71
|
|
|
|
|
|
|
); |
72
|
21
|
|
|
|
|
80
|
$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
|
|
155
|
match => [ all => sub { $_[1]->{value} } ], |
|
338
|
|
|
|
|
1193
|
|
79
|
|
|
|
|
|
|
); |
80
|
|
|
|
|
|
|
|
81
|
21
|
|
|
|
|
79
|
$schema->add_representer( |
82
|
|
|
|
|
|
|
undefined => \&represent_undef, |
83
|
|
|
|
|
|
|
); |
84
|
|
|
|
|
|
|
|
85
|
21
|
|
|
|
|
47
|
my $int_flags = B::SVp_IOK; |
86
|
21
|
|
|
|
|
29
|
my $float_flags = B::SVp_NOK; |
87
|
21
|
|
|
|
|
71
|
$schema->add_representer( |
88
|
|
|
|
|
|
|
flags => $int_flags, |
89
|
|
|
|
|
|
|
code => \&represent_int, |
90
|
|
|
|
|
|
|
); |
91
|
21
|
|
|
|
|
66
|
my %special = ( (0+'nan').'' => '.nan', (0+'inf').'' => '.inf', (0-'inf').'' => '-.inf' ); |
92
|
21
|
|
|
|
|
65
|
$schema->add_representer( |
93
|
|
|
|
|
|
|
flags => $float_flags, |
94
|
|
|
|
|
|
|
code => \&represent_float, |
95
|
|
|
|
|
|
|
); |
96
|
|
|
|
|
|
|
$schema->add_representer( |
97
|
|
|
|
|
|
|
equals => $_, |
98
|
|
|
|
|
|
|
code => \&represent_literal, |
99
|
21
|
|
|
|
|
71
|
) for ("", qw/ true false null /); |
100
|
21
|
|
|
|
|
465
|
$schema->add_representer( |
101
|
|
|
|
|
|
|
regex => qr{$RE_INT|$RE_FLOAT}, |
102
|
|
|
|
|
|
|
code => \&represent_literal, |
103
|
|
|
|
|
|
|
); |
104
|
|
|
|
|
|
|
|
105
|
21
|
100
|
|
|
|
64
|
if ($schema->bool_class) { |
106
|
2
|
|
|
|
|
3
|
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
|
|
|
|
|
101
|
return; |
115
|
|
|
|
|
|
|
} |
116
|
|
|
|
|
|
|
|
117
|
|
|
|
|
|
|
sub represent_undef { |
118
|
110
|
|
|
110
|
1
|
223
|
my ($rep, $node) = @_; |
119
|
110
|
|
|
|
|
255
|
$node->{style} = YAML_PLAIN_SCALAR_STYLE; |
120
|
110
|
|
|
|
|
207
|
$node->{data} = 'null'; |
121
|
110
|
|
|
|
|
373
|
return 1; |
122
|
|
|
|
|
|
|
} |
123
|
|
|
|
|
|
|
|
124
|
|
|
|
|
|
|
sub represent_literal { |
125
|
218
|
|
|
218
|
1
|
400
|
my ($rep, $node) = @_; |
126
|
218
|
|
100
|
|
|
1219
|
$node->{style} ||= YAML_SINGLE_QUOTED_SCALAR_STYLE; |
127
|
218
|
|
|
|
|
517
|
$node->{data} = "$node->{value}"; |
128
|
218
|
|
|
|
|
798
|
return 1; |
129
|
|
|
|
|
|
|
} |
130
|
|
|
|
|
|
|
|
131
|
|
|
|
|
|
|
|
132
|
|
|
|
|
|
|
sub represent_int { |
133
|
384
|
|
|
384
|
1
|
776
|
my ($rep, $node) = @_; |
134
|
384
|
100
|
|
|
|
1278
|
if (int($node->{value}) ne $node->{value}) { |
135
|
6
|
|
|
|
|
24
|
return 0; |
136
|
|
|
|
|
|
|
} |
137
|
378
|
|
|
|
|
821
|
$node->{style} = YAML_PLAIN_SCALAR_STYLE; |
138
|
378
|
|
|
|
|
798
|
$node->{data} = "$node->{value}"; |
139
|
378
|
|
|
|
|
1273
|
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
|
253
|
my ($rep, $node) = @_; |
149
|
130
|
100
|
|
|
|
757
|
if (exists $special{ $node->{value} }) { |
150
|
48
|
|
|
|
|
118
|
$node->{style} = YAML_PLAIN_SCALAR_STYLE; |
151
|
48
|
|
|
|
|
105
|
$node->{data} = $special{ $node->{value} }; |
152
|
48
|
|
|
|
|
161
|
return 1; |
153
|
|
|
|
|
|
|
} |
154
|
82
|
100
|
|
|
|
441
|
if (0.0 + $node->{value} ne $node->{value}) { |
155
|
3
|
|
|
|
|
13
|
return 0; |
156
|
|
|
|
|
|
|
} |
157
|
79
|
100
|
66
|
|
|
449
|
if (int($node->{value}) eq $node->{value} and not $node->{value} =~ m/\./) { |
158
|
35
|
|
|
|
|
81
|
$node->{value} .= '.0'; |
159
|
|
|
|
|
|
|
} |
160
|
79
|
|
|
|
|
185
|
$node->{style} = YAML_PLAIN_SCALAR_STYLE; |
161
|
79
|
|
|
|
|
239
|
$node->{data} = "$node->{value}"; |
162
|
79
|
|
|
|
|
305
|
return 1; |
163
|
|
|
|
|
|
|
} |
164
|
|
|
|
|
|
|
|
165
|
|
|
|
|
|
|
sub represent_bool { |
166
|
64
|
|
|
64
|
1
|
132
|
my ($rep, $node) = @_; |
167
|
64
|
100
|
|
|
|
305
|
my $string = $node->{value} ? 'true' : 'false'; |
168
|
64
|
|
|
|
|
602
|
$node->{style} = YAML_PLAIN_SCALAR_STYLE; |
169
|
64
|
|
|
|
|
110
|
@{ $node->{items} } = $string; |
|
64
|
|
|
|
|
188
|
|
170
|
64
|
|
|
|
|
142
|
$node->{data} = $string; |
171
|
64
|
|
|
|
|
238
|
return 1; |
172
|
|
|
|
|
|
|
} |
173
|
|
|
|
|
|
|
|
174
|
|
|
|
|
|
|
1; |
175
|
|
|
|
|
|
|
|
176
|
|
|
|
|
|
|
__END__ |