diff --git a/lib/jsonpath.rb b/lib/jsonpath.rb index 914b44f..0bd509d 100644 --- a/lib/jsonpath.rb +++ b/lib/jsonpath.rb @@ -32,7 +32,7 @@ def initialize(path, opts = {}) until scanner.eos? if (token = scanner.scan(/\$\B|@\B|\*|\.\./)) @path << token - elsif (token = scanner.scan(/[$@a-zA-Z0-9:{}_-]+/)) + elsif (token = scanner.scan(/[$@\p{Alnum}:{}_ -]+/)) @path << "['#{token}']" elsif (token = scanner.scan(/'(.*?)'/)) @path << "[#{token}]" @@ -87,7 +87,7 @@ def on(obj_or_str, opts = {}) end a end - + def self.fetch_all_path(obj) all_paths = ['$'] find_path(obj, '$', all_paths, obj.class == Array) diff --git a/test/test_jsonpath.rb b/test/test_jsonpath.rb index 6bae8e7..5ab02d7 100644 --- a/test/test_jsonpath.rb +++ b/test/test_jsonpath.rb @@ -245,7 +245,7 @@ def test_use_first end def test_counting - assert_equal 57, JsonPath.new('$..*').on(@object).to_a.size + assert_equal 59, JsonPath.new('$..*').on(@object).to_a.size end def test_space_in_path @@ -475,6 +475,16 @@ def test_support_underscore_in_member_names JsonPath.new('$.store._links').on(@object) end + def test_support_for_umlauts_in_member_names + assert_equal [@object['store']['Übermorgen']], + JsonPath.new('$.store.Übermorgen').on(@object) + end + + def test_support_for_spaces_in_member_name + assert_equal [@object['store']['Title Case']], + JsonPath.new('$.store.Title Case').on(@object) + end + def test_dig_return_string assert_equal ['asdf'], JsonPath.new("$.store.book..tags[?(@ == 'asdf')]").on(@object) assert_equal [], JsonPath.new("$.store.book..tags[?(@ == 'not_asdf')]").on(@object) @@ -1275,10 +1285,12 @@ def example_object }, '@id' => 'http://example.org/store/42', '$meta-data' => 'whatevs', + 'Übermorgen' => 'The day after tomorrow', + 'Title Case' => 'A title case string', '_links' => { 'self' => {} } } } end - + def test_fetch_all_path data = { "foo" => nil,