Skip to content

Commit

Permalink
Merge pull request #164 from jgoodw1n/umlauts-in-member-names
Browse files Browse the repository at this point in the history
Update member name regexp to support UTF8 alphanumeric characters
  • Loading branch information
joshbuddy committed Sep 27, 2023
2 parents 2a1fe15 + c0e0640 commit b6da998
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 4 deletions.
4 changes: 2 additions & 2 deletions lib/jsonpath.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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}]"
Expand Down Expand Up @@ -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)
Expand Down
16 changes: 14 additions & 2 deletions test/test_jsonpath.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit b6da998

Please sign in to comment.