diff options
Diffstat (limited to 'compat/json/compat.go')
-rw-r--r-- | compat/json/compat.go | 21 |
1 files changed, 20 insertions, 1 deletions
diff --git a/compat/json/compat.go b/compat/json/compat.go index edc6908..d33f278 100644 --- a/compat/json/compat.go +++ b/compat/json/compat.go @@ -188,6 +188,15 @@ func Compact(dst *bytes.Buffer, src []byte) error { return err } +func isSpace(c byte) bool { + switch c { + case 0x0020, 0x000A, 0x000D, 0x0009: + return true + default: + return false + } +} + func Indent(dst *bytes.Buffer, src []byte, prefix, indent string) error { start := dst.Len() err := reencode(dst, src, lowmemjson.ReEncoderConfig{ @@ -198,8 +207,18 @@ func Indent(dst *bytes.Buffer, src []byte, prefix, indent string) error { }) if err != nil { dst.Truncate(start) + return err } - return err + + // Preserve trailing whitespace. + lastNonWS := len(src) - 1 + for ; lastNonWS >= 0 && isSpace(src[lastNonWS]); lastNonWS-- { + } + if _, err := dst.Write(src[lastNonWS+1:]); err != nil { + return err + } + + return nil } func Valid(data []byte) bool { |