From 49319198500729fd65bd6d69071f45f2d7ae2aa7 Mon Sep 17 00:00:00 2001 From: Luke Shumaker Date: Tue, 14 Feb 2023 11:02:09 -0700 Subject: compat/json: Compact, Indent: Clear the output if there's an error --- compat/json/compat.go | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) (limited to 'compat/json/compat.go') diff --git a/compat/json/compat.go b/compat/json/compat.go index 300ab2f..1cdbf0b 100644 --- a/compat/json/compat.go +++ b/compat/json/compat.go @@ -157,18 +157,28 @@ func reencode(dst io.Writer, src []byte, cfg lowmemjson.ReEncoderConfig) error { } func Compact(dst *bytes.Buffer, src []byte) error { - return reencode(dst, src, lowmemjson.ReEncoderConfig{ + start := dst.Len() + err := reencode(dst, src, lowmemjson.ReEncoderConfig{ Compact: true, BackslashEscape: lowmemjson.EscapePreserve, }) + if err != nil { + dst.Truncate(start) + } + return err } func Indent(dst *bytes.Buffer, src []byte, prefix, indent string) error { - return reencode(dst, src, lowmemjson.ReEncoderConfig{ + start := dst.Len() + err := reencode(dst, src, lowmemjson.ReEncoderConfig{ Indent: indent, Prefix: prefix, BackslashEscape: lowmemjson.EscapePreserve, }) + if err != nil { + dst.Truncate(start) + } + return err } func Valid(data []byte) bool { -- cgit v1.2.3-2-g168b