super-graph/jsn/tree.go

38 lines
567 B
Go
Raw Permalink Normal View History

2019-09-05 06:09:56 +02:00
package jsn
import (
"bytes"
"encoding/json"
)
2019-12-25 07:24:30 +01:00
func Tree(v []byte) (map[string]json.RawMessage, bool, error) {
2019-09-05 06:09:56 +02:00
dec := json.NewDecoder(bytes.NewReader(v))
array := false
// read open bracket
for i := range v {
if v[i] != ' ' {
array = (v[i] == '[')
break
}
}
if array {
if _, err := dec.Token(); err != nil {
return nil, false, err
}
}
// while the array contains values
2019-12-25 07:24:30 +01:00
var m map[string]json.RawMessage
2019-09-05 06:09:56 +02:00
// decode an array value (Message)
err := dec.Decode(&m)
if err != nil {
return nil, false, err
}
return m, array, nil
}