Skip to content

Commit

Permalink
Merge pull request #165 from anupama-kumari/master
Browse files Browse the repository at this point in the history
#162 : symbolize_keys option doesn't work as the README says
  • Loading branch information
joshbuddy committed Oct 12, 2023
2 parents 3102fb3 + b8bd29f commit 96210e5
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 1 deletion.
6 changes: 5 additions & 1 deletion lib/jsonpath.rb
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ def join(join_path)

def on(obj_or_str, opts = {})
a = enum_on(obj_or_str).to_a
if opts[:symbolize_keys]
if symbolize_keys?(opts)
a.map! do |e|
e.each_with_object({}) { |(k, v), memo| memo[k.to_sym] = v; }
end
Expand Down Expand Up @@ -153,4 +153,8 @@ def set_max_nesting
return unless @opts[:max_nesting].is_a?(Integer) && @opts[:max_nesting] > MAX_NESTING_ALLOWED
@opts[:max_nesting] = false
end

def symbolize_keys?(opts)
opts.fetch(:symbolize_keys, @opts&.dig(:symbolize_keys))
end
end
6 changes: 6 additions & 0 deletions test/test_jsonpath.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1312,4 +1312,10 @@ def test_extractore_with_dollar_key
assert_equal ["success"], JsonPath.on(json, "$.test.$")
assert_equal ["123"], JsonPath.on(json, "$.test.a")
end

def test_symbolize_key
data = { "store" => { "book" => [{"category" => "reference"}]}}
assert_equal [{"category": "reference"}], JsonPath.new('$..book[0]', symbolize_keys: true).on(data)
assert_equal [{"category": "reference"}], JsonPath.new('$..book[0]').on(data, symbolize_keys: true)
end
end

0 comments on commit 96210e5

Please sign in to comment.