mirror of https://github.com/tidwall/gjson.git
Allow for Index > 0 on path compontent that are not modifiers.
This commit fixes an issue where non-modifier path components such as '@hello' return 0 for the Result.Index value.
This commit is contained in:
parent
980f12c60e
commit
475b4036c3
25
gjson.go
25
gjson.go
|
@ -775,7 +775,7 @@ func parseArrayPath(path string) (r arrayPathResult) {
|
|||
}
|
||||
if path[i] == '.' {
|
||||
r.part = path[:i]
|
||||
if !r.arrch && i < len(path)-1 && isDotPiperChar(path[i+1]) {
|
||||
if !r.arrch && i < len(path)-1 && isDotPiperChar(path[i+1:]) {
|
||||
r.pipe = path[i+1:]
|
||||
r.piped = true
|
||||
} else {
|
||||
|
@ -936,8 +936,23 @@ right:
|
|||
}
|
||||
|
||||
// peek at the next byte and see if it's a '@', '[', or '{'.
|
||||
func isDotPiperChar(c byte) bool {
|
||||
return !DisableModifiers && (c == '@' || c == '[' || c == '{')
|
||||
func isDotPiperChar(s string) bool {
|
||||
if DisableModifiers {
|
||||
return false
|
||||
}
|
||||
c := s[0]
|
||||
if c == '@' {
|
||||
// check that the next component is *not* a modifier.
|
||||
i := 1
|
||||
for ; i < len(s); i++ {
|
||||
if s[i] == '.' || s[i] == '|' {
|
||||
break
|
||||
}
|
||||
}
|
||||
_, ok := modifiers[s[1:i]]
|
||||
return ok
|
||||
}
|
||||
return c == '[' || c == '{'
|
||||
}
|
||||
|
||||
type objectPathResult struct {
|
||||
|
@ -959,7 +974,7 @@ func parseObjectPath(path string) (r objectPathResult) {
|
|||
}
|
||||
if path[i] == '.' {
|
||||
r.part = path[:i]
|
||||
if i < len(path)-1 && isDotPiperChar(path[i+1]) {
|
||||
if i < len(path)-1 && isDotPiperChar(path[i+1:]) {
|
||||
r.pipe = path[i+1:]
|
||||
r.piped = true
|
||||
} else {
|
||||
|
@ -989,7 +1004,7 @@ func parseObjectPath(path string) (r objectPathResult) {
|
|||
continue
|
||||
} else if path[i] == '.' {
|
||||
r.part = string(epart)
|
||||
if i < len(path)-1 && isDotPiperChar(path[i+1]) {
|
||||
if i < len(path)-1 && isDotPiperChar(path[i+1:]) {
|
||||
r.pipe = path[i+1:]
|
||||
r.piped = true
|
||||
} else {
|
||||
|
|
|
@ -2543,3 +2543,14 @@ func TestJSONString(t *testing.T) {
|
|||
testJSONString(t, string(buf[:]))
|
||||
}
|
||||
}
|
||||
|
||||
func TestIndexAtSymbol(t *testing.T) {
|
||||
json := `{
|
||||
"@context": {
|
||||
"rdfs": "http://www.w3.org/2000/01/rdf-schema#",
|
||||
"@vocab": "http://schema.org/",
|
||||
"sh": "http://www.w3.org/ns/shacl#"
|
||||
}
|
||||
}`
|
||||
assert(t, Get(json, "@context.@vocab").Index == 85)
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue