mirror of https://github.com/tidwall/gjson.git
Fix mod flatten including non-existent arrays
This commit is contained in:
parent
b61527bf1a
commit
c75c954102
28
gjson.go
28
gjson.go
|
@ -2176,11 +2176,6 @@ func parseAny(json string, i int, hit bool) (int, Result, bool) {
|
|||
return i, res, false
|
||||
}
|
||||
|
||||
var ( // used for testing
|
||||
testWatchForFallback bool
|
||||
testLastWasFallback bool
|
||||
)
|
||||
|
||||
// GetMany searches json for the multiple paths.
|
||||
// The return value is a Result array where the number of items
|
||||
// will be equal to the number of input paths.
|
||||
|
@ -2745,19 +2740,24 @@ func modFlatten(json, arg string) string {
|
|||
out = append(out, '[')
|
||||
var idx int
|
||||
res.ForEach(func(_, value Result) bool {
|
||||
var raw string
|
||||
if value.IsArray() {
|
||||
if deep {
|
||||
raw = unwrap(modFlatten(value.Raw, arg))
|
||||
} else {
|
||||
raw = unwrap(value.Raw)
|
||||
}
|
||||
} else {
|
||||
raw = value.Raw
|
||||
}
|
||||
raw = strings.TrimSpace(raw)
|
||||
if len(raw) > 0 {
|
||||
if idx > 0 {
|
||||
out = append(out, ',')
|
||||
}
|
||||
if value.IsArray() {
|
||||
if deep {
|
||||
out = append(out, unwrap(modFlatten(value.Raw, arg))...)
|
||||
} else {
|
||||
out = append(out, unwrap(value.Raw)...)
|
||||
}
|
||||
} else {
|
||||
out = append(out, value.Raw...)
|
||||
}
|
||||
out = append(out, raw...)
|
||||
idx++
|
||||
}
|
||||
return true
|
||||
})
|
||||
out = append(out, ']')
|
||||
|
|
|
@ -787,6 +787,12 @@ var manyJSON = ` {
|
|||
func combine(results []Result) string {
|
||||
return fmt.Sprintf("%v", results)
|
||||
}
|
||||
|
||||
var ( // used for testing
|
||||
testWatchForFallback bool
|
||||
testLastWasFallback bool
|
||||
)
|
||||
|
||||
func TestManyBasic(t *testing.T) {
|
||||
testWatchForFallback = true
|
||||
defer func() {
|
||||
|
@ -2222,3 +2228,8 @@ func TestSubpathsWithMultipaths(t *testing.T) {
|
|||
assert(t, Get(json, `#.@ugly`).Raw == `[{"a":1},{"a":2,"values":["a","b","c","d","e"]},true,["a","b","c","d","e"],4]`)
|
||||
assert(t, Get(json, `#.[0,3]`).Raw == `[[],[],[],["a","d"],[]]`)
|
||||
}
|
||||
|
||||
func TestFlattenRemoveNonExist(t *testing.T) {
|
||||
raw := Get("[[1],[2,[[],[3]],[4,[5],[],[[[6]]]]]]", `@flatten:{"deep":true}`).Raw
|
||||
assert(t, raw == "[1,2,3,4,5,6]")
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue