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"
|
2022-01-24 23:21:48 +03:00
|
|
|
"github.com/wader/fq/pkg/bitio"
|
2021-08-17 13:06:32 +03:00
|
|
|
"github.com/wader/fq/pkg/decode"
|
2021-12-02 00:48:25 +03:00
|
|
|
"github.com/wader/fq/pkg/scalar"
|
2020-06-08 03:29:51 +03:00
|
|
|
)
|
|
|
|
|
2021-11-24 17:25:27 +03:00
|
|
|
// TODO: should read multiple json values or just one?
|
|
|
|
// TODO: root not array/struct how to add unknown gaps?
|
|
|
|
// TODO: ranges not end up correct
|
|
|
|
// TODO: use jd.InputOffset() * 8?
|
|
|
|
|
2020-06-08 03:29:51 +03:00
|
|
|
func init() {
|
2021-11-17 18:46:10 +03:00
|
|
|
registry.MustRegister(decode.Format{
|
2020-06-08 03:29:51 +03:00
|
|
|
Name: format.JSON,
|
|
|
|
Description: "JSON",
|
|
|
|
ProbeOrder: 100, // last
|
|
|
|
Groups: []string{format.PROBE},
|
|
|
|
DecodeFn: decodeJSON,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func decodeJSON(d *decode.D, in interface{}) interface{} {
|
2022-01-24 23:21:48 +03:00
|
|
|
br := d.RawLen(d.Len())
|
|
|
|
jd := stdjson.NewDecoder(bitio.NewIOReader(br))
|
2021-12-02 00:48:25 +03:00
|
|
|
var s scalar.S
|
2021-11-03 19:19:33 +03:00
|
|
|
if err := jd.Decode(&s.Actual); err != nil {
|
2021-11-17 18:26:13 +03:00
|
|
|
d.Fatalf(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-17 18:26:13 +03:00
|
|
|
d.Fatalf("root not object or array")
|
2020-06-08 03:29:51 +03:00
|
|
|
}
|
2021-11-24 17:25:27 +03:00
|
|
|
|
2021-12-03 02:06:11 +03:00
|
|
|
d.Value.V = &s
|
2021-11-24 17:25:27 +03:00
|
|
|
d.Value.Range.Len = d.Len()
|
2020-06-08 03:29:51 +03:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|