From 0225e64003f423218bdc60f78cbff63a797afe0f Mon Sep 17 00:00:00 2001 From: James Goodwin Date: Wed, 6 Sep 2023 19:50:07 -0400 Subject: [PATCH 1/2] Update member name regexp to support UTF8 alphabetic and numeric characters see: https://ruby-doc.org/3.0.6/Regexp.html#class-Regexp-label-Character+Properties --- lib/jsonpath.rb | 4 ++-- test/test_jsonpath.rb | 10 ++++++++-- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/lib/jsonpath.rb b/lib/jsonpath.rb index 238a7bd..f0e5fe1 100644 --- a/lib/jsonpath.rb +++ b/lib/jsonpath.rb @@ -30,7 +30,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}]" @@ -85,7 +85,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 db8a5e4..5fcbdbb 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 58, JsonPath.new('$..*').on(@object).to_a.size end def test_space_in_path @@ -475,6 +475,11 @@ 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_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) @@ -1246,10 +1251,11 @@ def example_object }, '@id' => 'http://example.org/store/42', '$meta-data' => 'whatevs', + 'Übermorgen' => 'The day after tomorrow', '_links' => { 'self' => {} } } } end - + def test_fetch_all_path data = { "foo" => nil, From c0e0640e354ff1ea228a85fa32612a76e518d80f Mon Sep 17 00:00:00 2001 From: James Goodwin Date: Mon, 11 Sep 2023 21:21:36 -0400 Subject: [PATCH 2/2] Update member name regexp to allow spaces --- lib/jsonpath.rb | 2 +- test/test_jsonpath.rb | 8 +++++++- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/lib/jsonpath.rb b/lib/jsonpath.rb index f0e5fe1..c832f15 100644 --- a/lib/jsonpath.rb +++ b/lib/jsonpath.rb @@ -30,7 +30,7 @@ def initialize(path, opts = {}) until scanner.eos? if (token = scanner.scan(/\$\B|@\B|\*|\.\./)) @path << token - elsif (token = scanner.scan(/[$@\p{Alnum}:{}_-]+/)) + elsif (token = scanner.scan(/[$@\p{Alnum}:{}_ -]+/)) @path << "['#{token}']" elsif (token = scanner.scan(/'(.*?)'/)) @path << "[#{token}]" diff --git a/test/test_jsonpath.rb b/test/test_jsonpath.rb index 5fcbdbb..1f9d688 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 58, JsonPath.new('$..*').on(@object).to_a.size + assert_equal 59, JsonPath.new('$..*').on(@object).to_a.size end def test_space_in_path @@ -480,6 +480,11 @@ def test_support_for_umlauts_in_member_names 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) @@ -1252,6 +1257,7 @@ 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