add files
|
1 |
package Mojo::JSON::Pointer; |
2 |
use Mojo::Base -base; |
|
3 | ||
4 |
use Scalar::Util 'looks_like_number'; |
|
5 | ||
6 |
sub contains { shift->_pointer(1, @_) } |
|
7 |
sub get { shift->_pointer(0, @_) } |
|
8 | ||
9 |
sub _pointer { |
|
10 |
my ($self, $contains, $data, $pointer) = @_; |
|
11 | ||
12 |
return $data unless $pointer =~ s!^/!!; |
|
13 |
for my $p ($pointer eq '' ? ($pointer) : (split '/', $pointer)) { |
|
14 |
$p =~ s/~0/~/g; |
|
15 |
$p =~ s!~1!/!g; |
|
16 | ||
17 |
# Hash |
|
18 |
if (ref $data eq 'HASH' && exists $data->{$p}) { $data = $data->{$p} } |
|
19 | ||
20 |
# Array |
|
21 |
elsif (ref $data eq 'ARRAY' && looks_like_number($p) && @$data > $p) { |
|
22 |
$data = $data->[$p]; |
|
23 |
} |
|
24 | ||
25 |
# Nothing |
|
26 |
else { return undef } |
|
27 |
} |
|
28 | ||
29 |
return $contains ? 1 : $data; |
|
30 |
} |
|
31 | ||
32 |
1; |
|
33 | ||
34 |
=encoding utf8 |
|
35 | ||
36 |
=head1 NAME |
|
37 | ||
38 |
Mojo::JSON::Pointer - JSON Pointers |
|
39 | ||
40 |
=head1 SYNOPSIS |
|
41 | ||
42 |
use Mojo::JSON::Pointer; |
|
43 | ||
44 |
my $pointer = Mojo::JSON::Pointer->new; |
|
45 |
say $pointer->get({foo => [23, 'bar']}, '/foo/1'); |
|
46 |
say 'Contains "/foo".' if $pointer->contains({foo => [23, 'bar']}, '/foo'); |
|
47 | ||
48 |
=head1 DESCRIPTION |
|
49 | ||
50 |
L<Mojo::JSON::Pointer> is a relaxed implementation of RFC 6901. |
|
51 | ||
52 |
=head1 METHODS |
|
53 | ||
54 |
=head2 contains |
|
55 | ||
56 |
my $bool = $pointer->contains($data, '/foo/1'); |
|
57 | ||
58 |
Check if data structure contains a value that can be identified with the given |
|
59 |
JSON Pointer. |
|
60 | ||
61 |
# True |
|
62 |
$pointer->contains({'♥' => 'mojolicious'}, '/♥'); |
|
63 |
$pointer->contains({foo => 'bar', baz => [4, 5, 6]}, '/foo'); |
|
64 |
$pointer->contains({foo => 'bar', baz => [4, 5, 6]}, '/baz/2'); |
|
65 | ||
66 |
# False |
|
67 |
$pointer->contains({'♥' => 'mojolicious'}, '/☃'); |
|
68 |
$pointer->contains({foo => 'bar', baz => [4, 5, 6]}, '/bar'); |
|
69 |
$pointer->contains({foo => 'bar', baz => [4, 5, 6]}, '/baz/9'); |
|
70 | ||
71 |
=head2 get |
|
72 | ||
73 |
my $value = $pointer->get($data, '/foo/bar'); |
|
74 | ||
75 |
Extract value identified by the given JSON Pointer. |
|
76 | ||
77 |
# "mojolicious" |
|
78 |
$pointer->get({'♥' => 'mojolicious'}, '/♥'); |
|
79 | ||
80 |
# "bar" |
|
81 |
$pointer->get({foo => 'bar', baz => [4, 5, 6]}, '/foo'); |
|
82 | ||
83 |
# "4" |
|
84 |
$pointer->get({foo => 'bar', baz => [4, 5, 6]}, '/baz/0'); |
|
85 | ||
86 |
# "6" |
|
87 |
$pointer->get({foo => 'bar', baz => [4, 5, 6]}, '/baz/2'); |
|
88 | ||
89 |
=head1 SEE ALSO |
|
90 | ||
91 |
L<Mojolicious>, L<Mojolicious::Guides>, L<http://mojolicio.us>. |
|
92 | ||
93 |
=cut |