Fix trailing curly bracket

close #36
This commit is contained in:
tidwall 2020-04-01 05:03:04 -07:00
parent fcc1b99819
commit 11cb24d842
2 changed files with 31 additions and 1 deletions

View File

@ -341,7 +341,13 @@ func appendRawPaths(buf []byte, jstr string, paths []pathResult, raw string,
default: default:
return nil, &errorType{"json must be an object or array"} return nil, &errorType{"json must be an object or array"}
case '{': case '{':
buf = append(buf, jsres.Raw[:len(jsres.Raw)-1]...) end := len(jsres.Raw) - 1
for ; end > 0; end-- {
if jsres.Raw[end] == '}' {
break
}
}
buf = append(buf, jsres.Raw[:end]...)
if comma { if comma {
buf = append(buf, ',') buf = append(buf, ',')
} }

View File

@ -310,3 +310,27 @@ func TestDeleteDotKeyIssue19(t *testing.T) {
t.Fatalf("expected '%v', got '%v'", `{"data":{"key1":"value1"}}`, json) t.Fatalf("expected '%v', got '%v'", `{"data":{"key1":"value1"}}`, json)
} }
} }
func TestIssue36(t *testing.T) {
var json = `
{
"size": 1000
}
`
var raw = `
{
"sample": "hello"
}
`
_ = raw
if true {
json, _ = SetRaw(json, "aggs", raw)
}
if !gjson.Valid(json) {
t.Fatal("invalid json")
}
res := gjson.Get(json, "aggs.sample").String()
if res != "hello" {
t.Fatal("unexpected result")
}
}