| 1 |
// Package json implements a koanf.Parser that parses JSON bytes as conf maps. |
| 2 |
package json |
| 3 |
|
| 4 |
import ( |
| 5 |
"encoding/json" |
| 6 |
) |
| 7 |
|
| 8 |
// JSON implements a JSON parser. |
| 9 |
type JSON struct{} |
| 10 |
|
| 11 |
// Parser returns a JSON Parser. |
| 12 |
func Parser() *JSON { |
| 13 |
return &JSON{} |
| 14 |
} |
| 15 |
|
| 16 |
// Unmarshal parses the given JSON bytes. |
| 17 |
func (p *JSON) Unmarshal(b []byte) (map[string]any, error) { |
| 18 |
var out map[string]any |
| 19 |
if err := json.Unmarshal(b, &out); err != nil { |
| 20 |
return nil, err |
| 21 |
} |
| 22 |
return out, nil |
| 23 |
} |
| 24 |
|
| 25 |
// Marshal marshals the given config map to JSON bytes. |
| 26 |
func (p *JSON) Marshal(o map[string]any) ([]byte, error) { |
| 27 |
return json.Marshal(o) |
| 28 |
} |