File Coverage

blib/lib/YAML/PP/Schema/JSON.pm
Criterion Covered Total %
statement 87 87 100.0
branch 18 20 90.0
condition 4 5 80.0
subroutine 15 15 100.0
pod 6 6 100.0
total 130 133 97.7


line stmt bran cond sub pod time code
1 46     46   139325 use strict;
  46         61  
  46         1359  
2 46     46   147 use warnings;
  46         72  
  46         4448  
3             package YAML::PP::Schema::JSON;
4              
5             our $VERSION = 'v0.40.0'; # VERSION
6              
7 46     46   196 use base 'Exporter';
  46         57  
  46         5517  
8             our @EXPORT_OK = qw/
9             represent_int represent_float represent_literal represent_bool
10             represent_undef
11             /;
12              
13 46     46   214 use B;
  46         62  
  46         1102  
14 46     46   167 use Carp qw/ croak /;
  46         62  
  46         2268  
15              
16 46     46   1442 use YAML::PP::Common qw/ YAML_PLAIN_SCALAR_STYLE YAML_SINGLE_QUOTED_SCALAR_STYLE /;
  46         63  
  46         45077  
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 388     388   1614 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 57 my ($self, %args) = @_;
28 22         35 my $schema = $args{schema};
29 22         39 my $options = $args{options};
30 22         31 my $empty_null = 0;
31 22         39 for my $opt (@$options) {
32 2 50       6 if ($opt eq 'empty=str') {
    100          
33             }
34             elsif ($opt eq 'empty=null') {
35 1         1 $empty_null = 1;
36             }
37             else {
38 1         221 croak "Invalid option for JSON Schema: '$opt'";
39             }
40             }
41              
42             $schema->add_resolver(
43 21         90 tag => 'tag:yaml.org,2002:null',
44             match => [ equals => null => undef ],
45             );
46 21 100       45 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         61 $schema->add_resolver(
55             tag => 'tag:yaml.org,2002:str',
56             match => [ equals => '' => '' ],
57             implicit => 1,
58             );
59             }
60 21         58 $schema->add_resolver(
61             tag => 'tag:yaml.org,2002:bool',
62             match => [ equals => true => $schema->true ],
63             );
64 21         58 $schema->add_resolver(
65             tag => 'tag:yaml.org,2002:bool',
66             match => [ equals => false => $schema->false ],
67             );
68 21         66 $schema->add_resolver(
69             tag => 'tag:yaml.org,2002:int',
70             match => [ regex => $RE_INT => \&_to_int ],
71             );
72 21         62 $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   154 match => [ all => sub { $_[1]->{value} } ],
  338         1074  
79             );
80              
81 21         73 $schema->add_representer(
82             undefined => \&represent_undef,
83             );
84              
85 21         29 my $int_flags = B::SVp_IOK;
86 21         28 my $float_flags = B::SVp_NOK;
87 21         48 $schema->add_representer(
88             flags => $int_flags,
89             code => \&represent_int,
90             );
91 21         68 my %special = ( (0+'nan').'' => '.nan', (0+'inf').'' => '.inf', (0-'inf').'' => '-.inf' );
92 21         49 $schema->add_representer(
93             flags => $float_flags,
94             code => \&represent_float,
95             );
96             $schema->add_representer(
97             equals => $_,
98             code => \&represent_literal,
99 21         56 ) for ("", qw/ true false null /);
100 21         571 $schema->add_representer(
101             regex => qr{$RE_INT|$RE_FLOAT},
102             code => \&represent_literal,
103             );
104              
105 21 50       48 if ($schema->bool_class) {
106 21         27 for my $class (@{ $schema->bool_class }) {
  21         32  
107 21 100       39 if ($class eq 'perl') {
108 19         45 $schema->add_representer(
109             bool => 1,
110             code => \&represent_bool,
111             );
112 19         37 next;
113             }
114             $schema->add_representer(
115 2         6 class_equals => $class,
116             code => \&represent_bool,
117             );
118             }
119             }
120              
121 21         81 return;
122             }
123              
124             sub represent_undef {
125 110     110 1 171 my ($rep, $node) = @_;
126 110         177 $node->{style} = YAML_PLAIN_SCALAR_STYLE;
127 110         175 $node->{data} = 'null';
128 110         273 return 1;
129             }
130              
131             sub represent_literal {
132 219     219 1 380 my ($rep, $node) = @_;
133 219   100     1062 $node->{style} ||= YAML_SINGLE_QUOTED_SCALAR_STYLE;
134 219         445 $node->{data} = "$node->{value}";
135 219         716 return 1;
136             }
137              
138              
139             sub represent_int {
140 386     386 1 582 my ($rep, $node) = @_;
141 386 100       1124 if (int($node->{value}) ne $node->{value}) {
142 7         23 return 0;
143             }
144 379         647 $node->{style} = YAML_PLAIN_SCALAR_STYLE;
145 379         701 $node->{data} = "$node->{value}";
146 379         1188 return 1;
147             }
148              
149             my %special = (
150             (0+'nan').'' => '.nan',
151             (0+'inf').'' => '.inf',
152             (0-'inf').'' => '-.inf'
153             );
154             sub represent_float {
155 131     131 1 223 my ($rep, $node) = @_;
156 131 100       705 if (exists $special{ $node->{value} }) {
157 48         99 $node->{style} = YAML_PLAIN_SCALAR_STYLE;
158 48         99 $node->{data} = $special{ $node->{value} };
159 48         154 return 1;
160             }
161 83 100       391 if (0.0 + $node->{value} ne $node->{value}) {
162 4         10 return 0;
163             }
164 79 100 66     388 if (int($node->{value}) eq $node->{value} and not $node->{value} =~ m/\./) {
165 35         67 $node->{value} .= '.0';
166             }
167 79         187 $node->{style} = YAML_PLAIN_SCALAR_STYLE;
168 79         181 $node->{data} = "$node->{value}";
169 79         280 return 1;
170             }
171              
172             sub represent_bool {
173 66     66 1 91 my ($rep, $node) = @_;
174 66 100       237 my $string = $node->{value} ? 'true' : 'false';
175 66         448 $node->{style} = YAML_PLAIN_SCALAR_STYLE;
176 66         98 @{ $node->{items} } = $string;
  66         157  
177 66         126 $node->{data} = $string;
178 66         200 return 1;
179             }
180              
181             1;
182              
183             __END__