File: jsonl.go

package info (click to toggle)
fq 0.9.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 106,624 kB
  • sloc: xml: 2,835; makefile: 250; sh: 241; exp: 57; ansic: 21
file content (47 lines) | stat: -rw-r--r-- 912 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
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()
}