1
1
mirror of https://github.com/wader/fq.git synced 2024-11-30 09:58:13 +03:00
fq/format/json/json.go

120 lines
2.2 KiB
Go
Raw Normal View History

2020-06-08 03:29:51 +03:00
package json
import (
"bytes"
"embed"
2020-06-08 03:29:51 +03:00
stdjson "encoding/json"
"errors"
"fmt"
"io"
"math/big"
"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"
2020-06-08 03:29:51 +03:00
)
//go:embed json.jq
var jsonFS embed.FS
2020-06-08 03:29:51 +03:00
func init() {
interp.RegisterFormat(decode.Format{
2020-06-08 03:29:51 +03:00
Name: format.JSON,
Description: "JavaScript Object Notation",
ProbeOrder: format.ProbeOrderTextJSON,
2020-06-08 03:29:51 +03:00
Groups: []string{format.PROBE},
DecodeFn: decodeJSON,
Functions: []string{"_todisplay"},
2020-06-08 03:29:51 +03:00
})
interp.RegisterFS(jsonFS)
interp.RegisterFunc1("_tojson", toJSON)
2020-06-08 03:29:51 +03:00
}
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)
2020-06-08 03:29:51 +03:00
}
if !lines && (len(vs) != 1 || !foundEOF) {
d.Fatalf("trialing data after top-level value")
2020-06-08 03:29:51 +03:00
}
var s scalar.S
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()
2020-06-08 03:29:51 +03:00
return nil
}
func decodeJSON(d *decode.D, _ any) any {
return decodeJSONEx(d, false)
}
type ToJSONOpts struct {
Indent int
}
// TODO: share with interp code
func makeEncoder(opts ToJSONOpts) *colorjson.Encoder {
return colorjson.NewEncoder(
false,
false,
opts.Indent,
func(v any) any {
switch v := v.(type) {
case gojq.JQValue:
return v.JQValueToGoJQ()
case nil, bool, float64, int, string, *big.Int, map[string]any, []any:
return v
default:
panic(fmt.Sprintf("toValue not a JQValue value: %#v %T", v, v))
}
},
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()
}