1
1
mirror of https://github.com/wader/fq.git synced 2024-11-23 18:56:52 +03:00
fq/format/json/json.go

39 lines
829 B
Go
Raw Normal View History

2020-06-08 03:29:51 +03:00
package json
import (
stdjson "encoding/json"
"github.com/wader/fq/format"
"github.com/wader/fq/format/registry"
"github.com/wader/fq/pkg/decode"
2020-06-08 03:29:51 +03:00
)
func init() {
registry.MustRegister(&decode.Format{
Name: format.JSON,
Description: "JSON",
ProbeOrder: 100, // last
Groups: []string{format.PROBE},
DecodeFn: decodeJSON,
})
}
func decodeJSON(d *decode.D, in interface{}) interface{} {
bb := d.BitBufLen(d.Len())
jd := stdjson.NewDecoder(bb)
if err := jd.Decode(&d.Value.V); err != nil {
d.Invalid(err.Error())
}
switch d.Value.V.(type) {
case map[string]interface{},
[]interface{}:
default:
d.Invalid("root not object or array")
}
// TODO: root not array/struct how to add unknown gaps?
// TODO: ranges not end up correct
d.Value.Range.Len = jd.InputOffset() * 8
return nil
}