1
1
mirror of https://github.com/wader/fq.git synced 2024-11-26 21:55:57 +03:00
fq/format/json/jsonl.go
2023-05-01 13:19:04 +02:00

48 lines
912 B
Go

package json
import (
"bytes"
"embed"
"github.com/wader/fq/format"
"github.com/wader/fq/pkg/decode"
"github.com/wader/fq/pkg/interp"
)
//go:embed jsonl.jq
var jsonlFS embed.FS
// TODO: not strictly JSONL, allows any whitespace between
func init() {
interp.RegisterFormat(
format.JSONL,
&decode.Format{
Description: "JavaScript Object Notation Lines",
ProbeOrder: format.ProbeOrderTextFuzzy,
Groups: []*decode.Group{format.Probe},
DecodeFn: decodeJSONL,
Functions: []string{"_todisplay"},
})
interp.RegisterFS(jsonlFS)
interp.RegisterFunc0("to_jsonl", toJSONL)
}
func decodeJSONL(d *decode.D) any {
return decodeJSONEx(d, true)
}
func toJSONL(i *interp.Interp, c []any) any {
cj := makeEncoder(ToJSONOpts{})
bb := &bytes.Buffer{}
for _, v := range c {
if err := cj.Marshal(v, bb); err != nil {
return err
}
bb.WriteByte('\n')
}
return bb.String()
}