Skip to content

Commit 886f222

Browse files
authored
Encoder: add SetAddExtraNewline option to json.Encoder (#119)
* Encoder: add SetAddExtraNewline option to json.Encoder * feat: rename AddExtraNewline to AppendNewline * refactor: change AppendNewline to be part of AppendFlags * feat: make appendNewline private
1 parent f3a2210 commit 886f222

File tree

2 files changed

+59
-2
lines changed

2 files changed

+59
-2
lines changed

json/json.go

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,11 @@ const (
7070
// checking of raw messages. It should only be used if the values are
7171
// known to be valid json (e.g., they were created by json.Unmarshal).
7272
TrustRawMessage
73+
74+
// appendNewline is a formatting flag to enable the addition of a newline
75+
// in Encode (this matches the behavior of the standard encoding/json
76+
// package).
77+
appendNewline
7378
)
7479

7580
// ParseFlags is a type used to represent configuration options that can be
@@ -480,7 +485,9 @@ type Encoder struct {
480485
}
481486

482487
// NewEncoder is documented at https://golang.org/pkg/encoding/json/#NewEncoder
483-
func NewEncoder(w io.Writer) *Encoder { return &Encoder{writer: w, flags: EscapeHTML | SortMapKeys} }
488+
func NewEncoder(w io.Writer) *Encoder {
489+
return &Encoder{writer: w, flags: EscapeHTML | SortMapKeys | appendNewline}
490+
}
484491

485492
// Encode is documented at https://golang.org/pkg/encoding/json/#Encoder.Encode
486493
func (enc *Encoder) Encode(v interface{}) error {
@@ -498,7 +505,9 @@ func (enc *Encoder) Encode(v interface{}) error {
498505
return err
499506
}
500507

501-
buf.data = append(buf.data, '\n')
508+
if (enc.flags & appendNewline) != 0 {
509+
buf.data = append(buf.data, '\n')
510+
}
502511
b := buf.data
503512

504513
if enc.prefix != "" || enc.indent != "" {
@@ -556,6 +565,16 @@ func (enc *Encoder) SetTrustRawMessage(on bool) {
556565
}
557566
}
558567

568+
// SetAppendNewline is an extension to the standard encoding/json package which
569+
// allows the program to toggle the addition of a newline in Encode on or off.
570+
func (enc *Encoder) SetAppendNewline(on bool) {
571+
if on {
572+
enc.flags |= appendNewline
573+
} else {
574+
enc.flags &= ^appendNewline
575+
}
576+
}
577+
559578
var encoderBufferPool = sync.Pool{
560579
New: func() interface{} { return &encoderBuffer{data: make([]byte, 0, 4096)} },
561580
}

json/json_test.go

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1669,6 +1669,44 @@ func TestSetTrustRawMessage(t *testing.T) {
16691669
}
16701670
}
16711671

1672+
func TestSetAppendNewline(t *testing.T) {
1673+
buf := &bytes.Buffer{}
1674+
enc := NewEncoder(buf)
1675+
1676+
m := "value"
1677+
1678+
// Default encoding adds an extra newline
1679+
if err := enc.Encode(m); err != nil {
1680+
t.Error(err)
1681+
}
1682+
b := buf.Bytes()
1683+
exp := []byte(`"value"`)
1684+
exp = append(exp, '\n')
1685+
if bytes.Compare(exp, b) != 0 {
1686+
t.Error(
1687+
"unexpected encoding:",
1688+
"expected", exp,
1689+
"got", b,
1690+
)
1691+
}
1692+
1693+
// With SetAppendNewline(false), there shouldn't be a newline in the output
1694+
buf.Reset()
1695+
enc.SetAppendNewline(false)
1696+
if err := enc.Encode(m); err != nil {
1697+
t.Error(err)
1698+
}
1699+
b = buf.Bytes()
1700+
exp = []byte(`"value"`)
1701+
if bytes.Compare(exp, b) != 0 {
1702+
t.Error(
1703+
"unexpected encoding:",
1704+
"expected", exp,
1705+
"got", b,
1706+
)
1707+
}
1708+
}
1709+
16721710
func TestEscapeString(t *testing.T) {
16731711
b := Escape(`value`)
16741712
x := []byte(`"value"`)

0 commit comments

Comments
 (0)