Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#162 : symbolize_keys option doesn't work as the README says #165

Merged
merged 2 commits into from
Oct 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading