2020-06-08 03:29:51 +03:00
|
|
|
package json
|
|
|
|
|
|
|
|
import (
|
|
|
|
stdjson "encoding/json"
|
2021-08-17 13:06:32 +03:00
|
|
|
|
|
|
|
"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{} {
|
2021-11-05 17:04:26 +03:00
|
|
|
bb := d.RawLen(d.Len())
|
2020-06-08 03:29:51 +03:00
|
|
|
jd := stdjson.NewDecoder(bb)
|
2021-11-03 19:19:33 +03:00
|
|
|
var s decode.Scalar
|
|
|
|
if err := jd.Decode(&s.Actual); err != nil {
|
2021-11-16 19:11:26 +03:00
|
|
|
d.Fatal(err.Error())
|
2020-06-08 03:29:51 +03:00
|
|
|
}
|
2021-11-03 19:19:33 +03:00
|
|
|
switch s.Actual.(type) {
|
2020-06-08 03:29:51 +03:00
|
|
|
case map[string]interface{},
|
|
|
|
[]interface{}:
|
|
|
|
default:
|
2021-11-16 19:11:26 +03:00
|
|
|
d.Fatal("root not object or array")
|
2020-06-08 03:29:51 +03:00
|
|
|
}
|
|
|
|
// TODO: root not array/struct how to add unknown gaps?
|
|
|
|
// TODO: ranges not end up correct
|
2021-11-03 19:19:33 +03:00
|
|
|
d.Value.V = s
|
2020-06-08 03:29:51 +03:00
|
|
|
d.Value.Range.Len = jd.InputOffset() * 8
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|