Skip to content

Commit

Permalink
chore: json encoder cleanup (#106)
Browse files Browse the repository at this point in the history
  • Loading branch information
unrolled authored Sep 28, 2024
1 parent 024a529 commit 0733292
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 6 deletions.
8 changes: 4 additions & 4 deletions engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ type HTML struct {
bp GenericBufferPool
}

// JSONEncoder match encoding/json.Encoder capabilities.
// JSONEncoder is the interface for encoding/json.Encoder.
type JSONEncoder interface {
Encode(v interface{}) error
SetEscapeHTML(on bool)
Expand All @@ -48,7 +48,7 @@ type JSON struct {
UnEscapeHTML bool
Prefix []byte
StreamingJSON bool
NewEncoder func(w io.Writer) JSONEncoder
Encoder func(w io.Writer) JSONEncoder
}

// JSONP built-in renderer.
Expand Down Expand Up @@ -122,7 +122,7 @@ func (j JSON) Render(w io.Writer, v interface{}) error {
}

var buf bytes.Buffer
encoder := j.NewEncoder(&buf)
encoder := j.Encoder(&buf)
encoder.SetEscapeHTML(!j.UnEscapeHTML)

if j.Indent {
Expand Down Expand Up @@ -163,7 +163,7 @@ func (j JSON) renderStreamingJSON(w io.Writer, v interface{}) error {
_, _ = w.Write(j.Prefix)
}

encoder := j.NewEncoder(w)
encoder := j.Encoder(w)
encoder.SetEscapeHTML(!j.UnEscapeHTML)

if j.Indent {
Expand Down
4 changes: 2 additions & 2 deletions render.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ type Options struct {
// BufferPool to use when rendering HTML templates. If none is supplied
// defaults to SizedBufferPool of size 32 with 512KiB buffers.
BufferPool GenericBufferPool
// Custom JSON Encoder. Default to encoding/json.NewEncoder.
// Custom JSON Encoder. Defaults to encoding/json.NewEncoder.
JSONEncoder func(w io.Writer) JSONEncoder
}

Expand Down Expand Up @@ -535,7 +535,7 @@ func (r *Render) JSON(w io.Writer, status int, v interface{}) error {
Prefix: r.opt.PrefixJSON,
UnEscapeHTML: r.opt.UnEscapeHTML,
StreamingJSON: r.opt.StreamingJSON,
NewEncoder: r.opt.JSONEncoder,
Encoder: r.opt.JSONEncoder,
}

return r.Render(w, j, v)
Expand Down

0 comments on commit 0733292

Please sign in to comment.