-
-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathjson.go
More file actions
32 lines (25 loc) · 734 Bytes
/
json.go
File metadata and controls
32 lines (25 loc) · 734 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
30
31
32
//go:build !tinygo
// +build !tinygo
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 interface{}, options ...func(*json.Encoder)) error {
return NewJSONEncoder(w, options...).Encode(v)
}