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