-
-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathjson.go
More file actions
29 lines (23 loc) · 688 Bytes
/
Copy pathjson.go
File metadata and controls
29 lines (23 loc) · 688 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
package gfx
import (
"encoding/json"
"io"
)
// JSONIndent configures the prefix and indent level of a JSON encoder.
func JSONIndent(prefix, indent string) func(*json.Encoder) {
return func(enc *json.Encoder) {
enc.SetIndent(prefix, indent)
}
}
// NewJSONEncoder creates a new JSON encoder for the given io.Writer.
func NewJSONEncoder(w io.Writer, options ...func(*json.Encoder)) *json.Encoder {
enc := json.NewEncoder(w)
for _, o := range options {
o(enc)
}
return enc
}
// EncodeJSON creates a new JSON encoder and encodes the provided value.
func EncodeJSON(w io.Writer, v any, options ...func(*json.Encoder)) error {
return NewJSONEncoder(w, options...).Encode(v)
}