| 1 | package Mojo::JSON::Pointer; | ||||||||||||||||||||||||||||||||||
| 2 | 30 | ▶ | use Mojo::Base -base; | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 3 | |||||||||||||||||||||||||||||||||||
| 4 | 30 | has 'data'; | |||||||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||||||||
| 6 | 16 | ▶ | sub contains { shift->_pointer(0, @_) } | ||||||||||||||||||||||||||||||||
|
Subroutine
contains: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||
| 7 | 41 | ▶ | sub get { shift->_pointer(1, @_) } | ||||||||||||||||||||||||||||||||
|
Subroutine
get: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||||||||
| 9 | 30 | ▶ | sub new { @_ > 1 ? shift->SUPER::new(data => shift) : shift->SUPER::new } | ||||||||||||||||||||||||||||||||
|
Subroutine
new: called CC=2 SCAR=6.9
Pod
documented
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||||||||
| 11 | 57 | ▶ | sub _pointer { | ||||||||||||||||||||||||||||||||
|
Subroutine
_pointer: called CC=12 SCAR=24.8
| |||||||||||||||||||||||||||||||||||
| 12 | 57 | my ($self, $get, $pointer) = @_; | |||||||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||||||||
| 14 | 57 | my $data = $self->data; | |||||||||||||||||||||||||||||||||
| 15 | 57 | ▶ | return length $pointer ? undef : $get ? $data : 1 unless $pointer =~ s!^/!!; | ||||||||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||||||||
| 16 | 47 | ▶ | for my $p (length $pointer ? (split /\//, $pointer, -1) : ($pointer)) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 17 | 74 | $p =~ s!~1!/!g; | |||||||||||||||||||||||||||||||||
| 18 | 74 | $p =~ s/~0/~/g; | |||||||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||||||||
| 20 | # Hash | ||||||||||||||||||||||||||||||||||
| 21 | 74 | ▶ | if (ref $data eq 'HASH' && exists $data->{$p}) { $data = $data->{$p} } | ||||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition100%3/3
ref $data eq "HASH" and exists $data->{$p}
MC/DC100%2/2
ref $data eq "HASH" and exists $data->{$p}
ref $data eq "HASH"
exists $data->{$p}
Truth table100%3/3
A and B
A= ref $data eq "HASH"B= exists $data->{$p}
| |||||||||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||||||||
| 23 | # Array | ||||||||||||||||||||||||||||||||||
| 24 | 11 | ▶ | elsif (ref $data eq 'ARRAY' && $p =~ /^\d+$/ && @$data > $p) { $data = $data->[$p] } | ||||||||||||||||||||||||||||||||
|
Condition100%6/6
ref $data eq "ARRAY" and $p =~ /^\d+$/u and @$data > $p
ref $data eq "ARRAY" and $p =~ /^\d+$/u
MC/DC100%3/3
ref $data eq "ARRAY" and $p =~ /^\d+$/u and @$data > $p
ref $data eq "ARRAY"
$p =~ /^\d+$/u
@$data > $p
Truth table100%4/4
A and B and C
A= ref $data eq "ARRAY"B= $p =~ /^\d+$/uC= @$data > $p
| |||||||||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||||||||
| 26 | # Nothing | ||||||||||||||||||||||||||||||||||
| 27 | 10 | else { return undef } | |||||||||||||||||||||||||||||||||
| 28 | } | ||||||||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||||||||
| 30 | 37 | ▶ | return $get ? $data : 1; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 31 | } | ||||||||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||||||||
| 33 | 30 | 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 | ||||||||||||||||||||||||||||||||||