Skip to content

Commit 9fe20e1

Browse files
committed
Ignore everything within quotes while parsing
Fixes #11
1 parent 911d719 commit 9fe20e1

File tree

4 files changed

+26
-8
lines changed

4 files changed

+26
-8
lines changed

autoload/jsonpath.vim

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -113,14 +113,18 @@ function! jsonpath#scan_buffer_vimscript(search_for, to_line, to_column, from_li
113113
elseif char ==# '\'
114114
let escaped = 1
115115

116-
elseif quoted && in_key && char !=# '"'
117-
let key .= char
116+
elseif quoted
117+
if char ==# '"'
118+
let quoted = 0
119+
elseif in_key
120+
let key .= char
121+
endif
118122

119123
elseif char ==# '"'
120-
if in_key && !quoted
124+
let quoted = 1
125+
if in_key
121126
let key = ''
122127
endif
123-
let quoted = quoted ? 0 : 1
124128

125129
elseif char ==# ':'
126130
" Assume new object if encountering key outside root

jsonpath.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -60,13 +60,16 @@ def scan_stream(stream, path=[], line=-1, column=-1, from_line=1, verbose=False)
6060
if quoted and in_key:
6161
key += decoded
6262

63-
elif quoted and in_key and char != '"':
64-
key += char
63+
elif quoted:
64+
if char == '"':
65+
quoted = False
66+
elif in_key:
67+
key += char
6568

6669
elif char == '"':
67-
if in_key and not quoted:
70+
quoted = True
71+
if in_key:
6872
key = ""
69-
quoted = not quoted
7073

7174
elif char == ":":
7275
# Assume new object if encountering key outside root

tests.sh

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,10 @@ t tests/restcountries.json -l 17806:18 "219.capital"
3131
t tests/restcountries.json -l 6963:8 "85.latlng.0"
3232
t tests/unicode.json -l 1:27 "emoji"
3333
t tests/unicode.json -l 1:38 "👍"
34+
t tests/json-in-string.json -l 3:22 "templateString"
35+
t tests/json-in-string.json -l 3:29 "templateString"
36+
t tests/json-in-string.json -l 5:48 "json"
37+
t tests/json-in-string.json -l 6:12 "last"
3438

3539
# Find offset for path
3640
t tests/object.json "first" "2:12"

tests/json-in-string.json

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"string": "hello",
3+
"templateString": "foo-{{timestamp}}",
4+
"anotherString": "world",
5+
"json": "{\"key\": \"value\", \"nested\": {\"object\": true}}",
6+
"last": "string"
7+
}

0 commit comments

Comments
 (0)