Skip to content
This repository has been archived by the owner on Nov 17, 2022. It is now read-only.

Commit

Permalink
PATH option now works correctly
Browse files Browse the repository at this point in the history
* Like the original JSONPath it returns an array of strings.
	Within these strings is a square bracket style notation
	with single quoted strings and numbers with no quotes.

	I am undecided if this should be changed to escaped double
	quotes around strings to match the JSON spec.
* Added unit tests for a couple PATH expressions.
  • Loading branch information
Kate Rhodes committed Nov 7, 2008
1 parent 162d29e commit db481bd
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 5 deletions.
5 changes: 2 additions & 3 deletions README.mkdn
Expand Up @@ -16,7 +16,7 @@ or the bracket notation

$['store']['book'][0]['title']

for input pathes. Internal or output pathes will always be converted to the more general bracket–notation.
for input pathes. Internal or output pathes will always be converted to the more general bracket notation.

JSONPath allows the wildcard symbol * for member names and array indices. It borrows the descendant operator '..' from E4X and the array slice syntax proposal [start:end:step] from ECMASCRIPT 4.

Expand Down Expand Up @@ -135,7 +135,7 @@ the example results in the following arrays:

]

res2 (CURRENTLY BROKEN IN THE PERL VERSION):
res2:
[ "$['store']['book'][0]['author']",
"$['store']['book'][1]['author']",
"$['store']['book'][2]['author']",
Expand All @@ -146,7 +146,6 @@ Please note, that the return value of jsonPath is an array, which is also a vali

Issues

* PATH option doesn't produce useful results
* Currently only single quotes allowed inside of JSONPath expressions.
* Script expressions inside of JSONPath locations are currently not recursively evaluated by jsonPath. Only the global $ and local @ symbols are expanded by a simple regular expression.
* An alternative for jsonPath to return false in case of no match may be to return an empty array in future.
Expand Down
10 changes: 8 additions & 2 deletions lib/JSONPath.pm
Expand Up @@ -109,8 +109,14 @@ sub as_path(){
my @x = split(/;/, $path);
my $p = '$';
#the JS and PHP versions of this are totally whack
foreach my $piece (@x){
$p .= "[$x[$piece]]";
#foreach my $piece (@x){
for(my $i =1; $i <= $#x; $i++){
my $piece = $x[$i];
if ($piece =~ m/^\d+$/){
$p .= "[$piece]";
} else {
$p .= "['$piece']";
}
}
return $p;
}
Expand Down
26 changes: 26 additions & 0 deletions tests/JSONPathTest.pm
Expand Up @@ -167,4 +167,30 @@ sub test_array_retreival(){

}

sub test_path_operations(){
my $self = shift;
my $jp = JSONPath->new();
my $raw_result = undef;
my @result = undef;

$raw_result = $jp->run(\%test_structure, '$..author', {'result_type' => 'PATH'});
$self->assert($raw_result != 0);
@result = @{$raw_result};
$self->assert_equals(3, $#result);

$self->assert_equals("\$['store']['book'][0]['author']", $result[0]);
$self->assert_equals("\$['store']['book'][1]['author']", $result[1]);
$self->assert_equals("\$['store']['book'][2]['author']", $result[2]);
$self->assert_equals("\$['store']['book'][3]['author']", $result[3]);


$raw_result = $jp->run(\%test_structure, '$.store.!', {'result_type' => 'PATH'});
$self->assert($raw_result != 0);
@result = @{$raw_result};
$self->assert_equals(1, $#result);

$self->assert_equals("\$['store']", $result[0]);
$self->assert_equals("\$['store']", $result[1]);
}

return 1;

0 comments on commit db481bd

Please sign in to comment.