Skip to content

Commit

Permalink
more RFC 6901 compliance tweaks
Browse files Browse the repository at this point in the history
  • Loading branch information
kraih committed Apr 3, 2013
1 parent 602eaef commit 593965a
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 27 deletions.
20 changes: 9 additions & 11 deletions lib/Mojo/JSON/Pointer.pm
Expand Up @@ -10,7 +10,6 @@ sub get { shift->_pointer(0, @_) }
sub _pointer {
my ($self, $contains, $data, $pointer) = @_;

$pointer = decode('UTF-8', url_unescape $pointer);
return $data unless $pointer =~ s!^/!!;
for my $p ($pointer eq '' ? ($pointer) : (split '/', $pointer)) {
$p =~ s/~0/~/g;
Expand Down Expand Up @@ -56,31 +55,30 @@ L<Mojo::JSON::Pointer> is a relaxed implementation of RFC 6901.
my $success = $pointer->contains($data, '/foo/1');
Check if data structure contains a value that can be identified with the given
JSON Pointer in fragment identifier representation.
JSON Pointer.
# True
$pointer->contains({'fo o' => 'bar', baz => [4, 5, 6]}, '/fo%20o');
$pointer->contains({'fo o' => 'bar', baz => [4, 5, 6]}, '/baz/2');
$pointer->contains({foo => 'bar', baz => [4, 5, 6]}, '/foo');
$pointer->contains({foo => 'bar', baz => [4, 5, 6]}, '/baz/2');
# False
$pointer->contains({'fo o' => 'bar', baz => [4, 5, 6]}, '/bar');
$pointer->contains({'fo o' => 'bar', baz => [4, 5, 6]}, '/baz/9');
$pointer->contains({foo => 'bar', baz => [4, 5, 6]}, '/bar');
$pointer->contains({foo => 'bar', baz => [4, 5, 6]}, '/baz/9');
=head2 get
my $value = $pointer->get($data, '/foo/bar');
Extract value identified by the given JSON Pointer in fragment identifier
representation.
Extract value identified by the given JSON Pointer.
# "bar"
$pointer->get({'fo o' => 'bar', baz => [4, 5, 6]}, '/fo%20o');
$pointer->get({foo => 'bar', baz => [4, 5, 6]}, '/foo');
# "4"
$pointer->get({'fo o' => 'bar', baz => [4, 5, 6]}, '/baz/0');
$pointer->get({foo => 'bar', baz => [4, 5, 6]}, '/baz/0');
# "6"
$pointer->get({'fo o' => 'bar', baz => [4, 5, 6]}, '/baz/2');
$pointer->get({foo => 'bar', baz => [4, 5, 6]}, '/baz/2');
=head1 SEE ALSO
Expand Down
17 changes: 1 addition & 16 deletions t/mojo/json_pointer.t
Expand Up @@ -41,22 +41,12 @@ is $pointer->get({foo => {bar => [0, undef, 3]}}, '/foo/bar/6'), undef,
'"/foo/bar/6" is "undef"';

# "get" (encoded)
is $pointer->get({'' => [0, 1]}, '/%E2%99%A5/0'), 0,
'"/%E2%99%A5/0" is "0"';
is $pointer->get([{'^foob ar' => 'foo'}], '/0/^foob ar'), 'foo',
'"/0/^foob ar" is "foo"';
is $pointer->get([{'foob ar' => 'foo'}], '/0/foob%20ar'), 'foo',
'"/0/foob%20ar" is "foo"';
is $pointer->get([{'foo/bar' => 'bar'}], '/0/foo%2Fbar'), undef,
'"/0/foo%2Fbar" is "undef"';
is $pointer->get([{'foo/bar' => 'bar'}], '/0/foo~1bar'), 'bar',
'"/0/foo~1bar" is "bar"';
is $pointer->get([{'foo/bar/baz' => 'yada'}], '/0/foo~1bar~1baz'), 'yada',
'"/0/foo~1bar~1baz" is "yada"';
is $pointer->get([{'foo~/bar' => 'bar'}], '/0/foo~0~1bar'), 'bar',
'"/0/foo~0~1bar" is "bar"';
is $pointer->get([{'foo~/bar' => 'bar'}], '/0/foo%7E%30%7E%31bar'), 'bar',
'"/0/foo%7E%30%7E%31bar" is "bar"';
is $pointer->get(
[{'f~o~o~/b~' => {'a~' => {'r' => 'baz'}}}] => '/0/f~0o~0o~0~1b~0/a~0/r'),
'baz', '"/0/f~0o~0o~0~1b~0/a~0/r" is "baz"';
Expand All @@ -80,17 +70,12 @@ is_deeply $pointer->get($hash, '/foo'), ['bar', 'baz'],
is $pointer->get($hash, '/foo/0'), 'bar', '"/foo/0" is "bar"';
is $pointer->get($hash, '/'), 0, '"/" is 0';
is $pointer->get($hash, '/a~1b'), 1, '"/a~1b" is 1';
is $pointer->get($hash, '/c%25d'), 2, '"/c%25d" is 2';
is $pointer->get($hash, '/c%d'), 2, '"/c%d" is 2';
is $pointer->get($hash, '/e^f'), 3, '"/e^f" is 3';
is $pointer->get($hash, '/e%5Ef'), 3, '"/e%5Ef" is 3';
is $pointer->get($hash, '/g|h'), 4, '"/g|h" is 4';
is $pointer->get($hash, '/g%7Ch'), 4, '"/g%7Ch" is 4';
is $pointer->get($hash, '/i\\j'), 5, '"/i\\\\j" is 5';
is $pointer->get($hash, '/i%5Cj'), 5, '"/i%5Cj" is 5';
is $pointer->get($hash, '/k"l'), 6, '"/k\\"l" is 6';
is $pointer->get($hash, '/k%22l'), 6, '"/k%22l" is 6';
is $pointer->get($hash, '/ '), 7, '"/ " is 7';
is $pointer->get($hash, '/%20'), 7, '"/%20" is 7';
is $pointer->get($hash, '/m~0n'), 8, '"/m~0n" is 8';

done_testing();

0 comments on commit 593965a

Please sign in to comment.