1
1
mirror of https://github.com/wader/fq.git synced 2024-11-27 14:14:58 +03:00
fq/format/json/json.go
Mattias Wadman 9d116df799 decode: Rework use of TryFieldReaderRangeFormat
Handle failure better
2021-11-24 15:25:27 +01:00

45 lines
934 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"
)
// 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?
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.RawLen(d.Len())
jd := stdjson.NewDecoder(bb)
var s decode.Scalar
if err := jd.Decode(&s.Actual); err != nil {
d.Fatalf(err.Error())
}
switch s.Actual.(type) {
case map[string]interface{},
[]interface{}:
default:
d.Fatalf("root not object or array")
}
d.Value.V = s
d.Value.Range.Len = d.Len()
return nil
}