summaryrefslogtreecommitdiff
path: root/reencode.go
diff options
context:
space:
mode:
authorLuke Shumaker <lukeshu@datawire.io>2022-08-14 20:52:33 -0600
committerLuke Shumaker <lukeshu@datawire.io>2022-08-17 02:02:47 -0600
commit28dc29b7b05dc9c7ea1cec577963757f75faa601 (patch)
treece7e0c4ddfeed8e2db99bf72383e71fe7fef4f20 /reencode.go
parent35997d235f3bac7c3f9bcd4b8d2b26b0d88dc387 (diff)
Get the new borrowed tests passing
Diffstat (limited to 'reencode.go')
-rw-r--r--reencode.go17
1 files changed, 14 insertions, 3 deletions
diff --git a/reencode.go b/reencode.go
index 7c5ce52..bcb3932 100644
--- a/reencode.go
+++ b/reencode.go
@@ -23,13 +23,16 @@ type ReEncoder struct {
Indent string
// String to put before indents.
Prefix string
+ // Whether to emit a newline after each top-level value, even
+ // if it could unambiguously be omitted.
+ ForceTrailingNewlines bool
// Returns whether a given character in a string should be
// backslash-escaped. The bool argument is whether it was
// \u-escaped in the input. This does not affect characters
// that must or must-not be escaped to be valid JSON.
//
// If not set, then EscapeDefault is used.
- BackslashEscape func(rune, BackslashEscapeMode) BackslashEscapeMode
+ BackslashEscape BackslashEscaper
// state: .Write's utf8-decoding buffer
buf [utf8.UTFMax]byte
@@ -267,8 +270,16 @@ func (enc *ReEncoder) handleRune(c rune, t RuneType) error {
rune(enc.uhex[3])<<0
return enc.emit(writeStringChar(enc.Out, c, BackslashEscapeUnicode, enc.BackslashEscape))
- case RuneTypeError: // EOF
- return nil
+ case RuneTypeEOF: // start of next top-level value
+ if !enc.ForceTrailingNewlines && (enc.Compact || enc.Indent == "") && !enc.lastNonSpace.IsNumber() {
+ return nil
+ }
+ return enc.emitByte('\n')
+ case RuneTypeError: // .Close()
+ if !enc.ForceTrailingNewlines {
+ return nil
+ }
+ return enc.emitByte('\n')
default:
return enc.emitByte(byte(c))
}