mirror of
https://github.com/wader/fq.git
synced 2024-11-23 18:56:52 +03:00
39 lines
829 B
Go
39 lines
829 B
Go
package json
|
|
|
|
import (
|
|
stdjson "encoding/json"
|
|
|
|
"github.com/wader/fq/format"
|
|
"github.com/wader/fq/format/registry"
|
|
"github.com/wader/fq/pkg/decode"
|
|
)
|
|
|
|
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
|
|
}
|