line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Mojo::JSON::Pointer; |
2
|
60
|
|
|
60
|
|
66050
|
use Mojo::Base -base; |
|
60
|
|
|
|
|
137
|
|
|
60
|
|
|
|
|
491
|
|
3
|
|
|
|
|
|
|
|
4
|
|
|
|
|
|
|
has 'data'; |
5
|
|
|
|
|
|
|
|
6
|
26
|
|
|
26
|
1
|
88
|
sub contains { shift->_pointer(0, @_) } |
7
|
62
|
|
|
62
|
1
|
204
|
sub get { shift->_pointer(1, @_) } |
8
|
|
|
|
|
|
|
|
9
|
61
|
100
|
|
61
|
1
|
20962
|
sub new { @_ > 1 ? shift->SUPER::new(data => shift) : shift->SUPER::new } |
10
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
sub _pointer { |
12
|
88
|
|
|
88
|
|
196
|
my ($self, $get, $pointer) = @_; |
13
|
|
|
|
|
|
|
|
14
|
88
|
|
|
|
|
212
|
my $data = $self->data; |
15
|
88
|
100
|
|
|
|
577
|
return length $pointer ? undef : $get ? $data : 1 unless $pointer =~ s!^/!!; |
|
|
100
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
16
|
71
|
100
|
|
|
|
315
|
for my $p (length $pointer ? (split /\//, $pointer, -1) : ($pointer)) { |
17
|
108
|
|
|
|
|
235
|
$p =~ s!~1!/!g; |
18
|
108
|
|
|
|
|
164
|
$p =~ s/~0/~/g; |
19
|
|
|
|
|
|
|
|
20
|
|
|
|
|
|
|
# Hash |
21
|
108
|
100
|
100
|
|
|
679
|
if (ref $data eq 'HASH' && exists $data->{$p}) { $data = $data->{$p} } |
|
69
|
100
|
100
|
|
|
160
|
|
|
|
|
100
|
|
|
|
|
22
|
|
|
|
|
|
|
|
23
|
|
|
|
|
|
|
# Array |
24
|
24
|
|
|
|
|
66
|
elsif (ref $data eq 'ARRAY' && $p =~ /^\d+$/ && @$data > $p) { $data = $data->[$p] } |
25
|
|
|
|
|
|
|
|
26
|
|
|
|
|
|
|
# Nothing |
27
|
15
|
|
|
|
|
106
|
else { return undef } |
28
|
|
|
|
|
|
|
} |
29
|
|
|
|
|
|
|
|
30
|
56
|
100
|
|
|
|
358
|
return $get ? $data : 1; |
31
|
|
|
|
|
|
|
} |
32
|
|
|
|
|
|
|
|
33
|
|
|
|
|
|
|
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 is an implementation of L. |
52
|
|
|
|
|
|
|
|
53
|
|
|
|
|
|
|
=head1 ATTRIBUTES |
54
|
|
|
|
|
|
|
|
55
|
|
|
|
|
|
|
L 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 inherits all methods from L 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 object. |
112
|
|
|
|
|
|
|
|
113
|
|
|
|
|
|
|
=head1 SEE ALSO |
114
|
|
|
|
|
|
|
|
115
|
|
|
|
|
|
|
L, L, L. |
116
|
|
|
|
|
|
|
|
117
|
|
|
|
|
|
|
=cut |