Skip to content

Commit

Permalink
Ignore everything within quotes while parsing
Browse files Browse the repository at this point in the history
Fixes #11
  • Loading branch information
mogelbrod committed Jun 4, 2020
1 parent 911d719 commit 9fe20e1
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 8 deletions.
12 changes: 8 additions & 4 deletions autoload/jsonpath.vim
Original file line number Diff line number Diff line change
Expand Up @@ -113,14 +113,18 @@ function! jsonpath#scan_buffer_vimscript(search_for, to_line, to_column, from_li
elseif char ==# '\'
let escaped = 1

elseif quoted && in_key && char !=# '"'
let key .= char
elseif quoted
if char ==# '"'
let quoted = 0
elseif in_key
let key .= char
endif

elseif char ==# '"'
if in_key && !quoted
let quoted = 1
if in_key
let key = ''
endif
let quoted = quoted ? 0 : 1

elseif char ==# ':'
" Assume new object if encountering key outside root
Expand Down
11 changes: 7 additions & 4 deletions jsonpath.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,13 +60,16 @@ def scan_stream(stream, path=[], line=-1, column=-1, from_line=1, verbose=False)
if quoted and in_key:
key += decoded

elif quoted and in_key and char != '"':
key += char
elif quoted:
if char == '"':
quoted = False
elif in_key:
key += char

elif char == '"':
if in_key and not quoted:
quoted = True
if in_key:
key = ""
quoted = not quoted

elif char == ":":
# Assume new object if encountering key outside root
Expand Down
4 changes: 4 additions & 0 deletions tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ t tests/restcountries.json -l 17806:18 "219.capital"
t tests/restcountries.json -l 6963:8 "85.latlng.0"
t tests/unicode.json -l 1:27 "emoji"
t tests/unicode.json -l 1:38 "👍"
t tests/json-in-string.json -l 3:22 "templateString"
t tests/json-in-string.json -l 3:29 "templateString"
t tests/json-in-string.json -l 5:48 "json"
t tests/json-in-string.json -l 6:12 "last"

# Find offset for path
t tests/object.json "first" "2:12"
Expand Down
7 changes: 7 additions & 0 deletions tests/json-in-string.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"string": "hello",
"templateString": "foo-{{timestamp}}",
"anotherString": "world",
"json": "{\"key\": \"value\", \"nested\": {\"object\": true}}",
"last": "string"
}

0 comments on commit 9fe20e1

Please sign in to comment.