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

Restore support for legacy unquoted string literals #32

Merged
merged 1 commit into from
Jul 7, 2016
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
10 changes: 7 additions & 3 deletions lib/jmespath/lexer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ def tokenize(expression)
token = inside(chars, '"', T_QUOTED_IDENTIFIER)
if token.type == T_QUOTED_IDENTIFIER
token.value = "\"#{token.value}\""
token = parse_json(token)
token = parse_json(token, true)
end
tokens << token
when STATE_EQ
Expand Down Expand Up @@ -295,9 +295,13 @@ def inside(chars, delim, type)
Token.new(type, buffer.join, position)
end

def parse_json(token)
def parse_json(token, quoted=false)
begin
token.value = JSON.load(token.value)
if quoted
token.value = JSON.load(token.value)
else
token.value = JSON.load(token.value) rescue JSON.load(sprintf('"%s"', token.value.lstrip))
end
rescue JSON::ParserError
token.type = T_UNKNOWN
end
Expand Down
2 changes: 1 addition & 1 deletion spec/compliance_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
SimpleCov.command_name('test:compliance')

describe 'Compliance' do
Dir.glob('spec/compliance/*.json').each do |path|
Dir.glob('spec/{compliance,legacy}/*.json').each do |path|

test_file = File.basename(path).split('.').first
next if test_file == 'benchmarks'
Expand Down
56 changes: 56 additions & 0 deletions spec/legacy/literal.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
[
{
"given": {
"foo": [{"name": "a"}, {"name": "b"}],
"bar": {"baz": "qux"}
},
"cases": [
{
"expression": "`foo`",
"result": "foo"
},
{
"comment": "Double quotes must be escaped.",
"expression": "`foo\\\"quote`",
"result": "foo\"quote"
},
{
"expression": "`✓`",
"result": "✓"
},
{
"comment": "Double quote in literal",
"expression": "`foo\\\"bar`",
"result": "foo\"bar"
},
{
"expression": "`1\\``",
"result": "1`"
},
{
"comment": "Multiple literal expressions with escapes",
"expression": "`\\\\`.{a:`b`}",
"result": {"a": "b"}
}
]
},
{
"comment": "Literals",
"given": {"type": "object"},
"cases": [
{
"expression": "`foo`",
"result": "foo"
},
{
"expression": "` foo`",
"result": "foo"
},
{
"comment": "Literal on RHS of subexpr not allowed",
"expression": "foo.`bar`",
"error": "syntax"
}
]
}
]