Filename | /Users/ether/.perlbrew/libs/36.0@std/lib/perl5/Mojo/JSON/Pointer.pm |
Statements | Executed 377785 statements in 600ms |
Calls | P | F | Exclusive Time |
Inclusive Time |
Subroutine |
---|---|---|---|---|---|
47414 | 1 | 1 | 430ms | 690ms | _pointer | Mojo::JSON::Pointer::
47414 | 3 | 2 | 122ms | 812ms | get | Mojo::JSON::Pointer::
145810 | 3 | 1 | 67.0ms | 67.0ms | CORE:subst (opcode) | Mojo::JSON::Pointer::
210 | 1 | 1 | 838µs | 838µs | CORE:match (opcode) | Mojo::JSON::Pointer::
15 | 2 | 2 | 63µs | 90µs | new | Mojo::JSON::Pointer::
1 | 1 | 1 | 23µs | 154µs | BEGIN@2 | Mojo::JSON::Pointer::
0 | 0 | 0 | 0s | 0s | contains | Mojo::JSON::Pointer::
Line | State ments |
Time on line |
Calls | Time in subs |
Code |
---|---|---|---|---|---|
1 | package Mojo::JSON::Pointer; | ||||
2 | 2 | 322µs | 2 | 285µs | # spent 154µs (23+131) within Mojo::JSON::Pointer::BEGIN@2 which was called:
# once (23µs+131µs) by Moo::_Utils::_require at line 2 # spent 154µs making 1 call to Mojo::JSON::Pointer::BEGIN@2
# spent 131µs making 1 call to Mojo::Base::import |
3 | |||||
4 | 1 | 2µs | 1 | 34µs | has 'data'; # spent 34µs making 1 call to Mojo::JSON::Pointer::has |
5 | |||||
6 | sub contains { shift->_pointer(0, @_) } | ||||
7 | 47414 | 101ms | 47414 | 690ms | # spent 812ms (122+690) within Mojo::JSON::Pointer::get which was called 47414 times, avg 17µs/call:
# 38783 times (101ms+525ms) by JSON::Schema::Modern::_fetch_from_uri at line 867 of /Users/ether/git/JSON-Schema-Modern/lib/JSON/Schema/Modern.pm, avg 16µs/call
# 7668 times (16.7ms+114ms) by JSON::Schema::Modern::_fetch_from_uri at line 891 of /Users/ether/git/JSON-Schema-Modern/lib/JSON/Schema/Modern.pm, avg 17µs/call
# 963 times (4.28ms+51.5ms) by JSON::Schema::Modern::Document::OpenAPI::traverse at line 161 of JSON/Schema/Modern/Document/OpenAPI.pm, avg 58µs/call # spent 690ms making 47414 calls to Mojo::JSON::Pointer::_pointer, avg 15µs/call |
8 | |||||
9 | 15 | 55µs | 15 | 27µs | # spent 90µs (63+27) within Mojo::JSON::Pointer::new which was called 15 times, avg 6µs/call:
# 14 times (58µs+23µs) by JSON::Schema::Modern::Document::new at line 53 of (eval 428)[Sub/Quote.pm:3], avg 6µs/call
# once (5µs+4µs) by JSON::Schema::Modern::Document::OpenAPI::new at line 56 of (eval 421)[Sub/Quote.pm:3] # spent 27µs making 15 calls to Mojo::Base::new, avg 2µs/call |
10 | |||||
11 | # spent 690ms (430+260) within Mojo::JSON::Pointer::_pointer which was called 47414 times, avg 15µs/call:
# 47414 times (430ms+260ms) by Mojo::JSON::Pointer::get at line 7, avg 15µs/call | ||||
12 | 47414 | 24.3ms | my ($self, $get, $pointer) = @_; | ||
13 | |||||
14 | 47414 | 64.4ms | 47414 | 192ms | my $data = $self->data; # spent 192ms making 47414 calls to JSON::Schema::Modern::Document::data, avg 4µs/call |
15 | 47414 | 144ms | 47414 | 41.4ms | return length $pointer ? undef : $get ? $data : 1 unless $pointer =~ s!^/!!; # spent 41.4ms making 47414 calls to Mojo::JSON::Pointer::CORE:subst, avg 874ns/call |
16 | 20258 | 43.4ms | for my $p (length $pointer ? (split /\//, $pointer, -1) : ($pointer)) { | ||
17 | 49198 | 60.7ms | 49198 | 15.9ms | $p =~ s!~1!/!g; # spent 15.9ms making 49198 calls to Mojo::JSON::Pointer::CORE:subst, avg 323ns/call |
18 | 49198 | 47.7ms | 49198 | 9.68ms | $p =~ s/~0/~/g; # spent 9.68ms making 49198 calls to Mojo::JSON::Pointer::CORE:subst, avg 197ns/call |
19 | |||||
20 | # Hash | ||||
21 | 49198 | 81.8ms | 210 | 838µs | if (ref $data eq 'HASH' && exists $data->{$p}) { $data = $data->{$p} } # spent 838µs making 210 calls to Mojo::JSON::Pointer::CORE:match, avg 4µs/call |
22 | |||||
23 | # Array | ||||
24 | elsif (ref $data eq 'ARRAY' && $p =~ /^\d+$/ && @$data > $p) { $data = $data->[$p] } | ||||
25 | |||||
26 | # Nothing | ||||
27 | else { return undef } | ||||
28 | } | ||||
29 | |||||
30 | 20258 | 33.2ms | return $get ? $data : 1; | ||
31 | } | ||||
32 | |||||
33 | 1 | 3µs | 1; | ||
34 | |||||
35 | =encoding utf8 | ||||
36 | |||||
37 | =head1 NAME | ||||
38 | |||||
39 | Mojo::JSON::Pointer - JSON Pointers | ||||
40 | |||||
41 | =head1 SYNOPSIS | ||||
42 | |||||
43 | use Mojo::JSON::Pointer; | ||||
44 | |||||
45 | my $pointer = Mojo::JSON::Pointer->new({foo => [23, 'bar']}); | ||||
46 | say $pointer->get('/foo/1'); | ||||
47 | say 'Contains "/foo".' if $pointer->contains('/foo'); | ||||
48 | |||||
49 | =head1 DESCRIPTION | ||||
50 | |||||
51 | L<Mojo::JSON::Pointer> is an implementation of L<RFC 6901|https://tools.ietf.org/html/rfc6901>. | ||||
52 | |||||
53 | =head1 ATTRIBUTES | ||||
54 | |||||
55 | L<Mojo::JSON::Pointer> implements the following attributes. | ||||
56 | |||||
57 | =head2 data | ||||
58 | |||||
59 | my $data = $pointer->data; | ||||
60 | $pointer = $pointer->data({foo => 'bar'}); | ||||
61 | |||||
62 | Data structure to be processed. | ||||
63 | |||||
64 | =head1 METHODS | ||||
65 | |||||
66 | L<Mojo::JSON::Pointer> inherits all methods from L<Mojo::Base> and implements the following new ones. | ||||
67 | |||||
68 | =head2 contains | ||||
69 | |||||
70 | my $bool = $pointer->contains('/foo/1'); | ||||
71 | |||||
72 | Check if L</"data"> contains a value that can be identified with the given JSON Pointer. | ||||
73 | |||||
74 | # True | ||||
75 | Mojo::JSON::Pointer->new('just a string')->contains(''); | ||||
76 | Mojo::JSON::Pointer->new({'♥' => 'mojolicious'})->contains('/♥'); | ||||
77 | Mojo::JSON::Pointer->new({foo => 'bar', baz => [4, 5]})->contains('/foo'); | ||||
78 | Mojo::JSON::Pointer->new({foo => 'bar', baz => [4, 5]})->contains('/baz/1'); | ||||
79 | |||||
80 | # False | ||||
81 | Mojo::JSON::Pointer->new({'♥' => 'mojolicious'})->contains('/☃'); | ||||
82 | Mojo::JSON::Pointer->new({foo => 'bar', baz => [4, 5]})->contains('/bar'); | ||||
83 | Mojo::JSON::Pointer->new({foo => 'bar', baz => [4, 5]})->contains('/baz/9'); | ||||
84 | |||||
85 | =head2 get | ||||
86 | |||||
87 | my $value = $pointer->get('/foo/bar'); | ||||
88 | |||||
89 | Extract value from L</"data"> identified by the given JSON Pointer. | ||||
90 | |||||
91 | # "just a string" | ||||
92 | Mojo::JSON::Pointer->new('just a string')->get(''); | ||||
93 | |||||
94 | # "mojolicious" | ||||
95 | Mojo::JSON::Pointer->new({'♥' => 'mojolicious'})->get('/♥'); | ||||
96 | |||||
97 | # "bar" | ||||
98 | Mojo::JSON::Pointer->new({foo => 'bar', baz => [4, 5, 6]})->get('/foo'); | ||||
99 | |||||
100 | # "4" | ||||
101 | Mojo::JSON::Pointer->new({foo => 'bar', baz => [4, 5, 6]})->get('/baz/0'); | ||||
102 | |||||
103 | # "6" | ||||
104 | Mojo::JSON::Pointer->new({foo => 'bar', baz => [4, 5, 6]})->get('/baz/2'); | ||||
105 | |||||
106 | =head2 new | ||||
107 | |||||
108 | my $pointer = Mojo::JSON::Pointer->new; | ||||
109 | my $pointer = Mojo::JSON::Pointer->new({foo => 'bar'}); | ||||
110 | |||||
111 | Build new L<Mojo::JSON::Pointer> object. | ||||
112 | |||||
113 | =head1 SEE ALSO | ||||
114 | |||||
115 | L<Mojolicious>, L<Mojolicious::Guides>, L<https://mojolicious.org>. | ||||
116 | |||||
117 | =cut | ||||
# spent 838µs within Mojo::JSON::Pointer::CORE:match which was called 210 times, avg 4µs/call:
# 210 times (838µs+0s) by Mojo::JSON::Pointer::_pointer at line 21, avg 4µs/call | |||||
# spent 67.0ms within Mojo::JSON::Pointer::CORE:subst which was called 145810 times, avg 460ns/call:
# 49198 times (15.9ms+0s) by Mojo::JSON::Pointer::_pointer at line 17, avg 323ns/call
# 49198 times (9.68ms+0s) by Mojo::JSON::Pointer::_pointer at line 18, avg 197ns/call
# 47414 times (41.4ms+0s) by Mojo::JSON::Pointer::_pointer at line 15, avg 874ns/call |