Skip to content
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
7 changes: 5 additions & 2 deletions lib/jsonpath.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ def initialize(path, opts = nil)
@opts = opts
scanner = StringScanner.new(path)
@path = []
bracket_count = 0
while not scanner.eos?
if token = scanner.scan(/\$/)
@path << token
Expand All @@ -33,11 +32,15 @@ def initialize(path, opts = nil)
elsif t = scanner.scan(/\]/)
token << t
count -= 1
elsif t = scanner.scan(/[^\[\]]*/)
elsif t = scanner.scan(/[^\[\]]+/)
token << t
elsif scanner.eos?
raise ArgumentError, 'unclosed bracket'
end
end
@path << token
elsif token = scanner.scan(/\]/)
raise ArgumentError, 'unmatched closing bracket'
elsif token = scanner.scan(/\.\./)
@path << token
elsif scanner.scan(/\./)
Expand Down
9 changes: 9 additions & 0 deletions test/test_jsonpath.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,15 @@ def setup
@object2 = example_object
end

def test_bracket_matching
assert_raises(ArgumentError) {
JsonPath.new('$.store.book[0')
}
assert_raises(ArgumentError) {
JsonPath.new('$.store.book[0]]')
}
end

def test_lookup_direct_path
assert_equal 4, JsonPath.new('$.store.*').on(@object).first['book'].size
end
Expand Down