Permalink
Browse files

PATH option now works correctly

* 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
Kate Rhodes committed Nov 7, 2008
1 parent 162d29e commit db481bda0bc14d65c4366aa1b9ef07e0e9ba21e3
Showing with 36 additions and 5 deletions.
  1. +2 −3 README.mkdn
  2. +8 −2 lib/JSONPath.pm
  3. +26 −0 tests/JSONPathTest.pm
View
@@ -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.
@@ -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']",
@@ -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.
View
@@ -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;
}
View
@@ -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.