| 1 |
//go:build !tinygo |
| 2 |
// +build !tinygo |
| 3 |
|
| 4 |
package gfx |
| 5 |
|
| 6 |
import ( |
| 7 |
"encoding/json" |
| 8 |
"io" |
| 9 |
) |
| 10 |
|
| 11 |
// JSONIndent configures the prefix and indent level of a JSON encoder. |
| 12 |
func JSONIndent(prefix, indent string) func(*json.Encoder) { |
| 13 |
return func(enc *json.Encoder) { |
| 14 |
enc.SetIndent(prefix, indent) |
| 15 |
} |
| 16 |
} |
| 17 |
|
| 18 |
// NewJSONEncoder creates a new JSON encoder for the given io.Writer. |
| 19 |
func NewJSONEncoder(w io.Writer, options ...func(*json.Encoder)) *json.Encoder { |
| 20 |
enc := json.NewEncoder(w) |
| 21 |
|
| 22 |
for _, o := range options { |
| 23 |
o(enc) |
| 24 |
} |
| 25 |
|
| 26 |
return enc |
| 27 |
} |
| 28 |
|
| 29 |
// EncodeJSON creates a new JSON encoder and encodes the provided value. |
| 30 |
func EncodeJSON(w io.Writer, v interface{}, options ...func(*json.Encoder)) error { |
| 31 |
return NewJSONEncoder(w, options...).Encode(v) |
| 32 |
} |