diff --git a/lib/jsonpath.rb b/lib/jsonpath.rb index 77a95ad..9166b63 100644 --- a/lib/jsonpath.rb +++ b/lib/jsonpath.rb @@ -16,7 +16,7 @@ def initialize(path, opts = nil) scanner = StringScanner.new(path) @path = [] until scanner.eos? - if token = scanner.scan(/\$|@\B|\*|\.\./) + if token = scanner.scan(/\$\B|@\B|\*|\.\./) @path << token elsif token = scanner.scan(/[\$@a-zA-Z0-9:_-]+/) @path << "['#{token}']" diff --git a/test/test_jsonpath.rb b/test/test_jsonpath.rb index ff530fc..5418fff 100644 --- a/test/test_jsonpath.rb +++ b/test/test_jsonpath.rb @@ -99,7 +99,7 @@ def test_use_first end def test_counting - assert_equal 50, JsonPath.new('$..*').on(@object).to_a.size + assert_equal 54, JsonPath.new('$..*').on(@object).to_a.size end def test_space_in_path @@ -231,6 +231,21 @@ def test_support_filter_by_object_childnode_value assert_equal [], JsonPath.new("$.data[?(@.type == 'admins')]").on(data) end + def test_support_at_sign_in_member_names + assert_equal [@object['store']['@id']], JsonPath.new("$.store.@id").on(@object) + end + + def test_support_dollar_sign_in_member_names + assert_equal [@object['store']['$meta-data']], + JsonPath.new("$.store.$meta-data").on(@object) + end + + def test_support_underscore_in_member_names + assert_equal [@object['store']['_links']], + JsonPath.new("$.store._links").on(@object) + end + + def example_object { 'store' => { 'book' => [ @@ -278,7 +293,10 @@ def example_object 'single-speed' => 'no', '2seater' => 'yes', 'make:model' => 'Zippy Sweetwheeler' - } + }, + "@id" => "http://example.org/store/42", + "$meta-data" => "whatevs", + "_links" => { "self" => {} } } } end end