diff options
author | Luke Shumaker <lukeshu@lukeshu.com> | 2023-02-14 11:02:09 -0700 |
---|---|---|
committer | Luke Shumaker <lukeshu@lukeshu.com> | 2023-02-18 22:45:54 -0700 |
commit | 49319198500729fd65bd6d69071f45f2d7ae2aa7 (patch) | |
tree | 95ca5950c0abd8f2ad8e8770799e7802454c62f0 /compat/json/compat.go | |
parent | eaaf7bc29d43b4470623c75e6e409a049b3083af (diff) |
compat/json: Compact, Indent: Clear the output if there's an error
Diffstat (limited to 'compat/json/compat.go')
-rw-r--r-- | compat/json/compat.go | 14 |
1 files changed, 12 insertions, 2 deletions
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 { |