Skip to content

Commit

Permalink
fix "0" value bug in Mojo::JSON::Pointer
Browse files Browse the repository at this point in the history
  • Loading branch information
kraih committed Oct 14, 2015
1 parent c662f0e commit c99ece3
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 3 deletions.
1 change: 1 addition & 0 deletions Changes
@@ -1,5 +1,6 @@

6.25 2015-10-14
- Fixed "0" value bug in Mojo::JSON::Pointer.

6.24 2015-10-13
- Improved session security by not storing secrets in the stash and making
Expand Down
6 changes: 5 additions & 1 deletion lib/Mojo/JSON/Pointer.pm
Expand Up @@ -12,7 +12,7 @@ sub _pointer {
my ($self, $contains, $pointer) = @_;

my $data = $self->data;
return $data unless $pointer =~ s!^/!!;
return $contains ? 1 : $data unless $pointer =~ s!^/!!;
for my $p ($pointer eq '' ? ($pointer) : (split '/', $pointer, -1)) {
$p =~ s!~1!/!g;
$p =~ s/~0/~/g;
Expand Down Expand Up @@ -77,6 +77,7 @@ Check if L</"data"> contains a value that can be identified with the given JSON
Pointer.
# True
Mojo::JSON::Pointer->new('just a string')->contains('');
Mojo::JSON::Pointer->new({'♥' => 'mojolicious'})->contains('/♥');
Mojo::JSON::Pointer->new({foo => 'bar', baz => [4, 5]})->contains('/foo');
Mojo::JSON::Pointer->new({foo => 'bar', baz => [4, 5]})->contains('/baz/1');
Expand All @@ -92,6 +93,9 @@ Pointer.
Extract value from L</"data"> identified by the given JSON Pointer.
# "just a string"
Mojo::JSON::Pointer->new('just a string')->get('');
# "mojolicious"
Mojo::JSON::Pointer->new({'♥' => 'mojolicious'})->get('/♥');
Expand Down
18 changes: 16 additions & 2 deletions t/mojo/json_pointer.t
Expand Up @@ -12,6 +12,13 @@ ok !$pointer->contains('/bar'), 'does not contains "/bar"';
ok $pointer->new({foo => {bar => undef}})->contains('/foo/bar'),
'contains "/foo/bar"';

# "contains" (string)
$pointer = Mojo::JSON::Pointer->new('works');
ok $pointer->contains(''), 'contains ""';
ok !$pointer->contains('/'), 'does not contain "/"';
ok !$pointer->contains('/foo'), 'does not contain "/foo"';
ok $pointer->new('0')->contains(''), 'contains ""';

# "contains" (mixed)
$pointer = Mojo::JSON::Pointer->new({foo => [0, 1, 2]});
ok $pointer->contains(''), 'contains ""';
Expand All @@ -35,14 +42,21 @@ is $pointer->new({foo => {'' => 42}})->get('/foo/'), 42, '"/foo/" is "42"';
is $pointer->new({foo => {'' => {'' => 42}}})->get('/foo//'), 42,
'"/foo//" is "42"';

# "get" (string)
$pointer = Mojo::JSON::Pointer->new('works');
is $pointer->get(''), 'works', '"" is "works"';
is $pointer->get('/'), undef, '"/" is undef';
is $pointer->get('/foo'), undef, '"/foo" is undef';
is $pointer->new('0')->get(''), 0, '"/foo" is "0"';

# "get" (mixed)
is_deeply $pointer->new({foo => {bar => [1, 2, 3]}})->get('/foo/bar'),
[1, 2, 3], '"/foo/bar" is "[1, 2, 3]"';
$pointer = Mojo::JSON::Pointer->new({foo => {bar => [0, undef, 3]}});
is $pointer->get('/foo/bar/0'), 0, '"/foo/bar/0" is "0"';
is $pointer->get('/foo/bar/1'), undef, '"/foo/bar/1" is "undef"';
is $pointer->get('/foo/bar/1'), undef, '"/foo/bar/1" is undef';
is $pointer->get('/foo/bar/2'), 3, '"/foo/bar/2" is "3"';
is $pointer->get('/foo/bar/6'), undef, '"/foo/bar/6" is "undef"';
is $pointer->get('/foo/bar/6'), undef, '"/foo/bar/6" is undef';

# "get" (encoded)
is $pointer->new([{'foo/bar' => 'bar'}])->get('/0/foo~1bar'), 'bar',
Expand Down

0 comments on commit c99ece3

Please sign in to comment.