1
1
mirror of https://github.com/wader/fq.git synced 2024-11-29 23:27:12 +03:00
fq/format/json/json.go
Mattias Wadman 8e0dde03d0 decode: Support multiple format args and some rename and refactor
This will allow passing both cli options and format options to sub decoder.
Ex: pass keylog option to a tls decoder when decoding a pcap.
Ex: pass decode options to a format inside a http body inside a pcap.

Add ArgAs method to lookup argument based on type. This also makes the format
decode function have same signature as sub decoders in the decode API.

This change decode.Format a bit:
DecodeFn is now just func(d *D) any
DecodeInArg renamed to DefaultInArg
2023-02-18 21:38:51 +01:00

116 lines
2.1 KiB
Go

package json
import (
"bytes"
"embed"
stdjson "encoding/json"
"errors"
"io"
"github.com/wader/fq/format"
"github.com/wader/fq/internal/colorjson"
"github.com/wader/fq/pkg/bitio"
"github.com/wader/fq/pkg/decode"
"github.com/wader/fq/pkg/interp"
"github.com/wader/fq/pkg/scalar"
"github.com/wader/gojq"
)
//go:embed json.jq
var jsonFS embed.FS
func init() {
interp.RegisterFormat(decode.Format{
Name: format.JSON,
Description: "JavaScript Object Notation",
ProbeOrder: format.ProbeOrderTextJSON,
Groups: []string{format.PROBE},
DecodeFn: decodeJSON,
Functions: []string{"_todisplay"},
})
interp.RegisterFS(jsonFS)
interp.RegisterFunc1("_to_json", toJSON)
}
func decodeJSONEx(d *decode.D, lines bool) any {
var vs []any
// keep in sync with gojq fromJSON
jd := stdjson.NewDecoder(bitio.NewIOReader(d.RawLen(d.Len())))
jd.UseNumber()
foundEOF := false
for {
var v any
if err := jd.Decode(&v); err != nil {
if errors.Is(err, io.EOF) {
foundEOF = true
if lines {
break
} else if len(vs) == 1 {
break
}
} else if lines {
d.Fatalf(err.Error())
}
break
}
vs = append(vs, v)
}
if !lines && (len(vs) != 1 || !foundEOF) {
d.Fatalf("trialing data after top-level value")
}
var s scalar.Any
if lines {
if len(vs) == 0 {
d.Fatalf("not lines found")
}
s.Actual = gojq.NormalizeNumbers(vs)
} else {
s.Actual = gojq.NormalizeNumbers(vs[0])
}
d.Value.V = &s
d.Value.Range.Len = d.Len()
return nil
}
func decodeJSON(d *decode.D) any {
return decodeJSONEx(d, false)
}
type ToJSONOpts struct {
Indent int
}
// TODO: share with interp code
func makeEncoder(opts ToJSONOpts) *colorjson.Encoder {
return colorjson.NewEncoder(colorjson.Options{
Color: false,
Tab: false,
Indent: opts.Indent,
ValueFn: func(v any) any {
switch v := v.(type) {
case gojq.JQValue:
return v.JQValueToGoJQ()
default:
return v
}
},
Colors: colorjson.Colors{},
})
}
func toJSON(_ *interp.Interp, c any, opts ToJSONOpts) any {
cj := makeEncoder(opts)
bb := &bytes.Buffer{}
if err := cj.Marshal(c, bb); err != nil {
return err
}
return bb.String()
}