journal/gojq-extended/marshaler.go
2024-11-10 08:13:25 +00:00

28 lines
476 B
Go

package main
import (
"fmt"
"io"
"strings"
)
type marshaler interface {
marshal(any, io.Writer) error
}
type rawMarshaler struct {
m marshaler
checkNul bool
}
func (m *rawMarshaler) marshal(v any, w io.Writer) error {
if s, ok := v.(string); ok {
if m.checkNul && strings.ContainsRune(s, '\x00') {
return fmt.Errorf("cannot output a string containing NUL character: %q", s)
}
_, err := w.Write([]byte(s))
return err
}
return m.m.marshal(v, w)
}