1
1
mirror of https://github.com/wader/fq.git synced 2024-12-26 15:02:28 +03:00
fq/format/json/json.go

47 lines
1016 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/bitio"
"github.com/wader/fq/pkg/decode"
"github.com/wader/fq/pkg/scalar"
2020-06-08 03:29:51 +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() {
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{} {
br := d.RawLen(d.Len())
jd := stdjson.NewDecoder(bitio.NewIOReader(br))
var s scalar.S
if err := jd.Decode(&s.Actual); err != nil {
d.Fatalf(err.Error())
2020-06-08 03:29:51 +03:00
}
switch s.Actual.(type) {
2020-06-08 03:29:51 +03:00
case map[string]interface{},
[]interface{}:
default:
d.Fatalf("root not object or array")
2020-06-08 03:29:51 +03:00
}
d.Value.V = &s
d.Value.Range.Len = d.Len()
2020-06-08 03:29:51 +03:00
return nil
}