Skip to content

Commit 139787e

Browse files
symbolize_keys option doesn't work as the README says
1 parent 3102fb3 commit 139787e

File tree

2 files changed

+11
-1
lines changed

2 files changed

+11
-1
lines changed

lib/jsonpath.rb

+5-1
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ def join(join_path)
8080

8181
def on(obj_or_str, opts = {})
8282
a = enum_on(obj_or_str).to_a
83-
if opts[:symbolize_keys]
83+
if symbolize_keys?(opts)
8484
a.map! do |e|
8585
e.each_with_object({}) { |(k, v), memo| memo[k.to_sym] = v; }
8686
end
@@ -153,4 +153,8 @@ def set_max_nesting
153153
return unless @opts[:max_nesting].is_a?(Integer) && @opts[:max_nesting] > MAX_NESTING_ALLOWED
154154
@opts[:max_nesting] = false
155155
end
156+
157+
def symbolize_keys?(opts)
158+
opts.fetch(:symbolize_keys, @opts&.dig(:symbolize_keys))
159+
end
156160
end

test/test_jsonpath.rb

+6
Original file line numberDiff line numberDiff line change
@@ -1312,4 +1312,10 @@ def test_extractore_with_dollar_key
13121312
assert_equal ["success"], JsonPath.on(json, "$.test.$")
13131313
assert_equal ["123"], JsonPath.on(json, "$.test.a")
13141314
end
1315+
1316+
def test_symbolize_key
1317+
data = { "store" => { "book" => [{"category" => "test"}]}}
1318+
assert_equal [{"category": "reference"}], JsonPath.new('$..book[0]', symbolize_keys: true).on(data)
1319+
assert_equal [{"category": "reference"}], JsonPath.new('$..book[0]').on(data, symbolize_keys: true)
1320+
end
13151321
end

0 commit comments

Comments
 (0)