| Filename | /Users/ether/.perlbrew/libs/36.0@std/lib/perl5/YAML/PP/Schema/JSON.pm |
| Statements | Executed 165 statements in 1.40ms |
| Calls | P | F | Exclusive Time |
Inclusive Time |
Subroutine |
|---|---|---|---|---|---|
| 147 | 1 | 1 | 376µs | 376µs | YAML::PP::Schema::JSON::_to_int |
| 1 | 1 | 1 | 96µs | 118µs | YAML::PP::BEGIN@1.282 |
| 1 | 1 | 1 | 9µs | 11µs | YAML::PP::Schema::JSON::BEGIN@13 |
| 1 | 1 | 1 | 8µs | 41µs | YAML::PP::BEGIN@2.283 |
| 1 | 1 | 1 | 8µs | 37µs | YAML::PP::Schema::JSON::BEGIN@14 |
| 1 | 1 | 1 | 6µs | 66µs | YAML::PP::Schema::JSON::BEGIN@7 |
| 1 | 1 | 1 | 5µs | 19µs | YAML::PP::Schema::JSON::BEGIN@16 |
| 2 | 2 | 1 | 2µs | 2µs | YAML::PP::Schema::JSON::CORE:qr (opcode) |
| 0 | 0 | 0 | 0s | 0s | YAML::PP::Schema::JSON::__ANON__[:78] |
| 0 | 0 | 0 | 0s | 0s | YAML::PP::Schema::JSON::_to_float |
| 0 | 0 | 0 | 0s | 0s | YAML::PP::Schema::JSON::register |
| 0 | 0 | 0 | 0s | 0s | YAML::PP::Schema::JSON::represent_bool |
| 0 | 0 | 0 | 0s | 0s | YAML::PP::Schema::JSON::represent_float |
| 0 | 0 | 0 | 0s | 0s | YAML::PP::Schema::JSON::represent_int |
| 0 | 0 | 0 | 0s | 0s | YAML::PP::Schema::JSON::represent_literal |
| 0 | 0 | 0 | 0s | 0s | YAML::PP::Schema::JSON::represent_undef |
| Line | State ments |
Time on line |
Calls | Time in subs |
Code |
|---|---|---|---|---|---|
| 1 | 2 | 63µs | 2 | 140µs | # spent 118µs (96+22) within YAML::PP::BEGIN@1.282 which was called:
# once (96µs+22µs) by YAML::PP::BEGIN@9 at line 1 # spent 118µs making 1 call to YAML::PP::BEGIN@1.282
# spent 22µs making 1 call to strict::import |
| 2 | 2 | 37µs | 2 | 74µs | # spent 41µs (8+33) within YAML::PP::BEGIN@2.283 which was called:
# once (8µs+33µs) by YAML::PP::BEGIN@9 at line 2 # spent 41µs making 1 call to YAML::PP::BEGIN@2.283
# spent 33µs making 1 call to warnings::import |
| 3 | package YAML::PP::Schema::JSON; | ||||
| 4 | |||||
| 5 | 1 | 0s | our $VERSION = '0.035'; # VERSION | ||
| 6 | |||||
| 7 | 2 | 31µs | 2 | 126µs | # spent 66µs (6+60) within YAML::PP::Schema::JSON::BEGIN@7 which was called:
# once (6µs+60µs) by YAML::PP::BEGIN@9 at line 7 # spent 66µs making 1 call to YAML::PP::Schema::JSON::BEGIN@7
# spent 60µs making 1 call to base::import |
| 8 | 1 | 1µs | our @EXPORT_OK = qw/ | ||
| 9 | represent_int represent_float represent_literal represent_bool | ||||
| 10 | represent_undef | ||||
| 11 | /; | ||||
| 12 | |||||
| 13 | 2 | 17µs | 2 | 13µs | # spent 11µs (9+2) within YAML::PP::Schema::JSON::BEGIN@13 which was called:
# once (9µs+2µs) by YAML::PP::BEGIN@9 at line 13 # spent 11µs making 1 call to YAML::PP::Schema::JSON::BEGIN@13
# spent 2µs making 1 call to B::import |
| 14 | 2 | 22µs | 2 | 66µs | # spent 37µs (8+29) within YAML::PP::Schema::JSON::BEGIN@14 which was called:
# once (8µs+29µs) by YAML::PP::BEGIN@9 at line 14 # spent 37µs making 1 call to YAML::PP::Schema::JSON::BEGIN@14
# spent 29µs making 1 call to Exporter::import |
| 15 | |||||
| 16 | 2 | 579µs | 2 | 33µs | # spent 19µs (5+14) within YAML::PP::Schema::JSON::BEGIN@16 which was called:
# once (5µs+14µs) by YAML::PP::BEGIN@9 at line 16 # spent 19µs making 1 call to YAML::PP::Schema::JSON::BEGIN@16
# spent 14µs making 1 call to Exporter::import |
| 17 | |||||
| 18 | 1 | 7µs | 1 | 2µs | my $RE_INT = qr{^(-?(?:0|[1-9][0-9]*))$}; # spent 2µs making 1 call to YAML::PP::Schema::JSON::CORE:qr |
| 19 | 1 | 1µs | 1 | 0s | my $RE_FLOAT = qr{^(-?(?:0|[1-9][0-9]*)(?:\.[0-9]*)?(?:[eE][+-]?[0-9]+)?)$}; # spent 0s making 1 call to YAML::PP::Schema::JSON::CORE:qr |
| 20 | |||||
| 21 | 147 | 642µs | # spent 376µs within YAML::PP::Schema::JSON::_to_int which was called 147 times, avg 3µs/call:
# 147 times (376µs+0s) by YAML::PP::Schema::load_scalar at line 342 of YAML/PP/Schema.pm, avg 3µs/call | ||
| 22 | |||||
| 23 | # DaTa++ && shmem++ | ||||
| 24 | sub _to_float { unpack F => pack F => $_[2]->[0] } | ||||
| 25 | |||||
| 26 | sub register { | ||||
| 27 | my ($self, %args) = @_; | ||||
| 28 | my $schema = $args{schema}; | ||||
| 29 | my $options = $args{options}; | ||||
| 30 | my $empty_null = 0; | ||||
| 31 | for my $opt (@$options) { | ||||
| 32 | if ($opt eq 'empty=str') { | ||||
| 33 | } | ||||
| 34 | elsif ($opt eq 'empty=null') { | ||||
| 35 | $empty_null = 1; | ||||
| 36 | } | ||||
| 37 | else { | ||||
| 38 | croak "Invalid option for JSON Schema: '$opt'"; | ||||
| 39 | } | ||||
| 40 | } | ||||
| 41 | |||||
| 42 | $schema->add_resolver( | ||||
| 43 | tag => 'tag:yaml.org,2002:null', | ||||
| 44 | match => [ equals => null => undef ], | ||||
| 45 | ); | ||||
| 46 | if ($empty_null) { | ||||
| 47 | $schema->add_resolver( | ||||
| 48 | tag => 'tag:yaml.org,2002:null', | ||||
| 49 | match => [ equals => '' => undef ], | ||||
| 50 | implicit => 1, | ||||
| 51 | ); | ||||
| 52 | } | ||||
| 53 | else { | ||||
| 54 | $schema->add_resolver( | ||||
| 55 | tag => 'tag:yaml.org,2002:str', | ||||
| 56 | match => [ equals => '' => '' ], | ||||
| 57 | implicit => 1, | ||||
| 58 | ); | ||||
| 59 | } | ||||
| 60 | $schema->add_resolver( | ||||
| 61 | tag => 'tag:yaml.org,2002:bool', | ||||
| 62 | match => [ equals => true => $schema->true ], | ||||
| 63 | ); | ||||
| 64 | $schema->add_resolver( | ||||
| 65 | tag => 'tag:yaml.org,2002:bool', | ||||
| 66 | match => [ equals => false => $schema->false ], | ||||
| 67 | ); | ||||
| 68 | $schema->add_resolver( | ||||
| 69 | tag => 'tag:yaml.org,2002:int', | ||||
| 70 | match => [ regex => $RE_INT => \&_to_int ], | ||||
| 71 | ); | ||||
| 72 | $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 | match => [ all => sub { $_[1]->{value} } ], | ||||
| 79 | ); | ||||
| 80 | |||||
| 81 | $schema->add_representer( | ||||
| 82 | undefined => \&represent_undef, | ||||
| 83 | ); | ||||
| 84 | |||||
| 85 | my $int_flags = B::SVp_IOK; | ||||
| 86 | my $float_flags = B::SVp_NOK; | ||||
| 87 | $schema->add_representer( | ||||
| 88 | flags => $int_flags, | ||||
| 89 | code => \&represent_int, | ||||
| 90 | ); | ||||
| 91 | my %special = ( (0+'nan').'' => '.nan', (0+'inf').'' => '.inf', (0-'inf').'' => '-.inf' ); | ||||
| 92 | $schema->add_representer( | ||||
| 93 | flags => $float_flags, | ||||
| 94 | code => \&represent_float, | ||||
| 95 | ); | ||||
| 96 | $schema->add_representer( | ||||
| 97 | equals => $_, | ||||
| 98 | code => \&represent_literal, | ||||
| 99 | ) for ("", qw/ true false null /); | ||||
| 100 | $schema->add_representer( | ||||
| 101 | regex => qr{$RE_INT|$RE_FLOAT}, | ||||
| 102 | code => \&represent_literal, | ||||
| 103 | ); | ||||
| 104 | |||||
| 105 | if ($schema->bool_class) { | ||||
| 106 | for my $class (@{ $schema->bool_class }) { | ||||
| 107 | $schema->add_representer( | ||||
| 108 | class_equals => $class, | ||||
| 109 | code => \&represent_bool, | ||||
| 110 | ); | ||||
| 111 | } | ||||
| 112 | } | ||||
| 113 | |||||
| 114 | return; | ||||
| 115 | } | ||||
| 116 | |||||
| 117 | sub represent_undef { | ||||
| 118 | my ($rep, $node) = @_; | ||||
| 119 | $node->{style} = YAML_PLAIN_SCALAR_STYLE; | ||||
| 120 | $node->{data} = 'null'; | ||||
| 121 | return 1; | ||||
| 122 | } | ||||
| 123 | |||||
| 124 | sub represent_literal { | ||||
| 125 | my ($rep, $node) = @_; | ||||
| 126 | $node->{style} ||= YAML_SINGLE_QUOTED_SCALAR_STYLE; | ||||
| 127 | $node->{data} = "$node->{value}"; | ||||
| 128 | return 1; | ||||
| 129 | } | ||||
| 130 | |||||
| 131 | |||||
| 132 | sub represent_int { | ||||
| 133 | my ($rep, $node) = @_; | ||||
| 134 | if (int($node->{value}) ne $node->{value}) { | ||||
| 135 | return 0; | ||||
| 136 | } | ||||
| 137 | $node->{style} = YAML_PLAIN_SCALAR_STYLE; | ||||
| 138 | $node->{data} = "$node->{value}"; | ||||
| 139 | return 1; | ||||
| 140 | } | ||||
| 141 | |||||
| 142 | 1 | 1µs | my %special = ( | ||
| 143 | (0+'nan').'' => '.nan', | ||||
| 144 | (0+'inf').'' => '.inf', | ||||
| 145 | (0-'inf').'' => '-.inf' | ||||
| 146 | ); | ||||
| 147 | sub represent_float { | ||||
| 148 | my ($rep, $node) = @_; | ||||
| 149 | if (exists $special{ $node->{value} }) { | ||||
| 150 | $node->{style} = YAML_PLAIN_SCALAR_STYLE; | ||||
| 151 | $node->{data} = $special{ $node->{value} }; | ||||
| 152 | return 1; | ||||
| 153 | } | ||||
| 154 | if (0.0 + $node->{value} ne $node->{value}) { | ||||
| 155 | return 0; | ||||
| 156 | } | ||||
| 157 | if (int($node->{value}) eq $node->{value} and not $node->{value} =~ m/\./) { | ||||
| 158 | $node->{value} .= '.0'; | ||||
| 159 | } | ||||
| 160 | $node->{style} = YAML_PLAIN_SCALAR_STYLE; | ||||
| 161 | $node->{data} = "$node->{value}"; | ||||
| 162 | return 1; | ||||
| 163 | } | ||||
| 164 | |||||
| 165 | sub represent_bool { | ||||
| 166 | my ($rep, $node) = @_; | ||||
| 167 | my $string = $node->{value} ? 'true' : 'false'; | ||||
| 168 | $node->{style} = YAML_PLAIN_SCALAR_STYLE; | ||||
| 169 | @{ $node->{items} } = $string; | ||||
| 170 | $node->{data} = $string; | ||||
| 171 | return 1; | ||||
| 172 | } | ||||
| 173 | |||||
| 174 | 1 | 4µs | 1; | ||
| 175 | |||||
| 176 | __END__ | ||||
sub YAML::PP::Schema::JSON::CORE:qr; # opcode |