Skip to content

Commit

Permalink
Encoder: add SetAddExtraNewline option to json.Encoder (#119)
Browse files Browse the repository at this point in the history
* Encoder: add SetAddExtraNewline option to json.Encoder

* feat: rename AddExtraNewline to AppendNewline

* refactor: change AppendNewline to be part of AppendFlags

* feat: make appendNewline private
  • Loading branch information
Jerska committed Feb 16, 2022
1 parent f3a2210 commit 886f222
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 2 deletions.
23 changes: 21 additions & 2 deletions json/json.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,11 @@ const (
// checking of raw messages. It should only be used if the values are
// known to be valid json (e.g., they were created by json.Unmarshal).
TrustRawMessage

// appendNewline is a formatting flag to enable the addition of a newline
// in Encode (this matches the behavior of the standard encoding/json
// package).
appendNewline
)

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

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

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

buf.data = append(buf.data, '\n')
if (enc.flags & appendNewline) != 0 {
buf.data = append(buf.data, '\n')
}
b := buf.data

if enc.prefix != "" || enc.indent != "" {
Expand Down Expand Up @@ -556,6 +565,16 @@ func (enc *Encoder) SetTrustRawMessage(on bool) {
}
}

// SetAppendNewline is an extension to the standard encoding/json package which
// allows the program to toggle the addition of a newline in Encode on or off.
func (enc *Encoder) SetAppendNewline(on bool) {
if on {
enc.flags |= appendNewline
} else {
enc.flags &= ^appendNewline
}
}

var encoderBufferPool = sync.Pool{
New: func() interface{} { return &encoderBuffer{data: make([]byte, 0, 4096)} },
}
Expand Down
38 changes: 38 additions & 0 deletions json/json_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1669,6 +1669,44 @@ func TestSetTrustRawMessage(t *testing.T) {
}
}

func TestSetAppendNewline(t *testing.T) {
buf := &bytes.Buffer{}
enc := NewEncoder(buf)

m := "value"

// Default encoding adds an extra newline
if err := enc.Encode(m); err != nil {
t.Error(err)
}
b := buf.Bytes()
exp := []byte(`"value"`)
exp = append(exp, '\n')
if bytes.Compare(exp, b) != 0 {
t.Error(
"unexpected encoding:",
"expected", exp,
"got", b,
)
}

// With SetAppendNewline(false), there shouldn't be a newline in the output
buf.Reset()
enc.SetAppendNewline(false)
if err := enc.Encode(m); err != nil {
t.Error(err)
}
b = buf.Bytes()
exp = []byte(`"value"`)
if bytes.Compare(exp, b) != 0 {
t.Error(
"unexpected encoding:",
"expected", exp,
"got", b,
)
}
}

func TestEscapeString(t *testing.T) {
b := Escape(`value`)
x := []byte(`"value"`)
Expand Down

0 comments on commit 886f222

Please sign in to comment.