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 51     51   146547 use strict;
  51         77  
  51         1529  
2 51     51   1921 use warnings;
  51         75  
  51         3099  
3             package YAML::PP::Schema::JSON;
4              
5             our $VERSION = 'v0.41.0'; # VERSION
6              
7 51     51   210 use base 'Exporter';
  51         73  
  51         6142  
8             our @EXPORT_OK = qw/
9             represent_int represent_float represent_literal represent_bool
10             represent_undef
11             /;
12              
13 51     51   263 use B;
  51         87  
  51         1477  
14 51     51   203 use Carp qw/ croak /;
  51         73  
  51         2641  
15              
16 51     51   1488 use YAML::PP::Common qw/ YAML_PLAIN_SCALAR_STYLE YAML_SINGLE_QUOTED_SCALAR_STYLE /;
  51         90  
  51         51926  
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 9703     9703   31746 sub _to_int { 0 + $_[2]->[0] }
22              
23             # DaTa++ && shmem++
24 76     76   769 sub _to_float { unpack F => pack F => $_[2]->[0] }
25              
26             sub register {
27 22     22 1 53 my ($self, %args) = @_;
28 22         33 my $schema = $args{schema};
29 22         30 my $options = $args{options};
30 22         29 my $empty_null = 0;
31 22         38 for my $opt (@$options) {
32 2 50       6 if ($opt eq 'empty=str') {
    100          
33             }
34             elsif ($opt eq 'empty=null') {
35 1         4 $empty_null = 1;
36             }
37             else {
38 1         215 croak "Invalid option for JSON Schema: '$opt'";
39             }
40             }
41              
42             $schema->add_resolver(
43 21         68 tag => 'tag:yaml.org,2002:null',
44             match => [ equals => null => undef ],
45             );
46 21 100       40 if ($empty_null) {
47 1         4 $schema->add_resolver(
48             tag => 'tag:yaml.org,2002:null',
49             match => [ equals => '' => undef ],
50             implicit => 1,
51             );
52             }
53             else {
54 20         55 $schema->add_resolver(
55             tag => 'tag:yaml.org,2002:str',
56             match => [ equals => '' => '' ],
57             implicit => 1,
58             );
59             }
60 21         55 $schema->add_resolver(
61             tag => 'tag:yaml.org,2002:bool',
62             match => [ equals => true => $schema->true ],
63             );
64 21         50 $schema->add_resolver(
65             tag => 'tag:yaml.org,2002:bool',
66             match => [ equals => false => $schema->false ],
67             );
68 21         64 $schema->add_resolver(
69             tag => 'tag:yaml.org,2002:int',
70             match => [ regex => $RE_INT => \&_to_int ],
71             );
72 21         60 $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   107 match => [ all => sub { $_[1]->{value} } ],
  338         1071  
79             );
80              
81 21         68 $schema->add_representer(
82             undefined => \&represent_undef,
83             );
84              
85 21         25 my $int_flags = B::SVp_IOK;
86 21         28 my $float_flags = B::SVp_NOK;
87 21         76 $schema->add_representer(
88             flags => $int_flags,
89             code => \&represent_int,
90             );
91 21         60 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         52 ) for ("", qw/ true false null /);
100 21         593 $schema->add_representer(
101             regex => qr{$RE_INT|$RE_FLOAT},
102             code => \&represent_literal,
103             );
104              
105 21 50       46 if ($schema->bool_class) {
106 21         32 for my $class (@{ $schema->bool_class }) {
  21         33  
107 21 100       44 if ($class eq 'perl') {
108 19         42 $schema->add_representer(
109             bool => 1,
110             code => \&represent_bool,
111             );
112 19         29 next;
113             }
114             $schema->add_representer(
115 2         4 class_equals => $class,
116             code => \&represent_bool,
117             );
118             }
119             }
120              
121 21         90 return;
122             }
123              
124             sub represent_undef {
125 110     110 1 169 my ($rep, $node) = @_;
126 110         204 $node->{style} = YAML_PLAIN_SCALAR_STYLE;
127 110         194 $node->{data} = 'null';
128 110         313 return 1;
129             }
130              
131             sub represent_literal {
132 219     219 1 393 my ($rep, $node) = @_;
133 219   100     1069 $node->{style} ||= YAML_SINGLE_QUOTED_SCALAR_STYLE;
134 219         453 $node->{data} = "$node->{value}";
135 219         721 return 1;
136             }
137              
138              
139             sub represent_int {
140 386     386 1 608 my ($rep, $node) = @_;
141 386 100       1055 if (int($node->{value}) ne $node->{value}) {
142 7         21 return 0;
143             }
144 379         623 $node->{style} = YAML_PLAIN_SCALAR_STYLE;
145 379         736 $node->{data} = "$node->{value}";
146 379         1013 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 192 my ($rep, $node) = @_;
156 131 100       653 if (exists $special{ $node->{value} }) {
157 48         110 $node->{style} = YAML_PLAIN_SCALAR_STYLE;
158 48         95 $node->{data} = $special{ $node->{value} };
159 48         163 return 1;
160             }
161 83 100       391 if (0.0 + $node->{value} ne $node->{value}) {
162 4         12 return 0;
163             }
164 79 100 66     406 if (int($node->{value}) eq $node->{value} and not $node->{value} =~ m/\./) {
165 35         68 $node->{value} .= '.0';
166             }
167 79         186 $node->{style} = YAML_PLAIN_SCALAR_STYLE;
168 79         210 $node->{data} = "$node->{value}";
169 79         260 return 1;
170             }
171              
172             sub represent_bool {
173 66     66 1 107 my ($rep, $node) = @_;
174 66 100       303 my $string = $node->{value} ? 'true' : 'false';
175 66         598 $node->{style} = YAML_PLAIN_SCALAR_STYLE;
176 66         99 @{ $node->{items} } = $string;
  66         188  
177 66         119 $node->{data} = $string;
178 66         231 return 1;
179             }
180              
181             1;
182              
183             __END__