Skip to content
Closed
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
12 changes: 8 additions & 4 deletions lib/jsonpath/enumerable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,17 @@ def each(context = @object, key = nil, pos = 0, &blk)
when ??
raise "Cannot use ?(...) unless eval is enabled" unless allow_eval?
case node
when Hash, Array
(node.is_a?(Hash) ? node.keys : (0..node.size)).each do |e|
@_current_node = node[e]
when Array
node.size.times do |index|
@_current_node = node[index]
if process_function_or_literal(sub_path[1, sub_path.size - 1])
each(@_current_node, nil, pos + 1, &blk)
end
end
when Hash
if process_function_or_literal(sub_path[1, sub_path.size - 1])
each(@_current_node, nil, pos + 1, &blk)
end
else
yield node if process_function_or_literal(sub_path[1, sub_path.size - 1])
end
Expand All @@ -54,7 +58,7 @@ def each(context = @object, key = nil, pos = 0, &blk)
end_idx = (array_args[1] && process_function_or_literal(array_args[1], -1) || (sub_path.count(':') == 0 ? start_idx : -1))
next unless end_idx
if start_idx == end_idx
next unless start_idx < node.size
next unless start_idx < node.size
end
end
start_idx %= node.size
Expand Down
15 changes: 13 additions & 2 deletions test/test_jsonpath.rb
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ def test_wildcard_empty_array
assert_equal [], JsonPath.on(object, "$..bicycle.tire[*]")
end

def test_support_filter_by_childnode_value
def test_support_filter_by_array_childnode_value
assert_equal [@object['store']['book'][3]], JsonPath.new("$..book[?(@.price > 20)]").on(@object)
end

Expand All @@ -171,7 +171,18 @@ def test_support_filter_by_childnode_value_and_select_child_key
def test_support_filter_by_childnode_value_over_childnode_and_select_child_key
assert_equal ["Osennie Vizity"], JsonPath.new("$..book[?(@.written.year == 1996)].title").on(@object)
end


def test_support_filter_by_object_childnode_value
data = {
"data" => {
"type" => "users",
"id" => "123"
}
}
assert_equal [{"type"=>"users", "id"=>"123"}], JsonPath.new("$.data[?(@.type == 'users')]").on(data)
assert_equal [] , JsonPath.new("$.data[?(@.type == 'admins')]").on(data)
end

def example_object
{ "store"=> {
"book" => [
Expand Down