Fix trailing multiselector value

This commit is contained in:
tidwall 2019-11-02 14:52:36 -07:00
parent 94e070960b
commit c34bf81952
2 changed files with 30 additions and 12 deletions

View File

@ -1556,19 +1556,30 @@ func parseArray(c *parseContext, i int, path string) (int, bool) {
var jsons = make([]byte, 0, 64)
jsons = append(jsons, '[')
for j, k := 0, 0; j < len(alog); j++ {
_, res, ok := parseAny(c.json, alog[j], true)
if ok {
res := res.Get(rp.alogkey)
if res.Exists() {
if k > 0 {
jsons = append(jsons, ',')
idx := alog[j]
for idx < len(c.json) {
switch c.json[idx] {
case ' ', '\t', '\r', '\n':
idx++
continue
}
break
}
if idx < len(c.json) && c.json[idx] != ']' {
_, res, ok := parseAny(c.json, idx, true)
if ok {
res := res.Get(rp.alogkey)
if res.Exists() {
if k > 0 {
jsons = append(jsons, ',')
}
raw := res.Raw
if len(raw) == 0 {
raw = res.String()
}
jsons = append(jsons, []byte(raw)...)
k++
}
raw := res.Raw
if len(raw) == 0 {
raw = res.String()
}
jsons = append(jsons, []byte(raw)...)
k++
}
}
}

View File

@ -2016,3 +2016,10 @@ func TestModifiersInMultipaths(t *testing.T) {
assert(t, res.Raw == exp)
}
func TestIssue141(t *testing.T) {
json := `{"data": [{"q": 11, "w": 12}, {"q": 21, "w": 22}, {"q": 31, "w": 32} ], "sql": "some stuff here"}`
assert(t, Get(json, "data.#").Int() == 3)
assert(t, Get(json, "data.#.{q}|@ugly").Raw == `[{"q":11},{"q":21},{"q":31}]`)
assert(t, Get(json, "data.#.q|@ugly").Raw == `[11,21,31]`)
}