mirror of
https://github.com/jimeh/go-render.git
synced 2026-02-19 03:16:39 +00:00
feat(render): experimental package to render arbitrary values to different formats
This commit is contained in:
45
json.go
Normal file
45
json.go
Normal file
@@ -0,0 +1,45 @@
|
||||
package render
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
)
|
||||
|
||||
// JSON is a renderer that marshals values to JSON.
|
||||
type JSON struct {
|
||||
// Pretty specifies whether the output should be pretty-printed. If true,
|
||||
// the output will be indented and newlines will be added.
|
||||
Pretty bool
|
||||
|
||||
// Prefix is the prefix added to each level of indentation when Pretty is
|
||||
// true.
|
||||
Prefix string
|
||||
|
||||
// Indent is the string added to each level of indentation when Pretty is
|
||||
// true. If empty, two spaces will be used instead.
|
||||
Indent string
|
||||
}
|
||||
|
||||
var _ Renderer = (*JSON)(nil)
|
||||
|
||||
// Render marshals the given value to JSON.
|
||||
func (j *JSON) Render(w io.Writer, v any) error {
|
||||
enc := json.NewEncoder(w)
|
||||
if j.Pretty {
|
||||
prefix := j.Prefix
|
||||
indent := j.Indent
|
||||
if indent == "" {
|
||||
indent = " "
|
||||
}
|
||||
|
||||
enc.SetIndent(prefix, indent)
|
||||
}
|
||||
|
||||
err := enc.Encode(v)
|
||||
if err != nil {
|
||||
return fmt.Errorf("%w: %w", Err, err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
Reference in New Issue
Block a user