diff options
Diffstat (limited to 'compat')
-rw-r--r-- | compat/json/compat.go | 38 | ||||
-rw-r--r-- | compat/json/compat_test.go | 7 |
2 files changed, 18 insertions, 27 deletions
diff --git a/compat/json/compat.go b/compat/json/compat.go index 48d708b..688b35c 100644 --- a/compat/json/compat.go +++ b/compat/json/compat.go @@ -50,45 +50,42 @@ func convertEncodeError(err error) error { return err } -func marshal(v any, formatter *lowmemjson.ReEncoder) ([]byte, error) { +func marshal(v any, cfg lowmemjson.ReEncoderConfig) ([]byte, error) { var buf bytes.Buffer - formatter.Out = &buf - if err := convertEncodeError(lowmemjson.NewEncoder(formatter).Encode(v)); err != nil { + if err := convertEncodeError(lowmemjson.NewEncoder(lowmemjson.NewReEncoder(&buf, cfg)).Encode(v)); err != nil { return nil, err } return buf.Bytes(), nil } func MarshalIndent(v any, prefix, indent string) ([]byte, error) { - return marshal(v, &lowmemjson.ReEncoder{ + return marshal(v, lowmemjson.ReEncoderConfig{ Indent: indent, Prefix: prefix, }) } func Marshal(v any) ([]byte, error) { - return marshal(v, &lowmemjson.ReEncoder{ + return marshal(v, lowmemjson.ReEncoderConfig{ Compact: true, }) } type Encoder struct { encoder *lowmemjson.Encoder - formatter lowmemjson.ReEncoder + formatter *lowmemjson.ReEncoder } func NewEncoder(w io.Writer) *Encoder { ret := &Encoder{ - formatter: lowmemjson.ReEncoder{ - Out: w, - + formatter: lowmemjson.NewReEncoder(w, lowmemjson.ReEncoderConfig{ AllowMultipleValues: true, Compact: true, ForceTrailingNewlines: true, - }, + }), } - ret.encoder = lowmemjson.NewEncoder(&ret.formatter) + ret.encoder = lowmemjson.NewEncoder(ret.formatter) return ret } @@ -126,13 +123,11 @@ func convertReEncodeError(err error) error { } func HTMLEscape(dst *bytes.Buffer, src []byte) { - formatter := &lowmemjson.ReEncoder{ - Out: dst, - } - _, _ = formatter.Write(src) + _, _ = lowmemjson.NewReEncoder(dst, lowmemjson.ReEncoderConfig{}).Write(src) } -func reencode(src []byte, formatter *lowmemjson.ReEncoder) error { +func reencode(dst io.Writer, src []byte, cfg lowmemjson.ReEncoderConfig) error { + formatter := lowmemjson.NewReEncoder(dst, cfg) _, err := formatter.Write(src) if err == nil { err = formatter.Close() @@ -141,16 +136,14 @@ func reencode(src []byte, formatter *lowmemjson.ReEncoder) error { } func Compact(dst *bytes.Buffer, src []byte) error { - return reencode(src, &lowmemjson.ReEncoder{ - Out: dst, + return reencode(dst, src, lowmemjson.ReEncoderConfig{ Compact: true, BackslashEscape: lowmemjson.EscapePreserve, }) } func Indent(dst *bytes.Buffer, src []byte, prefix, indent string) error { - return reencode(src, &lowmemjson.ReEncoder{ - Out: dst, + return reencode(dst, src, lowmemjson.ReEncoderConfig{ Indent: indent, Prefix: prefix, BackslashEscape: lowmemjson.EscapePreserve, @@ -158,10 +151,9 @@ func Indent(dst *bytes.Buffer, src []byte, prefix, indent string) error { } func Valid(data []byte) bool { - formatter := &lowmemjson.ReEncoder{ - Out: io.Discard, + formatter := lowmemjson.NewReEncoder(io.Discard, lowmemjson.ReEncoderConfig{ Compact: true, - } + }) _, err := formatter.Write(data) return err == nil } diff --git a/compat/json/compat_test.go b/compat/json/compat_test.go index dac5e54..feb850b 100644 --- a/compat/json/compat_test.go +++ b/compat/json/compat_test.go @@ -14,11 +14,10 @@ import ( var parseTag = internal.ParseTag -type scanner = lowmemjson.ReEncoder +type scanner = lowmemjson.ReEncoderConfig -func checkValid(in []byte, scan *lowmemjson.ReEncoder) error { - scan.Out = io.Discard - return reencode(in, scan) +func checkValid(in []byte, scan *lowmemjson.ReEncoderConfig) error { + return reencode(io.Discard, in, *scan) } func isValidNumber(s string) bool { |